From 753b95be1c872b70cb2de654409d66ad8d9638cc Mon Sep 17 00:00:00 2001 From: Liam Naddell Date: Fri, 19 Jul 2024 20:34:47 -0400 Subject: [PATCH] Builder style! --- .../expand/rust-macro-builtins-utility.cc | 46 ++++++++++++------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/gcc/rust/expand/rust-macro-builtins-utility.cc b/gcc/rust/expand/rust-macro-builtins-utility.cc index 7b368bdc6b3..2e399438011 100644 --- a/gcc/rust/expand/rust-macro-builtins-utility.cc +++ b/gcc/rust/expand/rust-macro-builtins-utility.cc @@ -16,8 +16,11 @@ // along with GCC; see the file COPYING3. If not see // . +//TODO: Make sure all of these are needded #include "rust-fmt.h" +#include #include "rust-expr.h" +#include "rust-ast-builder.h" #include "rust-macro-builtins.h" #include "rust-macro-builtins-helpers.h" @@ -234,15 +237,10 @@ tl::optional MacroBuiltin::option_env_handler (location_t invoc_locus, AST::MacroInvocData &invoc, AST::InvocKind semicolon) { + //TODO: Use ast/rust-ast-builder.h instead. auto invoc_token_tree = invoc.get_delim_tok_tree (); MacroInvocLexer lex (invoc_token_tree.to_token_stream ()); Parser parser (lex); - //create copy of option - AST::PathExprSegment p_option ("Option",invoc_locus); - AST::PathExprSegment p_none ("None",invoc_locus); - AST::PathExprSegment p_some ("Some",invoc_locus); - std::unique_ptr none (new AST::PathInExpression(std::vector({p_option,p_none}),std::vector (), invoc_locus)); - std::unique_ptr some (new AST::PathInExpression(std::vector({p_option,p_some}),std::vector (), invoc_locus)); auto last_token_id = macro_end_token (invoc_token_tree, parser); std::unique_ptr lit_expr = nullptr; @@ -281,28 +279,42 @@ MacroBuiltin::option_env_handler (location_t invoc_locus, AST::MacroInvocData &i } } + //TODO: Comments + //TODO: Pass tokens through parser.skip_token (last_token_id); auto env_value = getenv (lit_expr->as_string ().c_str ()); + AST::Builder b(invoc_locus); + std::string option = "Option"; if (env_value == nullptr) { - //TODO: Fix + //TODO: Fix token auto tok = make_token (Token::make_string (invoc_locus, std::move ("DELETE ME"))); - auto node = AST::SingleASTNode (std::move(none)); - std::vector nodes({node}); + + std::string none = "None"; + std::unique_ptr none_expr = std::unique_ptr(new AST::PathInExpression(b.path_in_expression(std::vector({option,none})))); + + auto node = AST::SingleASTNode (std::move(none_expr)); + std::vector nodes; + nodes.push_back(node); + return AST::Fragment (nodes, std::move (tok)); } - std::vector> es; - es.push_back(make_string (invoc_locus, env_value)); - std::unique_ptr e_some (new AST::CallExpr(std::move(some), std::move(es),std::vector (), invoc_locus)); - auto node = AST::SingleASTNode (std::move(e_some)); + std::string some = "Some"; + //TODO: Vector constructors + std::vector> args; + args.push_back(b.literal_string(env_value)); - //TODO: FIx - auto tok - = make_token (Token::make_string (invoc_locus, std::move (env_value))); + std::unique_ptr some_expr = b.call(std::unique_ptr(new AST::PathInExpression(b.path_in_expression(std::vector({option,some})))),std::move(args)); + + auto node = AST::SingleASTNode (std::move(some_expr)); + + //TODO: Fix + auto tok = make_token (Token::make_string (invoc_locus, std::move (env_value))); - std::vector nodes({node}); + std::vector nodes; + nodes.push_back(node); return AST::Fragment (nodes, std::move (tok)); }