Skip to content
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

Start reusing rustc's format_args parser #2822

Merged
merged 13 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/ccpp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,8 @@ jobs:
g++-4.8 \
gcc-4.8-multilib \
g++-4.8-multilib \
dejagnu
dejagnu \
cargo

- name: Configure
run: |
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,4 @@ test.code-workspace

gcc/rust/test3-tiny/*
.clang-format.swap
libgrust/*/target/
16 changes: 13 additions & 3 deletions gcc/rust/Make-lang.in
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ GCCRS_D_OBJS = \
rust/rustspec.o \
$(END)

LIBS += -ldl -lpthread

gccrs$(exeext): $(GCCRS_D_OBJS) $(EXTRA_GCC_OBJS) libcommon-target.a $(LIBDEPS)
+$(LINKER) $(ALL_LINKERFLAGS) $(LDFLAGS) -o $@ \
$(GCCRS_D_OBJS) $(EXTRA_GCC_OBJS) libcommon-target.a \
Expand Down Expand Up @@ -100,6 +102,7 @@ GRS_OBJS = \
rust/rust-proc-macro-invoc-lexer.o \
rust/rust-macro-substitute-ctx.o \
rust/rust-macro-builtins.o \
rust/rust-fmt.o \
rust/rust-hir.o \
rust/rust-hir-map.o \
rust/rust-attributes.o \
Expand Down Expand Up @@ -208,14 +211,14 @@ RUST_ALL_OBJS = $(GRS_OBJS) $(RUST_TARGET_OBJS)

rust_OBJS = $(RUST_ALL_OBJS) rust/rustspec.o

RUST_LDFLAGS = $(LDFLAGS) -L./../libgrust/libproc_macro_internal
RUST_LIBDEPS = $(LIBDEPS) ../libgrust/libproc_macro_internal/libproc_macro_internal.a
RUST_LDFLAGS = $(LDFLAGS) -L./../libgrust/libproc_macro_internal -L./../libgrust/librustc_format_parser/
RUST_LIBDEPS = $(LIBDEPS) ../libgrust/libproc_macro_internal/libproc_macro_internal.a rust/libformat_parser.a

# The compiler itself is called crab1
crab1$(exeext): $(RUST_ALL_OBJS) attribs.o $(BACKEND) $(RUST_LIBDEPS) $(rust.prev)
@$(call LINK_PROGRESS,$(INDEX.rust),start)
+$(LLINKER) $(ALL_LINKERFLAGS) $(RUST_LDFLAGS) -o $@ \
$(RUST_ALL_OBJS) attribs.o $(BACKEND) $(LIBS) ../libgrust/libproc_macro_internal/libproc_macro_internal.a $(BACKENDLIBS)
$(RUST_ALL_OBJS) attribs.o $(BACKEND) $(LIBS) ../libgrust/libproc_macro_internal/libproc_macro_internal.a rust/libformat_parser.a $(BACKENDLIBS)
@$(call LINK_PROGRESS,$(INDEX.rust),end)

# Build hooks.
Expand Down Expand Up @@ -401,6 +404,13 @@ rust/%.o: rust/lex/%.cc
$(COMPILE) $(RUST_CXXFLAGS) $(RUST_INCLUDES) $<
$(POSTCOMPILE)

%.toml:
echo $@

