public inbox for gcc-rust@gcc.gnu.org
 help / color / mirror / Atom feed
From: arthur.cohen@embecosm.com
To: gcc-patches@gcc.gnu.org
Cc: gcc-rust@gcc.gnu.org, richard.guenther@gmail.com,
	jakub@redhat.com, Philip Herron <philip.herron@embecosm.com>
Subject: [PATCH Rust front-end v4 39/46] gccrs: These are wrappers ported from reusing gccgo
Date: Tue,  6 Dec 2022 11:14:17 +0100	[thread overview]
Message-ID: <20221206101417.778807-40-arthur.cohen@embecosm.com> (raw)
In-Reply-To: <20221206101417.778807-1-arthur.cohen@embecosm.com>

From: Philip Herron <philip.herron@embecosm.com>

The wrappers over linemap and location will eventually disappear here but
served as a useful starting point for us. We have wrappers over the
diagnostics system which we might be able to get rid of as well.
---
 gcc/rust/rust-diagnostics.cc     | 244 +++++++++++++++++++++++++++++++
 gcc/rust/rust-diagnostics.h      | 154 +++++++++++++++++++
 gcc/rust/rust-gcc-diagnostics.cc |  84 +++++++++++
 gcc/rust/rust-linemap.cc         | 229 +++++++++++++++++++++++++++++
 gcc/rust/rust-linemap.h          | 163 +++++++++++++++++++++
 gcc/rust/rust-location.h         | 105 +++++++++++++
 gcc/rust/rust-system.h           |  86 +++++++++++
 7 files changed, 1065 insertions(+)
 create mode 100644 gcc/rust/rust-diagnostics.cc
 create mode 100644 gcc/rust/rust-diagnostics.h
 create mode 100644 gcc/rust/rust-gcc-diagnostics.cc
 create mode 100644 gcc/rust/rust-linemap.cc
 create mode 100644 gcc/rust/rust-linemap.h
 create mode 100644 gcc/rust/rust-location.h
 create mode 100644 gcc/rust/rust-system.h

diff --git a/gcc/rust/rust-diagnostics.cc b/gcc/rust/rust-diagnostics.cc
new file mode 100644
index 00000000000..c2d3e4ee8be
--- /dev/null
+++ b/gcc/rust/rust-diagnostics.cc
@@ -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
diff --git a/gcc/rust/rust-diagnostics.h b/gcc/rust/rust-diagnostics.h
new file mode 100644
index 00000000000..93bd1b3237b
--- /dev/null
+++ b/gcc/rust/rust-diagnostics.h
@@ -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)
diff --git a/gcc/rust/rust-gcc-diagnostics.cc b/gcc/rust/rust-gcc-diagnostics.cc
new file mode 100644
index 00000000000..db07372dfb5
--- /dev/null
+++ b/gcc/rust/rust-gcc-diagnostics.cc
@@ -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;
+}
diff --git a/gcc/rust/rust-linemap.cc b/gcc/rust/rust-linemap.cc
new file mode 100644
index 00000000000..b32a965a4aa
--- /dev/null
+++ b/gcc/rust/rust-linemap.cc
@@ -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 ());
+}
diff --git a/gcc/rust/rust-linemap.h b/gcc/rust/rust-linemap.h
new file mode 100644
index 00000000000..0ba95f87575
--- /dev/null
+++ b/gcc/rust/rust-linemap.h
@@ -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)
diff --git a/gcc/rust/rust-location.h b/gcc/rust/rust-location.h
new file mode 100644
index 00000000000..1bb875fe6a4
--- /dev/null
+++ b/gcc/rust/rust-location.h
@@ -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)
diff --git a/gcc/rust/rust-system.h b/gcc/rust/rust-system.h
new file mode 100644
index 00000000000..3a600237966
--- /dev/null
+++ b/gcc/rust/rust-system.h
@@ -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)
-- 
2.38.1


  parent reply	other threads:[~2022-12-06 10:14 UTC|newest]

