Skip to content

Commit

Permalink
expand: Switch semicolon boolean to an enum instead.
Browse files Browse the repository at this point in the history
gcc/rust/ChangeLog:

	* ast/rust-ast-fragment.h (enum class): Add InvocKind and AsmKind enums.
	* ast/rust-macro.h: Switch semicolon boolean to InvocKind enum.
	* expand/rust-expand-visitor.cc (ExpandVisitor::visit): Likewise.
	* expand/rust-macro-builtins-asm.cc (MacroBuiltin::asm_handler): Likewise.
	(parse_asm): Likewise.
	* expand/rust-macro-builtins-asm.h (parse_asm): Likewise.
	* expand/rust-macro-builtins-format-args.cc (MacroBuiltin::format_args_handler): Likewise.
	* expand/rust-macro-builtins-include.cc (MacroBuiltin::include_bytes_handler): Likewise.
	(MacroBuiltin::include_str_handler): Likewise.
	(MacroBuiltin::include_handler): Likewise.
	* expand/rust-macro-builtins-location.cc (MacroBuiltin::file_handler): Likewise.
	(MacroBuiltin::column_handler): Likewise.
	(MacroBuiltin::line_handler): Likewise.
	* expand/rust-macro-builtins-log-debug.cc (MacroBuiltin::assert_handler): Likewise.
	* expand/rust-macro-builtins-utility.cc (MacroBuiltin::compile_error_handler): Likewise.
	(MacroBuiltin::concat_handler): Likewise.
	(MacroBuiltin::env_handler): Likewise.
	(MacroBuiltin::cfg_handler): Likewise.
	(MacroBuiltin::stringify_handler): Likewise.
	* expand/rust-macro-builtins.cc (format_args_maker): Likewise.
	(enum class): Likewise.
	(inline_asm_maker): Likewise.
	(MacroBuiltin::sorry): Likewise.
	(MacroBuiltin::proc_macro_builtin): Likewise.
	* expand/rust-macro-builtins.h: Likewise.
	* expand/rust-macro-expand.cc (MacroExpander::expand_decl_macro): Likewise.
	(MacroExpander::expand_eager_invocations): Likewise.
	(MacroExpander::expand_invoc): Likewise.
	* expand/rust-macro-expand.h (struct MacroExpander): Likewise.
  • Loading branch information
CohenArthur committed Jun 18, 2024
1 parent fb906a4 commit 7d22e90
Show file tree
Hide file tree
Showing 14 changed files with 105 additions and 73 deletions.
14 changes: 13 additions & 1 deletion gcc/rust/ast/rust-ast-fragment.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,25 @@ class Fragment
void assert_single_fragment (SingleASTNode::NodeType expected) const;
};

enum class InvocKind
{
Expr,
Semicoloned,
};

enum class AsmKind
{
Global,
Inline
};

/**
* This is the type for transcriber functions found in
* rust-macro-builtins.{h,cc}.
*/
using MacroTranscriberFunc
= std::function<tl::optional<Fragment> (location_t, MacroInvocData &,
bool semicolon)>;
InvocKind semicolon)>;

} // namespace AST
} // namespace Rust
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 @@ -482,7 +482,7 @@ class MacroRulesDefinition : public VisItem
* should make use of the actual rules. If the macro is builtin, then another
* associated transcriber should be used
*/
static Fragment dummy_builtin (location_t, MacroInvocData &, bool)
static Fragment dummy_builtin (location_t, MacroInvocData &, AST::InvocKind)
{
rust_unreachable ();
return Fragment::create_error ();
Expand Down
5 changes: 4 additions & 1 deletion gcc/rust/expand/rust-expand-visitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
// <http://www.gnu.org/licenses/>.

#include "rust-expand-visitor.h"
#include "rust-ast-fragment.h"
#include "rust-proc-macro.h"
#include "rust-attributes.h"
#include "rust-ast.h"
Expand Down Expand Up @@ -467,7 +468,9 @@ void
ExpandVisitor::visit (AST::MacroInvocation &macro_invoc)
{
// TODO: Can we do the AST fragment replacing here? Probably not, right?
expander.expand_invoc (macro_invoc, macro_invoc.has_semicolon ());
expander.expand_invoc (macro_invoc, macro_invoc.has_semicolon ()
? AST::InvocKind::Semicoloned
: AST::InvocKind::Expr);
}

void
Expand Down
20 changes: 12 additions & 8 deletions gcc/rust/expand/rust-macro-builtins-asm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
// along with GCC; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.

#include "rust-make-unique.h"
#include "rust-macro-builtins-asm.h"
#include "rust-ast-fragment.h"
#include "rust-ast.h"
#include "rust-stmt.h"

Expand Down Expand Up @@ -537,9 +539,9 @@ parse_format_string (InlineAsmContext &inline_asm_ctx)

tl::optional<AST::Fragment>
MacroBuiltin::asm_handler (location_t invoc_locus, AST::MacroInvocData &invoc,
bool semicolon, bool is_global_asm)
AST::InvocKind semicolon, AST::AsmKind is_global_asm)
{
return parse_asm (invoc_locus, invoc, is_global_asm, semicolon);
return parse_asm (invoc_locus, invoc, semicolon, is_global_asm);
}