rust/libformat_parser.a: $(srcdir)/../libgrust/libformat_parser/Cargo.toml $(wildcard $(srcdir)/../libgrust/libformat_parser/src/*.rs)
cargo build --manifest-path $(srcdir)/../libgrust/libformat_parser/Cargo.toml --release # FIXME: Not always release, right?
cp $(srcdir)/../libgrust/libformat_parser/target/release/liblibformat_parser.a $@

# build all rust/parse files in rust folder, add cross-folder includes
rust/%.o: rust/parse/%.cc
$(COMPILE) $(RUST_CXXFLAGS) $(RUST_INCLUDES) $<
Expand Down
43 changes: 43 additions & 0 deletions gcc/rust/ast/rust-fmt.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Copyright (C) 2020-2023 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/>.

#include "rust-fmt.h"
#include "rust-diagnostics.h"

namespace Rust {
namespace Fmt {

Pieces
Pieces::collect (std::string &&to_parse)
{
auto piece_slice = collect_pieces (to_parse.c_str ());

rust_debug ("[ARTHUR] %p, %lu", (const void *) piece_slice.base_ptr,
piece_slice.len);

// this performs multiple copies, can we avoid them maybe?
// auto pieces = std::vector<Piece> (piece_slice.base_ptr,
// piece_slice.base_ptr + piece_slice.len);

return Pieces (piece_slice, std::move (to_parse));
}

Pieces::~Pieces () { destroy_pieces (slice); }

} // namespace Fmt
} // namespace Rust
269 changes: 269 additions & 0 deletions gcc/rust/ast/rust-fmt.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@
// Copyright (C) 2023-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_FMT_H
#define RUST_FMT_H

#include "rust-system.h"

// FIXME: How to encode Option?

namespace Rust {
namespace Fmt {

struct RustHamster
{
// hehe
};

/// Enum of alignments which are supported.
enum class Alignment
{
/// The value will be aligned to the left.
AlignLeft,
/// The value will be aligned to the right.
AlignRight,
/// The value will be aligned in the center.
AlignCenter,
/// The value will take on a default alignment.
AlignUnknown,
};

/// Enum for the debug hex flags.
enum class DebugHex
{
/// The `x` flag in `{:x?}`.
Lower,
/// The `X` flag in `{:X?}`.
Upper,
};

/// Enum for the sign flags.
enum class Sign
{
/// The `+` flag.
Plus,
/// The `-` flag.
Minus,
};

/// Enum describing where an argument for a format can be located.
struct Position
{
enum class Tag
{
/// The argument is implied to be located at an index
ArgumentImplicitlyIs,
/// The argument is located at a specific index given in the format,
ArgumentIs,
/// The argument has a name.
ArgumentNamed,
};

struct ArgumentImplicitlyIs_Body
{
size_t _0;
};

struct ArgumentIs_Body
{
size_t _0;
};

struct ArgumentNamed_Body
{
RustHamster _0;
};

Tag tag;
union
{
ArgumentImplicitlyIs_Body argument_implicitly_is;
ArgumentIs_Body argument_is;
ArgumentNamed_Body argument_named;
};
};

/// Range inside of a `Span` used for diagnostics when we only have access to
/// relative positions.
struct InnerSpan
{
size_t start;
size_t end;
};

/// A count is used for the precision and width parameters of an integer, and
/// can reference either an argument or a literal integer.
struct Count
{
enum class Tag
{
/// The count is specified explicitly.
CountIs,
/// The count is specified by the argument with the given name.
CountIsName,
/// The count is specified by the argument at the given index.
CountIsParam,
/// The count is specified by a star (like in `{:.*}`) that refers to the
/// argument at the given index.
CountIsStar,
/// The count is implied and cannot be explicitly specified.
CountImplied,
};

struct CountIs_Body
{
size_t _0;
};

struct CountIsName_Body
{
RustHamster _0;
InnerSpan _1;
};

struct CountIsParam_Body
{
size_t _0;
};

struct CountIsStar_Body
{
size_t _0;
};

Tag tag;
union
{
CountIs_Body count_is;
CountIsName_Body count_is_name;
CountIsParam_Body count_is_param;
CountIsStar_Body count_is_star;
};
};

/// Specification for the formatting of an argument in the format string.
struct FormatSpec
{
/// Optionally specified character to fill alignment with.
const uint32_t *fill;
/// Span of the optionally specified fill character.
const InnerSpan *fill_span;
/// Optionally specified alignment.
Alignment align;
/// The `+` or `-` flag.
const Sign *sign;
/// The `#` flag.
bool alternate;
/// The `0` flag.
bool zero_pad;
/// The `x` or `X` flag. (Only for `Debug`.)
const DebugHex *debug_hex;
/// The integer precision to use.
Count precision;
/// The span of the precision formatting flag (for diagnostics).
const InnerSpan *precision_span;
/// The string width requested for the resulting format.
Count width;
/// The span of the width formatting flag (for diagnostics).
const InnerSpan *width_span;
/// The descriptor string representing the name of the format desired for
/// this argument, this can be empty or any number of characters, although
/// it is required to be one word.
RustHamster ty;
/// The span of the descriptor string (for diagnostics).
const InnerSpan *ty_span;
};

/// Representation of an argument specification.
struct Argument
{
/// Where to find this argument
Position position;
/// The span of the position indicator. Includes any whitespace in implicit
/// positions (`{ }`).
InnerSpan position_span;
/// How to format the argument
FormatSpec format;
};

/// A piece is a portion of the format string which represents the next part
/// to emit. These are emitted as a stream by the `Parser` class.
struct Piece
{
enum class Tag
{
/// A literal string which should directly be emitted
String,
/// This describes that formatting should process the next argument (as
/// specified inside) for emission.
NextArgument,
};

struct String_Body
{
RustHamster _0;
};

struct NextArgument_Body
{
const Argument *_0;
};

Tag tag;
union
{
String_Body string;
NextArgument_Body next_argument;
};
};

struct PieceSlice
{
const Piece *base_ptr;
size_t len;
size_t cap;
};

extern "C" {

PieceSlice
collect_pieces (const char *input);

void destroy_pieces (PieceSlice);

} // extern "C"

struct Pieces
{
static Pieces collect (std::string &&to_parse);
~Pieces ();

private:
Pieces (PieceSlice slice, std::string &&to_parse)
: slice (slice), to_parse (std::move (to_parse))
{}

PieceSlice slice;
std::string to_parse;
};

} // namespace Fmt
} // namespace Rust

#endif // !RUST_FMT_H
Loading
Loading