-
Notifications
You must be signed in to change notification settings - Fork 173
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
Return proper AST nodes from format_args!() expansion #2859
Conversation
aaddca5
to
0513f60
Compare
56641d0
to
2ec9838
Compare
It might be easier to start by expanding format_args calls as a a regular macro expansion - which is what is done in #2893. So I'll drop the last commit here and we can revisit expansion as part of HIR lowering later. The reason I'm not doing it now is that I would really like to get this over the line, and while expanding as part of HIR lowering has certain benefits, it's also harder and requires modifications of our HIR builders in order to ensure we're using the proper DefIds, etc. Doing it this way is much easier. |
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.
I'm trying to understand the codebase/ Can i ask if the class ASTLoweringExpr is just implementing different visiting methods so an AST representing an expression can accept this visitor and the new ASTLoweringExpr class will be the HIR?
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.
(good initiative!)
yes, basically all of our "lowering visitors" are simple classes which take care of a subset of the AST nodes we have. So you'll have an ASTLoweringExpr
, which takes care of lowering all expr-like nodes - function calls, literal expressions, binary operations, etc.
you'll also have an ASTLoweringStmt
(https://github.com/Rust-GCC/gccrs/blob/master/gcc/rust/hir/rust-ast-lower-stmt.h) for all statements, and so on.
The goal of these visitors is to take in an AST node (so if you look at ASTLoweringStmt, there's an override for the visit function which takes an AST::LetStmt
node, as variable declarations are statements: declaration.
If you look at the implementation of this function, you'll see there's not much going on - if we look at all that a let statement can be, we get something like this:
let ComplexPattern { a, b, c }: ComplexPattern = get_complex_pattern();
so we have to "visit" and "lower" each of those elements - the pattern, the type, and the initial expression. but in its simplest form a let statement can also be this:
let a;
with just a pattern, no type annotation and no inital expression.
then, we get mappings for our new HIR node we're going to be creating, as all HIR nodes can be refered to using a sort of index/mapping, and we just create our new HIR::LetStmt
node. This visitor is structured in such a way that it keeps an HIR::Stmt* translated
member, which will store the translated/lowered version of the AST node it received.
each of these top-level visitors (such as ASTLoweringStmt
will then return the HIR node they've created: https://github.com/Rust-GCC/gccrs/blob/7064be37a38b4c3c8ad8193e13312e2c558303dc/gcc/rust/hir/rust-ast-lower-stmt.cc#L28C1-L44C2
hope that helps :) feel free to keep asking questions like these!
This commit adds a base for creating AST FormatArgs nodes after expanding invocations of `format_args!()`. These nodes will then be expanded to the proper runtime function calls (to core::fmt::rt) during the AST lowering. gcc/rust/ChangeLog: * ast/rust-builtin-ast-nodes.h: New file. * ast/rust-ast-full-decls.h (class FormatArgs): Declare new class. * ast/rust-ast-collector.cc: Handle FormatArgs nodes properly. * ast/rust-ast-collector.h: Likewise. * ast/rust-ast-full.h: Likewise. * ast/rust-ast-visitor.cc: Likewise. * ast/rust-ast-visitor.h: Likewise. * ast/rust-ast.cc: Likewise. * ast/rust-ast.h: Likewise. * expand/rust-derive.h: Likewise. * hir/rust-ast-lower-base.cc: Likewise. * hir/rust-ast-lower-base.h: Likewise. * hir/rust-ast-lower-expr.cc: Likewise. * hir/rust-ast-lower-expr.h: Likewise. * resolve/rust-ast-resolve-base.cc: Likewise. * resolve/rust-ast-resolve-base.h: Likewise.
gcc/rust/ChangeLog: * expand/rust-macro-builtins.cc (format_args_maker): New function. (try_expand_many_expr): Add comment about reworking function. (MacroBuiltin::format_args_handler): Add newline parameter. * expand/rust-macro-builtins.h: Likewise.
gcc/rust/ChangeLog: * parse/rust-parse.h: New method.
gcc/rust/ChangeLog: * Make-lang.in: Do not build Rust library in release mode. * ast/rust-ast.cc: Make FormatArgs inherit from AST::Expr * ast/rust-builtin-ast-nodes.h: Improve FormatArg* nodes and helpers. * ast/rust-fmt.cc (Pieces::collect): Fix interface to match FFI function. * ast/rust-fmt.h (collect_pieces): Likewise. (struct Pieces): Add append_newline parameter. * expand/rust-macro-builtins.cc: Add proper parsing of format_args input. * hir/rust-ast-lower-base.cc: Include diagnostics header. libgrust/ChangeLog: * libformat_parser/src/lib.rs: Switch interface to use more parser parameters. * libformat_parser/src/bin.rs: Use new interface.
actually it's probably worth keeping the commit anyway because this is something we should probably be doing in the future - it just sets up scaffolding for doing so. I'll add documentation about how this isn't the case yet |
2ec9838
to
0be3896
Compare
gcc/rust/ChangeLog: * Make-lang.in: Compile the new source file. * ast/rust-ast-collector.cc (TokenCollector::visit): Error out when visiting FormatArgs nodes. * resolve/rust-ast-resolve-base.cc (ResolverBase::visit): Likewise. * hir/rust-ast-lower-expr.cc (ASTLoweringExpr::visit): Likewise. * ast/rust-ast.cc (FormatArgs::get_locus): New. * ast/rust-builtin-ast-nodes.h: Improve FormatArgs API. * ast/rust-fmt.cc (Pieces::~Pieces): Cleanup. (Pieces::Pieces): Likewise. * ast/rust-fmt.h (struct Pieces): Add pieces_vector member. * hir/rust-ast-lower-format-args.cc: New file. * hir/rust-ast-lower-format-args.h: New file.
gcc/rust/ChangeLog: * expand/rust-macro-builtins.cc (MacroBuiltin::format_args_handler): Add documentation regarding future tasks.
0be3896
to
f0be1f1
Compare
Needs #2822
Fixes #2856
This PR creates the
FormatArgs
AST node, and starts the process of building one as part of theformat_args!()
macro expansion. Having a dedicated AST node is what is being done in modern versions ofrustc
- it allows for certain optimizations like flattening and inlining of format_args arguments which reduce the generated code size.This AST node should be converted into the proper function call as part of HIR lowering.