tl::expected<InlineAsmContext, std::string>
Expand Down Expand Up @@ -609,7 +611,7 @@ parse_asm_arg (InlineAsmContext inline_asm_ctx)

tl::optional<AST::Fragment>
parse_asm (location_t invoc_locus, AST::MacroInvocData &invoc,
bool is_global_asm, bool semicolon)
AST::InvocKind semicolon, AST::AsmKind is_global_asm)
{
// From the rule of asm.
// We first peek and see if it is a format string or not.
Expand All @@ -631,7 +633,8 @@ parse_asm (location_t invoc_locus, AST::MacroInvocData &invoc,
Parser<MacroInvocLexer> parser (lex);
auto last_token_id = macro_end_token (invoc.get_delim_tok_tree (), parser);

AST::InlineAsm inline_asm (invoc_locus, is_global_asm);
AST::InlineAsm inline_asm (invoc_locus,
is_global_asm == AST::AsmKind::Global);
auto inline_asm_ctx = InlineAsmContext (inline_asm, parser, last_token_id);

// operands stream, also handles the optional ","
Expand All @@ -654,10 +657,11 @@ parse_asm (location_t invoc_locus, AST::MacroInvocData &invoc,

// If the macro invocation has a semicolon (`asm!("...");`), then we need
// to make it a statement. This way, it will be expanded properly.
if (semicolon)
single_vec.emplace_back (
AST::SingleASTNode (std::unique_ptr<AST::Stmt> (
new AST::ExprStmt (std::move (node), invoc_locus, semicolon))));
if (semicolon == AST::InvocKind::Semicoloned)
single_vec.emplace_back (AST::SingleASTNode (
Rust::make_unique<AST::ExprStmt> (std::move (node), invoc_locus,
semicolon
== AST::InvocKind::Semicoloned)));
else
single_vec.emplace_back (AST::SingleASTNode (std::move (node)));

Expand Down
3 changes: 2 additions & 1 deletion gcc/rust/expand/rust-macro-builtins-asm.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@

#include "rust-ast-fragment.h"
#include "rust-macro-builtins.h"
#include "rust-macro-builtins-helpers.h"
#include "expected.h"
Expand Down Expand Up @@ -59,7 +60,7 @@ parse_reg_operand (InlineAsmContext inline_asm_ctx);

tl::optional<AST::Fragment>
parse_asm (location_t invoc_locus, AST::MacroInvocData &invoc,
bool is_global_asm, bool semicolon);
AST::InvocKind semicolon, AST::AsmKind is_global_asm);

bool
check_identifier (Parser<MacroInvocLexer> &parser, std::string ident);
Expand Down
4 changes: 3 additions & 1 deletion gcc/rust/expand/rust-macro-builtins-format-args.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
// 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-ast-fragment.h"
#include "rust-macro-builtins-helpers.h"
#include "rust-expand-format-args.h"

Expand Down Expand Up @@ -115,7 +116,8 @@ format_args_parse_arguments (AST::MacroInvocData &invoc)

tl::optional<AST::Fragment>
MacroBuiltin::format_args_handler (location_t invoc_locus,
AST::MacroInvocData &invoc, bool semicolon,
AST::MacroInvocData &invoc,
AST::InvocKind semicolon,
AST::FormatArgs::Newline nl)
{
auto input = format_args_parse_arguments (invoc);
Expand Down
10 changes: 7 additions & 3 deletions gcc/rust/expand/rust-macro-builtins-include.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// along with GCC; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.

#include "rust-ast-fragment.h"
#include "rust-common.h"
#include "rust-macro-builtins.h"
#include "rust-macro-builtins-helpers.h"
Expand All @@ -27,7 +28,8 @@ of the given file as reference to a byte array. Yields an expression of type

tl::optional<AST::Fragment>
MacroBuiltin::include_bytes_handler (location_t invoc_locus,
AST::MacroInvocData &invoc, bool semicolon)
AST::MacroInvocData &invoc,
AST::InvocKind semicolon)
{
/* Get target filename from the macro invocation, which is treated as a path
relative to the include!-ing file (currently being compiled). */
Expand Down Expand Up @@ -93,7 +95,8 @@ MacroBuiltin::include_bytes_handler (location_t invoc_locus,

tl::optional<AST::Fragment>
MacroBuiltin::include_str_handler (location_t invoc_locus,
AST::MacroInvocData &invoc, bool semicolon)
AST::MacroInvocData &invoc,
AST::InvocKind semicolon)
{
/* Get target filename from the macro invocation, which is treated as a path
relative to the include!-ing file (currently being compiled). */
Expand Down Expand Up @@ -182,7 +185,8 @@ scope compile time. */

tl::optional<AST::Fragment>
MacroBuiltin::include_handler (location_t invoc_locus,
AST::MacroInvocData &invoc, bool semicolon)
AST::MacroInvocData &invoc,
AST::InvocKind semicolon)
{
/* Get target filename from the macro invocation, which is treated as a path
relative to the include!-ing file (currently being compiled). */
Expand Down
9 changes: 6 additions & 3 deletions gcc/rust/expand/rust-macro-builtins-location.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@
// along with GCC; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.

#include "rust-ast-fragment.h"
#include "rust-macro-builtins.h"
#include "rust-macro-builtins-helpers.h"

namespace Rust {
tl::optional<AST::Fragment>
MacroBuiltin::file_handler (location_t invoc_locus, AST::MacroInvocData &, bool)
MacroBuiltin::file_handler (location_t invoc_locus, AST::MacroInvocData &,
AST::InvocKind)
{
auto current_file = LOCATION_FILE (invoc_locus);
auto file_str = AST::SingleASTNode (make_string (invoc_locus, current_file));
Expand All @@ -33,7 +35,7 @@ MacroBuiltin::file_handler (location_t invoc_locus, AST::MacroInvocData &, bool)

tl::optional<AST::Fragment>
MacroBuiltin::column_handler (location_t invoc_locus, AST::MacroInvocData &,
bool)
AST::InvocKind)
{
auto current_column = LOCATION_COLUMN (invoc_locus);

Expand All @@ -47,7 +49,8 @@ MacroBuiltin::column_handler (location_t invoc_locus, AST::MacroInvocData &,
}

tl::optional<AST::Fragment>
MacroBuiltin::line_handler (location_t invoc_locus, AST::MacroInvocData &, bool)
MacroBuiltin::line_handler (location_t invoc_locus, AST::MacroInvocData &,
AST::InvocKind)
{
auto current_line = LOCATION_LINE (invoc_locus);

Expand Down
4 changes: 3 additions & 1 deletion gcc/rust/expand/rust-macro-builtins-log-debug.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
// along with GCC; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.

#include "rust-ast-fragment.h"
#include "rust-macro-builtins.h"
#include "rust-macro-builtins-helpers.h"

namespace Rust {
tl::optional<AST::Fragment>
MacroBuiltin::assert_handler (location_t invoc_locus,
AST::MacroInvocData &invoc, bool semicolon)
AST::MacroInvocData &invoc,
AST::InvocKind semicolon)
{
rust_debug ("assert!() called");

Expand Down
13 changes: 8 additions & 5 deletions gcc/rust/expand/rust-macro-builtins-utility.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ namespace Rust {
during the compile time. */
tl::optional<AST::Fragment>
MacroBuiltin::compile_error_handler (location_t invoc_locus,
AST::MacroInvocData &invoc, bool semicolon)
AST::MacroInvocData &invoc,
AST::InvocKind semicolon)
{
auto lit_expr
= parse_single_string_literal (BuiltinMacro::CompileError,
Expand Down Expand Up @@ -87,7 +88,8 @@ MacroBuiltin::compile_error_handler (location_t invoc_locus,
// Can we do that easily?
tl::optional<AST::Fragment>
MacroBuiltin::concat_handler (location_t invoc_locus,
AST::MacroInvocData &invoc, bool semicolon)
AST::MacroInvocData &invoc,
AST::InvocKind semicolon)
{
auto invoc_token_tree = invoc.get_delim_tok_tree ();
MacroInvocLexer lex (invoc_token_tree.to_token_stream ());
Expand Down Expand Up @@ -152,7 +154,7 @@ MacroBuiltin::concat_handler (location_t invoc_locus,
compile time. */
tl::optional<AST::Fragment>
MacroBuiltin::env_handler (location_t invoc_locus, AST::MacroInvocData &invoc,
bool semicolon)
AST::InvocKind semicolon)
{
auto invoc_token_tree = invoc.get_delim_tok_tree ();
MacroInvocLexer lex (invoc_token_tree.to_token_stream ());
Expand Down Expand Up @@ -226,7 +228,7 @@ MacroBuiltin::env_handler (location_t invoc_locus, AST::MacroInvocData &invoc,

tl::optional<AST::Fragment>
MacroBuiltin::cfg_handler (location_t invoc_locus, AST::MacroInvocData &invoc,
bool semicolon)
AST::InvocKind semicolon)
{
// only parse if not already parsed
if (!invoc.is_parsed ())
Expand Down Expand Up @@ -265,7 +267,8 @@ MacroBuiltin::cfg_handler (location_t invoc_locus, AST::MacroInvocData &invoc,

tl::optional<AST::Fragment>
MacroBuiltin::stringify_handler (location_t invoc_locus,
AST::MacroInvocData &invoc, bool semicolon)
AST::MacroInvocData &invoc,
AST::InvocKind semicolon)
{
std::string content;
auto invoc_token_tree = invoc.get_delim_tok_tree ();
Expand Down
30 changes: 12 additions & 18 deletions gcc/rust/expand/rust-macro-builtins.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,26 +87,19 @@ const BiMap<std::string, BuiltinMacro> MacroBuiltin::builtins = {{
AST::MacroTranscriberFunc
format_args_maker (AST::FormatArgs::Newline nl)
{
return [nl] (location_t loc, AST::MacroInvocData &invoc, bool semicolon) {
return [nl] (location_t loc, AST::MacroInvocData &invoc,
AST::InvocKind semicolon) {
return MacroBuiltin::format_args_handler (loc, invoc, semicolon, nl);
};
}

enum class isGlobalAsm
{
Global,
Inline,
};

AST::MacroTranscriberFunc
inline_asm_maker (isGlobalAsm is_global_asm)
inline_asm_maker (AST::AsmKind global_asm)
{
bool global_asm = is_global_asm == isGlobalAsm::Global ? true : false;

return
[global_asm] (location_t loc, AST::MacroInvocData &invoc, bool semicolon) {
return MacroBuiltin::asm_handler (loc, invoc, semicolon, global_asm);
};
return [global_asm] (location_t loc, AST::MacroInvocData &invoc,
AST::InvocKind semicolon) {
return MacroBuiltin::asm_handler (loc, invoc, semicolon, global_asm);
};
}

std::unordered_map<std::string, AST::MacroTranscriberFunc>
Expand All @@ -125,8 +118,8 @@ std::unordered_map<std::string, AST::MacroTranscriberFunc>
{"include", MacroBuiltin::include_handler},
{"format_args", format_args_maker (AST::FormatArgs::Newline::No)},
{"format_args_nl", format_args_maker (AST::FormatArgs::Newline::Yes)},
{"asm", inline_asm_maker (isGlobalAsm::Inline)},
{"global_asm", inline_asm_maker (isGlobalAsm::Global)},
{"asm", inline_asm_maker (AST::AsmKind::Inline)},
{"global_asm", inline_asm_maker (AST::AsmKind::Global)},
/* Unimplemented macro builtins */
{"option_env", MacroBuiltin::sorry},
{"concat_idents", MacroBuiltin::sorry},
Expand Down Expand Up @@ -169,7 +162,7 @@ builtin_macro_from_string (const std::string &identifier)

tl::optional<AST::Fragment>
MacroBuiltin::sorry (location_t invoc_locus, AST::MacroInvocData &invoc,
bool semicolon)
AST::InvocKind semicolon)
{
rust_sorry_at (invoc_locus, "unimplemented builtin macro: %qs",
invoc.get_path ().as_string ().c_str ());
Expand All @@ -179,7 +172,8 @@ MacroBuiltin::sorry (location_t invoc_locus, AST::MacroInvocData &invoc,

tl::optional<AST::Fragment>
MacroBuiltin::proc_macro_builtin (location_t invoc_locus,
AST::MacroInvocData &invoc, bool semicolon)
AST::MacroInvocData &invoc,
AST::InvocKind semicolon)
{
rust_error_at (invoc_locus, "cannot invoke derive macro: %qs",
invoc.get_path ().as_string ().c_str ());
Expand Down
Loading

0 comments on commit 7d22e90

Please sign in to comment.