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
* 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 ();
// <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"
ExpandVisitor::visit (AST::MacroInvocation ¯o_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
// 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"
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>
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.
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 ","
// 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)));
+#include "rust-ast-fragment.h"
#include "rust-macro-builtins.h"
#include "rust-macro-builtins-helpers.h"
#include "expected.h"
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);
// 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"
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);
// 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"
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). */
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). */
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). */
// 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));
tl::optional<AST::Fragment>
MacroBuiltin::column_handler (location_t invoc_locus, AST::MacroInvocData &,
- bool)
+ AST::InvocKind)
{
auto current_column = LOCATION_COLUMN (invoc_locus);
}
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);
// 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");
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,
// 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 ());
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 ());
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 ())
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 ();
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>
{"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},
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 ());
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 ());
static tl::optional<AST::Fragment> assert_handler (location_t invoc_locus,
AST::MacroInvocData &invoc,
- bool semicolon);
+ AST::InvocKind semicolon);
static tl::optional<AST::Fragment> file_handler (location_t invoc_locus,
AST::MacroInvocData &invoc,
- bool semicolon);
+ AST::InvocKind semicolon);
static tl::optional<AST::Fragment> column_handler (location_t invoc_locus,
AST::MacroInvocData &invoc,
- bool semicolon);
+ AST::InvocKind semicolon);
static tl::optional<AST::Fragment>
include_bytes_handler (location_t invoc_locus, AST::MacroInvocData &invoc,
- bool semicolon);
+ AST::InvocKind semicolon);
static tl::optional<AST::Fragment>
include_str_handler (location_t invoc_locus, AST::MacroInvocData &invoc,
- bool semicolon);
+ AST::InvocKind semicolon);
static tl::optional<AST::Fragment>
stringify_handler (location_t invoc_locus, AST::MacroInvocData &invoc,
- bool semicolon);
+ AST::InvocKind semicolon);
static tl::optional<AST::Fragment>
compile_error_handler (location_t invoc_locus, AST::MacroInvocData &invoc,
- bool semicolon);
+ AST::InvocKind semicolon);
static tl::optional<AST::Fragment> concat_handler (location_t invoc_locus,
AST::MacroInvocData &invoc,
- bool semicolon);
+ AST::InvocKind semicolon);
static tl::optional<AST::Fragment> env_handler (location_t invoc_locus,
AST::MacroInvocData &invoc,
- bool semicolon);
+ AST::InvocKind semicolon);
static tl::optional<AST::Fragment> cfg_handler (location_t invoc_locus,
AST::MacroInvocData &invoc,
- bool semicolon);
+ AST::InvocKind semicolon);
static tl::optional<AST::Fragment>
include_handler (location_t invoc_locus, AST::MacroInvocData &invoc,
- bool semicolon);
+ AST::InvocKind semicolon);
static tl::optional<AST::Fragment> line_handler (location_t invoc_locus,
AST::MacroInvocData &invoc,
- bool semicolon);
+ AST::InvocKind semicolon);
static tl::optional<AST::Fragment> asm_handler (location_t invoc_locus,
AST::MacroInvocData &invoc,
- bool semicolon,
- bool is_global_asm);
+ AST::InvocKind semicolon,
+ AST::AsmKind is_global_asm);
static tl::optional<AST::Fragment>
format_args_handler (location_t invoc_locus, AST::MacroInvocData &invoc,
- bool semicolon, AST::FormatArgs::Newline nl);
+ AST::InvocKind semicolon, AST::FormatArgs::Newline nl);
- static tl::optional<AST::Fragment>
- sorry (location_t invoc_locus, AST::MacroInvocData &invoc, bool semicolon);
+ static tl::optional<AST::Fragment> sorry (location_t invoc_locus,
+ AST::MacroInvocData &invoc,
+ AST::InvocKind semicolon);
/* Builtin procedural macros do not work directly on tokens, but still need a
* builtin transcriber to be considered proper builtin macros */
static tl::optional<AST::Fragment>
- proc_macro_builtin (location_t, AST::MacroInvocData &, bool);
+ proc_macro_builtin (location_t, AST::MacroInvocData &, AST::InvocKind);
};
} // namespace Rust
#include "rust-macro-expand.h"
#include "optional.h"
+#include "rust-ast-fragment.h"
#include "rust-macro-substitute-ctx.h"
#include "rust-ast-full.h"
#include "rust-ast-visitor.h"
MacroExpander::expand_decl_macro (location_t invoc_locus,
AST::MacroInvocData &invoc,
AST::MacroRulesDefinition &rules_def,
- bool semicolon)
+ AST::InvocKind semicolon)
{
// ensure that both invocation and rules are in a valid state
rust_assert (!invoc.is_marked_for_strip ());
for (auto kv : substitution_map)
{
auto &to_expand = kv.second;
- expand_invoc (*to_expand, false);
+ expand_invoc (*to_expand, AST::InvocKind::Expr);
auto fragment = take_expanded_fragment ();
auto &new_tokens = fragment.get_tokens ();
}
void
-MacroExpander::expand_invoc (AST::MacroInvocation &invoc, bool has_semicolon)
+MacroExpander::expand_invoc (AST::MacroInvocation &invoc,
+ AST::InvocKind semicolon)
{
if (depth_exceeds_recursion_limit ())
{
last_invoc = *invoc.clone_macro_invocation_impl ();
last_def = *rdef;
- rust_debug ("[ARTHUR] semicolon: %s", has_semicolon ? "yes" : "no");
-
if (rdef->is_builtin ())
fragment = rdef
->get_builtin_transcriber () (invoc.get_locus (), invoc_data,
- has_semicolon)
+ semicolon)
.value_or (AST::Fragment::create_empty ());
else
- fragment = expand_decl_macro (invoc.get_locus (), invoc_data, *rdef,
- has_semicolon);
+ fragment
+ = expand_decl_macro (invoc.get_locus (), invoc_data, *rdef, semicolon);
set_expanded_fragment (std::move (fragment));
}
MacroExpander::transcribe_rule (
AST::MacroRule &match_rule, AST::DelimTokenTree &invoc_token_tree,
std::map<std::string, MatchedFragmentContainer *> &matched_fragments,
- bool semicolon, ContextType ctx)
+ AST::InvocKind invoc_kind, ContextType ctx)
{
+ bool semicolon = invoc_kind == AST::InvocKind::Semicoloned;
+
// we can manipulate the token tree to substitute the dollar identifiers so
// that when we call parse its already substituted for us
AST::MacroTranscriber &transcriber = match_rule.get_transcriber ();
#define RUST_MACRO_EXPAND_H
#include "optional.h"
+#include "rust-ast-fragment.h"
#include "rust-buffered-queue.h"
#include "rust-parse.h"
#include "rust-token.h"
/* Expands a macro invocation - possibly make both
* have similar duck-typed interface and use templates?*/
// should this be public or private?
- void expand_invoc (AST::MacroInvocation &invoc, bool has_semicolon);
+ void expand_invoc (AST::MacroInvocation &invoc, AST::InvocKind semicolon);
// Expands a single declarative macro.
AST::Fragment expand_decl_macro (location_t locus, AST::MacroInvocData &invoc,
AST::MacroRulesDefinition &rules_def,
- bool semicolon);
+ AST::InvocKind semicolon);
bool depth_exceeds_recursion_limit () const;
AST::Fragment transcribe_rule (
AST::MacroRule &match_rule, AST::DelimTokenTree &invoc_token_tree,
std::map<std::string, MatchedFragmentContainer *> &matched_fragments,
- bool semicolon, ContextType ctx);
+ AST::InvocKind invoc_kind, ContextType ctx);
bool match_fragment (Parser<MacroInvocLexer> &parser,
AST::MacroMatchFragment &fragment);