new file mode 100644
@@ -0,0 +1,244 @@
+// rust-diagnostics.cc -- GCC implementation of rust diagnostics interface.
+// Copyright (C) 2016-2022 Free Software Foundation, Inc.
+// Contributed by Than McIntosh, Google.
+
+// 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-system.h"
+#include "rust-diagnostics.h"
+
+static std::string
+mformat_value ()
+{
+ return std::string (xstrerror (errno));
+}
+
+// Rewrite a format string to expand any extensions not
+// supported by sprintf(). See comments in rust-diagnostics.h
+// for list of supported format specifiers.
+
+static std::string
+expand_format (const char *fmt)
+{
+ std::stringstream ss;
+ for (const char *c = fmt; *c; ++c)
+ {
+ if (*c != '%')
+ {
+ ss << *c;
+ continue;
+ }
+ c++;
+ switch (*c)
+ {
+ case '\0': {
+ // malformed format string
+ rust_unreachable ();
+ }
+ case '%': {
+ ss << "%";
+ break;
+ }
+ case 'm': {
+ ss << mformat_value ();
+ break;
+ }
+ case '<': {
+ ss << rust_open_quote ();
+ break;
+ }
+ case '>': {
+ ss << rust_close_quote ();
+ break;
+ }
+ case 'q': {
+ ss << rust_open_quote ();
+ c++;
+ if (*c == 'm')
+ {
+ ss << mformat_value ();
+ }
+ else
+ {
+ ss << "%" << *c;
+ }
+ ss << rust_close_quote ();
+ break;
+ }
+ default: {
+ ss << "%" << *c;
+ }
+ }
+ }
+ return ss.str ();
+}
+
+// Expand message format specifiers, using a combination of
+// expand_format above to handle extensions (ex: %m, %q) and vasprintf()
+// to handle regular printf-style formatting. A pragma is being used here to
+// suppress this warning:
+//
+// warning: function ‘std::__cxx11::string expand_message(const char*,
+// __va_list_tag*)’ might be a candidate for ‘gnu_printf’ format attribute
+// [-Wsuggest-attribute=format]
+//
+// What appears to be happening here is that the checker is deciding that
+// because of the call to vasprintf() (which has attribute gnu_printf), the
+// calling function must need to have attribute gnu_printf as well, even
+// though there is already an attribute declaration for it.
+
+static std::string
+expand_message (const char *fmt, va_list ap) RUST_ATTRIBUTE_GCC_DIAG (1, 0);
+
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wsuggest-attribute=format"
+
+static std::string
+expand_message (const char *fmt, va_list ap)
+{
+ char *mbuf = 0;
+ std::string expanded_fmt = expand_format (fmt);
+ int nwr = vasprintf (&mbuf, expanded_fmt.c_str (), ap);
+ if (nwr == -1)
+ {
+ // memory allocation failed
+ rust_be_error_at (Linemap::unknown_location (),
+ "memory allocation failed in vasprintf");
+ rust_assert (0);
+ }
+ std::string rval = std::string (mbuf);
+ free (mbuf);
+ return rval;
+}
+
+#pragma GCC diagnostic pop
+
+static const char *cached_open_quote = NULL;
+static const char *cached_close_quote = NULL;
+
+const char *
+rust_open_quote ()
+{
+ if (cached_open_quote == NULL)
+ rust_be_get_quotechars (&cached_open_quote, &cached_close_quote);
+ return cached_open_quote;
+}
+
+const char *
+rust_close_quote ()
+{
+ if (cached_close_quote == NULL)
+ rust_be_get_quotechars (&cached_open_quote, &cached_close_quote);
+ return cached_close_quote;
+}
+
+void
+rust_internal_error_at (const Location location, const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start (ap, fmt);
+ rust_be_internal_error_at (location, expand_message (fmt, ap));
+ va_end (ap);
+}
+
+void
+rust_error_at (const Location location, const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start (ap, fmt);
+ rust_be_error_at (location, expand_message (fmt, ap));
+ va_end (ap);
+}
+
+void
+rust_warning_at (const Location location, int opt, const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start (ap, fmt);
+ rust_be_warning_at (location, opt, expand_message (fmt, ap));
+ va_end (ap);
+}
+
+void
+rust_fatal_error (const Location location, const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start (ap, fmt);
+ rust_be_fatal_error (location, expand_message (fmt, ap));
+ va_end (ap);
+}
+
+void
+rust_inform (const Location location, const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start (ap, fmt);
+ rust_be_inform (location, expand_message (fmt, ap));
+ va_end (ap);
+}
+
+// Rich Locations
+void
+rust_error_at (const RichLocation &location, const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start (ap, fmt);
+ rust_be_error_at (location, expand_message (fmt, ap));
+ va_end (ap);
+}
+
+void
+rust_debug_loc (const Location location, const char *fmt, ...)
+{
+ if (!rust_be_debug_p ())
+ return;
+
+ va_list ap;
+
+ va_start (ap, fmt);
+ char *mbuf = NULL;
+ int nwr = vasprintf (&mbuf, fmt, ap);
+ va_end (ap);
+ if (nwr == -1)
+ {
+ rust_be_error_at (Linemap::unknown_location (),
+ "memory allocation failed in vasprintf");
+ rust_assert (0);
+ }
+ std::string rval = std::string (mbuf);
+ free (mbuf);
+ rust_be_inform (location, rval);
+}
+
+namespace Rust {
+Error::Error (const Location location, const char *fmt, ...) : locus (location)
+{
+ va_list ap;
+
+ va_start (ap, fmt);
+ message = expand_message (fmt, ap);
+ va_end (ap);
+
+ message.shrink_to_fit ();
+}
+} // namespace Rust
new file mode 100644
@@ -0,0 +1,154 @@
+// Copyright (C) 2020-2022 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/>.
+
+// rust-diagnostics.h -- interface to diagnostic reporting -*- C++ -*-
+
+#ifndef RUST_DIAGNOSTICS_H
+#define RUST_DIAGNOSTICS_H
+
+#include "rust-linemap.h"
+
+#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1)
+#define RUST_ATTRIBUTE_GCC_DIAG(m, n) \
+ __attribute__ ((__format__ (__gcc_tdiag__, m, n))) \
+ __attribute__ ((__nonnull__ (m)))
+#else
+#define RUST_ATTRIBUTE_GCC_DIAG(m, n)
+#endif
+
+// These declarations define the interface through which the frontend
+// reports errors and warnings. These functions accept printf-like
+// format specifiers (e.g. %d, %f, %s, etc), with the following additional
+// extensions:
+//
+// 1. 'q' qualifier may be applied to a specifier to add quoting, e.g.
+// %qd produces a quoted decimal output, %qs a quoted string output.
+// [This extension is supported only with single-character format
+// specifiers].
+//
+// 2. %m specifier outputs value of "strerror(errno)" at time of call.
+//
+// 3. %< outputs an opening quote, %> a closing quote.
+//
+// All other format specifiers are as defined by 'sprintf'. The final resulting
+// message is then sent to the back end via rust_be_error_at/rust_be_warning_at.
+
+// clang-format off
+// simple location
+extern void
+rust_internal_error_at (const Location, const char *fmt, ...)
+ RUST_ATTRIBUTE_GCC_DIAG (2, 3)
+ RUST_ATTRIBUTE_NORETURN;
+extern void
+rust_error_at (const Location, const char *fmt, ...)
+ RUST_ATTRIBUTE_GCC_DIAG (2, 3);
+extern void
+rust_warning_at (const Location, int opt, const char *fmt, ...)
+ RUST_ATTRIBUTE_GCC_DIAG (3, 4);
+extern void
+rust_fatal_error (const Location, const char *fmt, ...)
+ RUST_ATTRIBUTE_GCC_DIAG (2, 3)
+ RUST_ATTRIBUTE_NORETURN;
+extern void
+rust_inform (const Location, const char *fmt, ...)
+ RUST_ATTRIBUTE_GCC_DIAG (2, 3);
+
+// rich locations
+extern void
+rust_error_at (const RichLocation &, const char *fmt, ...)
+ RUST_ATTRIBUTE_GCC_DIAG (2, 3);
+// clang-format on
+
+// These interfaces provide a way for the front end to ask for
+// the open/close quote characters it should use when formatting
+// diagnostics (warnings, errors).
+extern const char *
+rust_open_quote ();
+extern const char *
+rust_close_quote ();
+
+// These interfaces are used by utilities above to pass warnings and
+// errors (once format specifiers have been expanded) to the back end,
+// and to determine quoting style. Avoid calling these routines directly;
+// instead use the equivalent routines above. The back end is required to
+// implement these routines.
+
+// clang-format off
+extern void
+rust_be_internal_error_at (const Location, const std::string &errmsg)
+ RUST_ATTRIBUTE_NORETURN;
+extern void
+rust_be_error_at (const Location, const std::string &errmsg);
+extern void
+rust_be_error_at (const RichLocation &, const std::string &errmsg);
+extern void
+rust_be_warning_at (const Location, int opt, const std::string &warningmsg);
+extern void
+rust_be_fatal_error (const Location, const std::string &errmsg)
+ RUST_ATTRIBUTE_NORETURN;
+extern void
+rust_be_inform (const Location, const std::string &infomsg);
+extern void
+rust_be_get_quotechars (const char **open_quote, const char **close_quote);
+extern bool
+rust_be_debug_p (void);
+// clang-format on
+
+namespace Rust {
+/* A structure used to represent an error. Useful for enabling
+ * errors to be ignored, e.g. if backtracking. */
+struct Error
+{
+ Location locus;
+ std::string message;
+ // TODO: store more stuff? e.g. node id?
+
+ Error (Location locus, std::string message)
+ : locus (locus), message (std::move (message))
+ {
+ message.shrink_to_fit ();
+ }
+
+ // TODO: the attribute part might be incorrect
+ Error (Location locus, const char *fmt,
+ ...) /*RUST_ATTRIBUTE_GCC_DIAG (2, 3)*/ RUST_ATTRIBUTE_GCC_DIAG (3, 4);
+
+ // Irreversibly emits the error as an error.
+ void emit_error () const { rust_error_at (locus, "%s", message.c_str ()); }
+
+ // Irreversibly emits the error as a fatal error.
+ void emit_fatal_error () const
+ {
+ rust_fatal_error (locus, "%s", message.c_str ());
+ }
+};
+} // namespace Rust
+
+// rust_debug uses normal printf formatting, not GCC diagnostic formatting.
+#define rust_debug(...) rust_debug_loc (Location (), __VA_ARGS__)
+
+// rust_sorry_at wraps GCC diagnostic "sorry_at" to accept "Location" instead of
+// "location_t"
+#define rust_sorry_at(location, ...) \
+ sorry_at (location.gcc_location (), __VA_ARGS__)
+
+void
+rust_debug_loc (const Location location, const char *fmt,
+ ...) ATTRIBUTE_PRINTF_2;
+
+#endif // !defined(RUST_DIAGNOSTICS_H)
new file mode 100644
@@ -0,0 +1,84 @@
+// Copyright (C) 2020-2022 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/>.
+
+// rust-gcc-diagnostics.cc -- GCC implementation of rust diagnostics interface.
+
+#include "rust-system.h"
+#include "rust-diagnostics.h"
+
+#include "options.h"
+
+void
+rust_be_internal_error_at (const Location location, const std::string &errmsg)
+{
+ std::string loc_str = Linemap::location_to_string (location);
+ if (loc_str.empty ())
+ internal_error ("%s", errmsg.c_str ());
+ else
+ internal_error ("at %s, %s", loc_str.c_str (), errmsg.c_str ());
+}
+
+void
+rust_be_error_at (const Location location, const std::string &errmsg)
+{
+ location_t gcc_loc = location.gcc_location ();
+ error_at (gcc_loc, "%s", errmsg.c_str ());
+}
+
+void
+rust_be_warning_at (const Location location, int opt,
+ const std::string &warningmsg)
+{
+ location_t gcc_loc = location.gcc_location ();
+ warning_at (gcc_loc, opt, "%s", warningmsg.c_str ());
+}
+
+void
+rust_be_fatal_error (const Location location, const std::string &fatalmsg)
+{
+ location_t gcc_loc = location.gcc_location ();
+ fatal_error (gcc_loc, "%s", fatalmsg.c_str ());
+}
+
+void
+rust_be_inform (const Location location, const std::string &infomsg)
+{
+ location_t gcc_loc = location.gcc_location ();
+ inform (gcc_loc, "%s", infomsg.c_str ());
+}
+
+void
+rust_be_error_at (const RichLocation &location, const std::string &errmsg)
+{
+ /* TODO: 'error_at' would like a non-'const' 'rich_location *'. */
+ rich_location &gcc_loc = const_cast<rich_location &> (location.get ());
+ error_at (&gcc_loc, "%s", errmsg.c_str ());
+}
+
+void
+rust_be_get_quotechars (const char **open_qu, const char **close_qu)
+{
+ *open_qu = open_quote;
+ *close_qu = close_quote;
+}
+
+bool
+rust_be_debug_p (void)
+{
+ return !!flag_rust_debug;
+}
new file mode 100644
@@ -0,0 +1,229 @@
+// Copyright (C) 2020-2022 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/>.
+
+// rust-linemap.cc -- GCC implementation of Linemap.
+
+#include "rust-linemap.h"
+
+// This class implements the Linemap interface defined by the
+// frontend.
+
+class Gcc_linemap : public Linemap
+{
+public:
+ Gcc_linemap () : Linemap (), in_file_ (false) {}
+
+ void start_file (const char *file_name, unsigned int line_begin);
+
+ void start_line (unsigned int line_number, unsigned int line_size);
+
+ Location get_location (unsigned int column);
+
+ void stop ();
+
+ std::string to_string (Location);
+
+ std::string location_file (Location);
+
+ int location_line (Location);
+
+ int location_column (Location);
+
+protected:
+ Location get_predeclared_location ();
+
+ Location get_unknown_location ();
+
+ bool is_predeclared (Location);
+
+ bool is_unknown (Location);
+
+private:
+ // Whether we are currently reading a file.
+ bool in_file_;
+};
+
+Linemap *Linemap::instance_ = NULL;
+
+// Start getting locations from a new file.
+
+void
+Gcc_linemap::start_file (const char *file_name, unsigned line_begin)
+{
+ if (this->in_file_)
+ linemap_add (line_table, LC_LEAVE, 0, NULL, 0);
+ linemap_add (line_table, LC_ENTER, 0, file_name, line_begin);
+ this->in_file_ = true;
+}
+
+// Stringify a location
+
+std::string
+Gcc_linemap::to_string (Location location)
+{
+ const line_map_ordinary *lmo;
+ location_t resolved_location;
+
+ // Screen out unknown and predeclared locations; produce output
+ // only for simple file:line locations.
+ resolved_location
+ = linemap_resolve_location (line_table, location.gcc_location (),
+ LRK_SPELLING_LOCATION, &lmo);
+ if (lmo == NULL || resolved_location < RESERVED_LOCATION_COUNT)
+ return "";
+ const char *path = LINEMAP_FILE (lmo);
+ if (!path)
+ return "";
+
+ // Strip the source file down to the base file, to reduce clutter.
+ std::stringstream ss;
+ ss << lbasename (path) << ":" << SOURCE_LINE (lmo, location.gcc_location ())
+ << ":" << SOURCE_COLUMN (lmo, location.gcc_location ());
+ return ss.str ();
+}
+
+// Return the file name for a given location.
+
+std::string
+Gcc_linemap::location_file (Location loc)
+{
+ return LOCATION_FILE (loc.gcc_location ());
+}
+
+// Return the line number for a given location.
+
+int
+Gcc_linemap::location_line (Location loc)
+{
+ return LOCATION_LINE (loc.gcc_location ());
+}
+
+// Return the column number for a given location.
+int
+Gcc_linemap::location_column (Location loc)
+{
+ return LOCATION_COLUMN (loc.gcc_location ());
+}
+
+// Stop getting locations.
+
+void
+Gcc_linemap::stop ()
+{
+ linemap_add (line_table, LC_LEAVE, 0, NULL, 0);
+ this->in_file_ = false;
+}
+
+// Start a new line.
+
+void
+Gcc_linemap::start_line (unsigned lineno, unsigned linesize)
+{
+ linemap_line_start (line_table, lineno, linesize);
+}
+
+// Get a location.
+
+Location
+Gcc_linemap::get_location (unsigned column)
+{
+ return Location (linemap_position_for_column (line_table, column));
+}
+
+// Get the unknown location.
+
+Location
+Gcc_linemap::get_unknown_location ()
+{
+ return Location (UNKNOWN_LOCATION);
+}
+
+// Get the predeclared location.
+
+Location
+Gcc_linemap::get_predeclared_location ()
+{
+ return Location (BUILTINS_LOCATION);
+}
+
+// Return whether a location is the predeclared location.
+
+bool
+Gcc_linemap::is_predeclared (Location loc)
+{
+ return loc.gcc_location () == BUILTINS_LOCATION;
+}
+
+// Return whether a location is the unknown location.
+
+bool
+Gcc_linemap::is_unknown (Location loc)
+{
+ return loc.gcc_location () == UNKNOWN_LOCATION;
+}
+
+// Return the Linemap to use for the gcc backend.
+
+Linemap *
+rust_get_linemap ()
+{
+ return new Gcc_linemap;
+}
+
+RichLocation::RichLocation (Location root)
+ : gcc_rich_loc (line_table, root.gcc_location ())
+{
+ /*rich_location (line_maps *set, location_t loc,
+ const range_label *label = NULL);*/
+}
+
+RichLocation::~RichLocation () {}
+
+void
+RichLocation::add_range (Location loc)
+{
+ gcc_rich_loc.add_range (loc.gcc_location ());
+}
+
+void
+RichLocation::add_fixit_insert_before (const std::string &new_parent)
+{
+ gcc_rich_loc.add_fixit_insert_before (new_parent.c_str ());
+}
+
+void
+RichLocation::add_fixit_insert_before (Location where,
+ const std::string &new_parent)
+{
+ gcc_rich_loc.add_fixit_insert_before (where.gcc_location (),
+ new_parent.c_str ());
+}
+
+void
+RichLocation::add_fixit_insert_after (const std::string &new_parent)
+{
+ gcc_rich_loc.add_fixit_insert_after (new_parent.c_str ());
+}
+
+void
+RichLocation::add_fixit_insert_after (Location where,
+ const std::string &new_parent)
+{
+ gcc_rich_loc.add_fixit_insert_after (where.gcc_location (),
+ new_parent.c_str ());
+}
new file mode 100644
@@ -0,0 +1,163 @@
+// rust-linemap.h -- interface to location tracking -*- C++ -*-
+
+// Copyright 2011 The Go Authors. All rights reserved.
+// Copyright (C) 2020-2022 Free Software Foundation, Inc.
+
+// Use of this source code is governed by a BSD-style
+// license that can be found in the '../go/gofrontend/LICENSE' file.
+
+#ifndef RUST_LINEMAP_H
+#define RUST_LINEMAP_H
+
+#include "rust-system.h"
+
+// The backend must define a type named Location which holds
+// information about a location in a source file. The only thing the
+// frontend does with instances of Location is pass them back to the
+// backend interface. The Location type must be assignable, and it
+// must be comparable: i.e., it must support operator= and operator<.
+// The type is normally passed by value rather than by reference, and
+// it should support that efficiently. The type should be defined in
+// "rust-location.h".
+#include "rust-location.h"
+
+// The Linemap class is a pure abstract interface, plus some static
+// convenience functions. The backend must implement the interface.
+
+/* TODO: probably better to replace linemap implementation as pure abstract
+ * interface with some sort of compile-time switch (macros or maybe templates if
+ * doable without too much extra annoyance) as to the definition of the methods
+ * or whatever. This is to improve performance, as virtual function calls would
+ * otherwise have to be made in tight loops like in the lexer. */
+
+class Linemap
+{
+public:
+ Linemap ()
+ {
+ // Only one instance of Linemap is allowed to exist.
+ rust_assert (Linemap::instance_ == NULL);
+ Linemap::instance_ = this;
+ }
+
+ virtual ~Linemap () { Linemap::instance_ = NULL; }
+
+ // Subsequent Location values will come from the file named
+ // FILE_NAME, starting at LINE_BEGIN. Normally LINE_BEGIN will be
+ // 0, but it will be non-zero if the Rust source has a //line comment.
+ virtual void start_file (const char *file_name, unsigned int line_begin) = 0;
+
+ // Subsequent Location values will come from the line LINE_NUMBER,
+ // in the current file. LINE_SIZE is the size of the line in bytes.
+ // This will normally be called for every line in a source file.
+ virtual void start_line (unsigned int line_number, unsigned int line_size)
+ = 0;
+
+ // Get a Location representing column position COLUMN on the current
+ // line in the current file.
+ virtual Location get_location (unsigned int column) = 0;
+
+ // Stop generating Location values. This will be called after all
+ // input files have been read, in case any cleanup is required.
+ virtual void stop () = 0;
+
+ // Produce a human-readable description of a Location, e.g.
+ // "foo.rust:10". Returns an empty string for predeclared, builtin or
+ // unknown locations.
+ virtual std::string to_string (Location) = 0;
+
+ // Return the file name for a given location.
+ virtual std::string location_file (Location) = 0;
+
+ // Return the line number for a given location.
+ virtual int location_line (Location) = 0;
+
+ // Return the column number for a given location.
+ virtual int location_column (Location) = 0;
+
+protected:
+ // Return a special Location used for predeclared identifiers. This
+ // Location should be different from that for any actual source
+ // file. This location will be used for various different types,
+ // functions, and objects created by the frontend.
+ virtual Location get_predeclared_location () = 0;
+
+ // Return a special Location which indicates that no actual location
+ // is known. This is used for undefined objects and for errors.
+ virtual Location get_unknown_location () = 0;
+
+ // Return whether the argument is the Location returned by
+ // get_predeclared_location.
+ virtual bool is_predeclared (Location) = 0;
+
+ // Return whether the argument is the Location returned by
+ // get_unknown_location.
+ virtual bool is_unknown (Location) = 0;
+
+ // The single existing instance of Linemap.
+ static Linemap *instance_;
+
+public:
+ // Following are convenience static functions, which allow us to
+ // access some virtual functions without explicitly passing around
+ // an instance of Linemap.
+
+ // Return the special Location used for predeclared identifiers.
+ static Location predeclared_location ()
+ {
+ rust_assert (Linemap::instance_ != NULL);
+ return Linemap::instance_->get_predeclared_location ();
+ }
+
+ // Return the special Location used when no location is known.
+ static Location unknown_location ()
+ {
+ rust_assert (Linemap::instance_ != NULL);
+ return Linemap::instance_->get_unknown_location ();
+ }
+
+ // Return whether the argument is the special location used for
+ // predeclared identifiers.
+ static bool is_predeclared_location (Location loc)
+ {
+ rust_assert (Linemap::instance_ != NULL);
+ return Linemap::instance_->is_predeclared (loc);
+ }
+
+ // Return whether the argument is the special location used when no
+ // location is known.
+ static bool is_unknown_location (Location loc)
+ {
+ rust_assert (Linemap::instance_ != NULL);
+ return Linemap::instance_->is_unknown (loc);
+ }
+
+ // Produce a human-readable description of a Location.
+ static std::string location_to_string (Location loc)
+ {
+ rust_assert (Linemap::instance_ != NULL);
+ return Linemap::instance_->to_string (loc);
+ }
+
+ // Return the file name of a location.
+ static std::string location_to_file (Location loc)
+ {
+ rust_assert (Linemap::instance_ != NULL);
+ return Linemap::instance_->location_file (loc);
+ }
+
+ // Return line number of a location.
+ static int location_to_line (Location loc)
+ {
+ rust_assert (Linemap::instance_ != NULL);
+ return Linemap::instance_->location_line (loc);
+ }
+
+ static int location_to_column (Location loc)
+ {
+ rust_assert (Linemap::instance_ != NULL);
+ return Linemap::instance_->location_column (loc);
+ }
+};
+
+#endif // !defined(RUST_LINEMAP_H)
new file mode 100644
@@ -0,0 +1,105 @@
+// Copyright (C) 2020-2022 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/>.
+
+// rust-location.h -- GCC specific Location declaration. -*- C++ -*-
+
+#ifndef RUST_LOCATION_H
+#define RUST_LOCATION_H
+
+#include "rust-system.h"
+
+// A location in an input source file.
+
+class Location
+{
+public:
+ Location () : gcc_loc_ (UNKNOWN_LOCATION) {}
+
+ explicit Location (location_t loc) : gcc_loc_ (loc) {}
+
+ location_t gcc_location () const { return gcc_loc_; }
+
+ Location operator+= (location_t rhs)
+ {
+ gcc_loc_ += rhs;
+ return *this;
+ }
+
+ Location operator-= (location_t rhs)
+ {
+ gcc_loc_ -= rhs;
+ return *this;
+ }
+
+ bool operator== (location_t rhs) { return rhs == gcc_loc_; }
+
+private:
+ location_t gcc_loc_;
+};
+
+// The Rust frontend requires the ability to compare Locations.
+
+inline bool
+operator< (Location loca, Location locb)
+{
+ return loca.gcc_location () < locb.gcc_location ();
+}
+
+inline bool
+operator== (Location loca, Location locb)
+{
+ return loca.gcc_location () == locb.gcc_location ();
+}
+
+inline Location
+operator+ (Location lhs, location_t rhs)
+{
+ lhs += rhs;
+ return lhs;
+}
+
+inline Location
+operator- (Location lhs, location_t rhs)
+{
+ lhs -= rhs;
+ return lhs;
+}
+
+class RichLocation
+{
+public:
+ RichLocation (Location root);
+ ~RichLocation ();
+
+ void add_range (Location loc);
+
+ void add_fixit_insert_before (const std::string &new_parent);
+
+ void add_fixit_insert_before (Location where, const std::string &new_parent);
+
+ void add_fixit_insert_after (const std::string &new_parent);
+
+ void add_fixit_insert_after (Location where, const std::string &new_parent);
+
+ const rich_location &get () const { return gcc_rich_loc; }
+
+private:
+ rich_location gcc_rich_loc;
+};
+
+#endif // !defined(RUST_LOCATION_H)
new file mode 100644
@@ -0,0 +1,86 @@
+// rust-system.h -- Rust frontend inclusion of gcc header files -*- C++ -*-
+// Copyright (C) 2009-2022 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_SYSTEM_H
+#define RUST_SYSTEM_H
+
+#define INCLUDE_ALGORITHM
+#include "config.h"
+
+/* Define this so that inttypes.h defines the PRI?64 macros even
+ when compiling with a C++ compiler. Define it here so in the
+ event inttypes.h gets pulled in by another header it is already
+ defined. */
+#define __STDC_FORMAT_MACROS
+
+// These must be included before the #poison declarations in system.h.
+
+#include <string>
+#include <list>
+#include <map>
+#include <set>
+#include <vector>
+#include <sstream>
+#include <string>
+#include <deque>
+#include <functional>
+#include <memory>
+#include <utility>
+#include <fstream>
+
+// Rust frontend requires C++11 minimum, so will have unordered_map and set
+#include <unordered_map>
+#include <unordered_set>
+
+/* We don't really need iostream, but some versions of gmp.h include
+ * it when compiled with C++, which means that we need to include it
+ * before the macro magic of safe-ctype.h, which is included by
+ * system.h. */
+#include <iostream>
+
+#include "system.h"
+#include "ansidecl.h"
+#include "coretypes.h"
+
+#include "diagnostic-core.h" /* For error_at and friends. */
+#include "intl.h" /* For _(). */
+
+#define RUST_ATTRIBUTE_NORETURN ATTRIBUTE_NORETURN
+
+// File separator to use based on whether or not the OS we're working with is
+// DOS-based
+#if defined(HAVE_DOS_BASED_FILE_SYSTEM)
+constexpr static const char *file_separator = "\\";
+#else
+constexpr static const char *file_separator = "/";
+#endif /* HAVE_DOS_BASED_FILE_SYSTEM */
+
+// When using gcc, rust_assert is just gcc_assert.
+#define rust_assert(EXPR) gcc_assert (EXPR)
+
+// When using gcc, rust_unreachable is just gcc_unreachable.
+#define rust_unreachable() gcc_unreachable ()
+
+extern void
+rust_preserve_from_gc (tree t);
+
+extern const char *
+rust_localize_identifier (const char *ident);
+
+#endif // !defined(RUST_SYSTEM_H)