Thread overview: 81+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-06 10:13 Rust front-end patches v4 arthur.cohen
2022-12-06 10:13 ` [PATCH Rust front-end v4 01/46] Use DW_ATE_UTF for the Rust 'char' type arthur.cohen
2022-12-06 10:13 ` [PATCH Rust front-end v4 02/46] gccrs: Add necessary hooks for a Rust front-end testsuite arthur.cohen
2022-12-06 10:13 ` [PATCH Rust front-end v4 03/46] gccrs: Add Debug info testsuite arthur.cohen
2022-12-06 10:13 ` [PATCH Rust front-end v4 04/46] gccrs: Add link cases testsuite arthur.cohen
2022-12-06 10:13 ` [PATCH Rust front-end v4 05/46] gccrs: Add general compilation test cases arthur.cohen
2022-12-06 10:13 ` [PATCH Rust front-end v4 06/46] gccrs: Add execution " arthur.cohen
2022-12-06 10:13 ` [PATCH Rust front-end v4 07/46] gccrs: Add gcc-check-target check-rust arthur.cohen
2022-12-06 10:13 ` [PATCH Rust front-end v4 08/46] gccrs: Add Rust front-end base AST data structures arthur.cohen
2022-12-06 10:13 ` [PATCH Rust front-end v4 09/46] gccrs: Add definitions of Rust Items in " arthur.cohen
2022-12-06 10:13 ` [PATCH Rust front-end v4 10/46] gccrs: Add full definitions of Rust " arthur.cohen
2022-12-06 10:13 ` [PATCH Rust front-end v4 11/46] gccrs: Add Rust AST visitors arthur.cohen
2022-12-06 10:13 ` [PATCH Rust front-end v4 12/46] gccrs: Add Lexer for Rust front-end arthur.cohen
2022-12-06 10:13 ` [PATCH Rust front-end v4 13/46] gccrs: Add Parser for Rust front-end pt.1 arthur.cohen
2022-12-06 10:13 ` [PATCH Rust front-end v4 14/46] gccrs: Add Parser for Rust front-end pt.2 arthur.cohen
2022-12-06 10:13 ` [PATCH Rust front-end v4 15/46] gccrs: Add expansion pass for the Rust front-end arthur.cohen
2022-12-06 10:13 ` [PATCH Rust front-end v4 16/46] gccrs: Add name resolution pass to " arthur.cohen
2022-12-06 10:13 ` [PATCH Rust front-end v4 17/46] gccrs: Add declarations for Rust HIR arthur.cohen
2022-12-06 10:13 ` [PATCH Rust front-end v4 18/46] gccrs: Add HIR definitions and visitor framework arthur.cohen
2022-12-06 10:13 ` [PATCH Rust front-end v4 19/46] gccrs: Add AST to HIR lowering pass arthur.cohen
2022-12-06 10:13 ` [PATCH Rust front-end v4 20/46] gccrs: Add wrapper for make_unique arthur.cohen
2022-12-07  8:50   ` Arsen Arsenović
2022-12-07  9:14     ` Thomas Schwinge
2022-12-06 10:13 ` [PATCH Rust front-end v4 21/46] gccrs: Add port of FNV hash used during legacy symbol mangling arthur.cohen
2022-12-06 10:13 ` [PATCH Rust front-end v4 22/46] gccrs: Add Rust ABI enum helpers arthur.cohen
2022-12-06 10:13 ` [PATCH Rust front-end v4 23/46] gccrs: Add Base62 implementation arthur.cohen
2022-12-06 10:13 ` [PATCH Rust front-end v4 24/46] gccrs: Add implementation of Optional arthur.cohen
2022-12-06 10:13 ` [PATCH Rust front-end v4 25/46] gccrs: Add attributes checker arthur.cohen
2022-12-06 10:13 ` [PATCH Rust front-end v4 26/46] gccrs: Add helpers mappings canonical path and lang items arthur.cohen
2022-12-06 10:13 ` [PATCH Rust front-end v4 27/46] gccrs: Add type resolution and trait solving pass arthur.cohen
2022-12-06 10:14 ` [PATCH Rust front-end v4 28/46] gccrs: Add Rust type information arthur.cohen
2022-12-06 10:14 ` [PATCH Rust front-end v4 29/46] gccrs: Add remaining type system transformations arthur.cohen
2022-12-06 10:14 ` [PATCH Rust front-end v4 30/46] gccrs: Add unsafe checks for Rust arthur.cohen
2022-12-06 10:14 ` [PATCH Rust front-end v4 31/46] gccrs: Add const checker arthur.cohen
2022-12-06 10:14 ` [PATCH Rust front-end v4 32/46] gccrs: Add privacy checks arthur.cohen
2022-12-06 10:14 ` [PATCH Rust front-end v4 33/46] gccrs: Add dead code scan on HIR arthur.cohen
2022-12-06 10:14 ` [PATCH Rust front-end v4 34/46] gccrs: Add unused variable scan arthur.cohen
2022-12-06 10:14 ` [PATCH Rust front-end v4 35/46] gccrs: Add metadata output pass arthur.cohen
2022-12-06 10:14 ` [PATCH Rust front-end v4 36/46] gccrs: Add base for HIR to GCC GENERIC lowering arthur.cohen
2022-12-06 10:14 ` [PATCH Rust front-end v4 37/46] gccrs: Add HIR to GCC GENERIC lowering for all nodes arthur.cohen
2022-12-06 10:14 ` [PATCH Rust front-end v4 38/46] gccrs: Add HIR to GCC GENERIC lowering entry point arthur.cohen
2022-12-06 10:14 ` arthur.cohen [this message]
2022-12-06 10:14 ` [PATCH Rust front-end v4 40/46] gccrs: Add GCC Rust front-end Make-lang.in arthur.cohen
2022-12-06 10:14 ` [PATCH Rust front-end v4 41/46] gccrs: Add config-lang.in arthur.cohen
2022-12-06 10:14 ` [PATCH Rust front-end v4 42/46] gccrs: Add lang-spec.h arthur.cohen
2022-12-06 10:14 ` [PATCH Rust front-end v4 43/46] gccrs: Add lang.opt arthur.cohen
2022-12-06 10:14 ` [PATCH Rust front-end v4 44/46] gccrs: Add compiler driver arthur.cohen
2022-12-06 10:14 ` [PATCH Rust front-end v4 45/46] gccrs: Compiler proper interface kicks off the pipeline arthur.cohen
2022-12-06 10:14 ` [PATCH Rust front-end v4 46/46] gccrs: Add README, CONTRIBUTING and compiler logo arthur.cohen
2022-12-09 10:18   ` Martin Liška
2022-12-13  1:43     ` Joseph Myers
2022-12-13 12:59       ` Martin Liška
2022-12-13 18:46         ` Joseph Myers
2022-12-06 11:03 ` Rust front-end patches v4 Richard Biener
2022-12-06 11:09   ` John Paul Adrian Glaubitz
2022-12-06 11:40     ` Arthur Cohen
2022-12-06 11:57       ` John Paul Adrian Glaubitz
2022-12-06 12:40         ` Mark Wielaard
2022-12-06 11:41   ` Iain Buclaw
2022-12-10  6:39   ` Prepare 'contrib/gcc-changelog/git_commit.py' for GCC/Rust (was: Rust front-end patches v4) Thomas Schwinge
2022-12-10  7:37     ` Add stub 'gcc/rust/ChangeLog' (was: Prepare 'contrib/gcc-changelog/git_commit.py' for GCC/Rust) Thomas Schwinge
2022-12-13 13:26   ` Rust front-end patches v4 Arthur Cohen
2022-12-13 13:30     ` Martin Liška
2022-12-13 13:53       ` Arthur Cohen
2022-12-13 13:40     ` Arthur Cohen
2022-12-14 22:58       ` Make '-frust-incomplete-and-experimental-compiler-do-not-use' a 'Common' option (was: Rust front-end patches v4) Thomas Schwinge
2022-12-15  7:53         ` Richard Biener
2022-12-15 10:14           ` Thomas Schwinge
2022-12-15 11:16             ` Jakub Jelinek
2022-12-15 11:39               ` Iain Buclaw
2022-12-15 11:50                 ` Jakub Jelinek
2022-12-15 15:01                   ` Thomas Schwinge
2022-12-15 15:17                     ` Jakub Jelinek
2022-12-16 14:10                       ` Add '-Wno-complain-wrong-lang', and use it in 'gcc/testsuite/lib/target-supports.exp:check_compile' and elsewhere (was: Make '-frust-incomplete-and-experimental-compiler-do-not-use' a 'Common' option) Thomas Schwinge
2022-12-16 21:24                         ` Iain Buclaw
2023-01-11 11:41                         ` [PING] Add '-Wno-complain-wrong-lang', and use it in 'gcc/testsuite/lib/target-supports.exp:check_compile' and elsewhere Thomas Schwinge
2023-01-11 12:31                           ` Jakub Jelinek
2023-02-21 10:21                             ` [PING, v2] " Thomas Schwinge
2023-02-21 23:20                               ` Joseph Myers
2022-12-09 13:24 ` Rust front-end patches v4 Martin Liška
2022-12-10 21:44   ` Thomas Schwinge

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221206101417.778807-40-arthur.cohen@embecosm.com \
    --to=arthur.cohen@embecosm.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=gcc-rust@gcc.gnu.org \
    --cc=jakub@redhat.com \
    --cc=philip.herron@embecosm.com \
    --cc=richard.guenther@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).