// along with GCC; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
#include "rust-ast-collector.h"
+#include "rust-ast.h"
+#include "rust-diagnostics.h"
#include "rust-item.h"
#include "rust-keyword-values.h"
}
}
+void
+TokenCollector::visit (AST::FormatArgs &fmt)
+{
+ rust_sorry_at (0, "unimplemented format_args!() visitor");
+}
+
} // namespace AST
} // namespace Rust
void visit (SliceType &type);
void visit (InferredType &type);
void visit (BareFunctionType &type);
+
+ void visit (FormatArgs &fmt);
};
} // namespace AST
class InferredType;
struct MaybeNamedParam;
class BareFunctionType;
+
+// rust-builtin-ast-nodes.h
+class FormatArgs;
} // namespace AST
} // namespace Rust
#include "rust-stmt.h"
#include "rust-type.h"
#include "rust-macro.h"
+#include "rust-builtin-ast-nodes.h"
#endif
visit (type.get_return_type ());
}
+void
+DefaultASTVisitor::visit (AST::FormatArgs &)
+{
+ // FIXME: Do we have anything to do? any subnodes to visit? Probably, right?
+}
+
void
DefaultASTVisitor::visit (AST::VariadicParam ¶m)
{
virtual void visit (InferredType &type) = 0;
virtual void visit (BareFunctionType &type) = 0;
+ // special AST nodes for certain builtin macros such as `asm!()`
+ virtual void visit (FormatArgs &fmt) = 0;
+
// TODO: rust-cond-compilation.h visiting? not currently used
};
virtual void visit (AST::SelfParam &self) override;
virtual void visit (AST::FunctionParam ¶m) override;
virtual void visit (AST::VariadicParam ¶m) override;
+ virtual void visit (AST::FormatArgs &fmt) override;
template <typename T> void visit (T &node) { node.accept_vis (*this); }
virtual void visit (AST::MacroTranscriber &transcriber);
virtual void visit (AST::StructPatternElements &spe);
virtual void visit (AST::MaybeNamedParam ¶m);
+
void visit (AST::Attribute &attribute) {}
template <typename T> void visit_outer_attrs (T &node)
vis.visit (*this);
}
+void
+FormatArgs::accept_vis (ASTVisitor &vis)
+{
+ vis.visit (*this);
+}
+
} // namespace AST
std::ostream &
class PathExpr : public ExprWithoutBlock
{
};
+
} // namespace AST
} // namespace Rust
--- /dev/null
+// 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_AST_BUILTIN_NODES_H
+#define RUST_AST_BUILTIN_NODES_H
+
+#include "rust-system.h"
+#include "line-map.h"
+#include "optional.h"
+#include "rust-ast.h"
+#include "rust-fmt.h"
+
+namespace Rust {
+namespace AST {
+
+// Definitions, from rustc's `FormatArgs` AST struct
+// https://github.com/rust-lang/rust/blob/1be468815c/compiler/rustc_ast/src/format.rs
+//
+// format_args!("hello {abc:.xyz$}!!", abc="world");
+// └──────────────────────────────────────────────┘
+// FormatArgs
+//
+// format_args!("hello {abc:.xyz$}!!", abc="world");
+// └─────────┘
+// argument
+//
+// format_args!("hello {abc:.xyz$}!!", abc="world");
+// └───────────────────┘
+// template
+//
+// format_args!("hello {abc:.xyz$}!!", abc="world");
+// └────┘└─────────┘└┘
+// pieces
+//
+// format_args!("hello {abc:.xyz$}!!", abc="world");
+// └────┘ └┘
+// literal pieces
+//
+// format_args!("hello {abc:.xyz$}!!", abc="world");
+// └─────────┘
+// placeholder
+//
+// format_args!("hello {abc:.xyz$}!!", abc="world");
+// └─┘ └─┘
+// positions (could be names, numbers, empty, or `*`)
+
+class FormatArgumentKind
+{
+public:
+ Identifier &get_ident ()
+ {
+ rust_assert (kind == Kind::Captured || kind == Kind::Named);
+
+ return ident.value ();
+ }
+
+private:
+ enum class Kind
+ {
+ Normal,
+ Named,
+ Captured,
+ } kind;
+
+ tl::optional<Identifier> ident;
+};
+
+class FormatArgument
+{
+ FormatArgumentKind kind;
+ std::unique_ptr<Expr> expr;
+};
+
+class FormatArguments
+{
+ std::vector<FormatArgument> args;
+};
+
+// TODO: Format documentation better
+// Having a separate AST node for `format_args!()` expansion allows some
+// important optimizations which help reduce generated code a lot. For example,
+// turning `format_args!("a {} {} {}", 15, "hey", 'a')` directly into
+// `format_args!("a 15 hey a")`, since all arguments are literals. Or,
+// flattening imbricated `format_args!()` calls: `format_args!("heyo {}",
+// format_args!("result: {}", some_result))` -> `format_args!("heyo result: {}",
+// some_result)`
+// FIXME: Move to rust-macro.h
+class FormatArgs : public Visitable
+{
+public:
+ enum class Newline
+ {
+ Yes,
+ No
+ };
+
+ FormatArgs (location_t loc, Fmt::PieceSlice template_str,
+ FormatArguments arguments)
+ : loc (loc), template_str (std::move (template_str)),
+ arguments (std::move (arguments))
+ {}
+
+ void accept_vis (AST::ASTVisitor &vis);
+
+private:
+ location_t loc;
+ Fmt::PieceSlice template_str;
+ FormatArguments arguments;
+};
+
+} // namespace AST
+} // namespace Rust
+
+#endif // ! RUST_AST_BUILTIN_NODES_H
virtual void visit (SelfParam ¶m) override final{};
virtual void visit (FunctionParam ¶m) override final{};
virtual void visit (VariadicParam ¶m) override final{};
+ virtual void visit (FormatArgs ¶m) override final{};
};
} // namespace AST
#include "rust-ast-lower-type.h"
#include "rust-ast-lower-pattern.h"
#include "rust-ast-lower-extern.h"
+#include "rust-ast.h"
#include "rust-attribute-values.h"
#include "rust-item.h"
#include "rust-system.h"
ASTLoweringBase::visit (AST::SelfParam ¶m)
{}
+void
+ASTLoweringBase::visit (AST::FormatArgs &fmt)
+{}
+
HIR::Lifetime
ASTLoweringBase::lower_lifetime (AST::Lifetime &lifetime,
bool default_to_static_lifetime)
#ifndef RUST_AST_LOWER_BASE
#define RUST_AST_LOWER_BASE
+#include "rust-ast.h"
#include "rust-system.h"
#include "rust-ast-full.h"
#include "rust-ast-visitor.h"
virtual void visit (AST::VariadicParam ¶m);
virtual void visit (AST::SelfParam ¶m);
+ virtual void visit (AST::FormatArgs &fmt);
+
protected:
ASTLoweringBase ()
: mappings (Analysis::Mappings::get ()),
#include "rust-ast-lower-struct-field-expr.h"
#include "rust-ast-lower-pattern.h"
#include "rust-ast-lower-type.h"
+#include "rust-ast.h"
+#include "rust-diagnostics.h"
namespace Rust {
namespace HIR {
expr.get_locus ());
}
+void
+ASTLoweringExpr::visit (AST::FormatArgs &fmt)
+{
+ rust_sorry_at (0, "unimplemented format_args!() visitor");
+}
+
} // namespace HIR
} // namespace Rust
#define RUST_AST_LOWER_EXPR
#include "rust-ast-lower-base.h"
+#include "rust-ast.h"
namespace Rust {
namespace HIR {
void visit (AST::ClosureExprInner &expr) override;
void visit (AST::ClosureExprInnerTyped &expr) override;
+ // Extra visitor for FormatArgs nodes
+ void visit (AST::FormatArgs &fmt) override;
+
private:
ASTLoweringExpr ();
ResolverBase::visit (AST::FunctionParam &)
{}
+void
+ResolverBase::visit (AST::FormatArgs &fmt)
+{
+ rust_sorry_at (0, "unimplemented format_args!() visitor");
+}
+
} // namespace Resolver
} // namespace Rust
#define RUST_AST_RESOLVE_BASE_H
#include "rust-ast-visitor.h"
+#include "rust-ast.h"
#include "rust-name-resolver.h"
#include "rust-diagnostics.h"
#include "rust-location.h"
void visit (AST::VariadicParam ¶m);
void visit (AST::SelfParam ¶m);
+ void visit (AST::FormatArgs &fmt);
+
protected:
ResolverBase ()
: resolver (Resolver::get ()), mappings (Analysis::Mappings::get ()),