public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Marc Poulhiès" <poulhies@adacore.com>
To: gcc-patches@gcc.gnu.org
Cc: Eric Botcazou <ebotcazou@adacore.com>
Subject: [COMMITTED 15/16] ada: Fix return mechanism reported by -gnatRm
Date: Fri, 14 Jun 2024 09:36:31 +0200	[thread overview]
Message-ID: <20240614073633.2089692-15-poulhies@adacore.com> (raw)
In-Reply-To: <20240614073633.2089692-1-poulhies@adacore.com>

From: Eric Botcazou <ebotcazou@adacore.com>

The return mechanism of functions is reported when the -gnatRm switch is
specified, but it is incorrect when the result type is not a by-reference
type in the language sense but is nevertheless returned by reference.

gcc/ada/

	* gcc-interface/decl.cc: Include function.h.
	(gnat_to_gnu_param): Minor comment tweaks.
	(gnat_to_gnu_subprog_type): Take into account the default for the
	computation of the return mechanism.  Give a warning if a by-copy
	specified mechanism cannot be honored.

Tested on x86_64-pc-linux-gnu, committed on master.

---
 gcc/ada/gcc-interface/decl.cc | 34 +++++++++++++++++++++++++++-------
 1 file changed, 27 insertions(+), 7 deletions(-)

diff --git a/gcc/ada/gcc-interface/decl.cc b/gcc/ada/gcc-interface/decl.cc
index 23983742605..aa31a888818 100644
--- a/gcc/ada/gcc-interface/decl.cc
+++ b/gcc/ada/gcc-interface/decl.cc
@@ -27,6 +27,7 @@
 #include "system.h"
 #include "coretypes.h"
 #include "target.h"
+#include "function.h"
 #include "tree.h"
 #include "gimple-expr.h"
 #include "stringpool.h"
@@ -5703,6 +5704,7 @@ gnat_to_gnu_param (Entity_Id gnat_param, tree gnu_param_type, bool first,
 
   input_location = saved_location;
 
+  /* Warn if we are asked to pass by copy but cannot.  */
   if (mech == By_Copy && (by_ref || by_component_ptr))
     post_error ("??cannot pass & by copy", gnat_param);
 
@@ -5735,12 +5737,13 @@ gnat_to_gnu_param (Entity_Id gnat_param, tree gnu_param_type, bool first,
   DECL_RESTRICTED_ALIASING_P (gnu_param) = restricted_aliasing_p;
   Sloc_to_locus (Sloc (gnat_param), &DECL_SOURCE_LOCATION (gnu_param));
 
-  /* If no Mechanism was specified, indicate what we're using, then
-     back-annotate it.  */
+  /* If no Mechanism was specified, indicate what we will use.  */
   if (mech == Default)
     mech = (by_ref || by_component_ptr) ? By_Reference : By_Copy;
 
+  /* Back-annotate the mechanism in all cases.  */
   Set_Mechanism (gnat_param, mech);
+
   return gnu_param;
 }
 
@@ -6129,11 +6132,6 @@ gnat_to_gnu_subprog_type (Entity_Id gnat_subprog, bool definition,
 	  associate_subprog_with_dummy_type (gnat_subprog, gnu_return_type);
 	  incomplete_profile_p = true;
 	}
-
-      if (kind == E_Function)
-	Set_Mechanism (gnat_subprog, return_by_direct_ref_p
-				     || return_by_invisi_ref_p
-				     ? By_Reference : By_Copy);
     }
 
   /* A procedure (something that doesn't return anything) shouldn't be
@@ -6636,6 +6634,28 @@ gnat_to_gnu_subprog_type (Entity_Id gnat_subprog, bool definition,
 	  if (warn_shadow)
 	    post_error ("'G'C'C builtin not found for&!??", gnat_subprog);
 	}
+
+      /* Finally deal with the return mechanism for a function.  */
+      if (kind == E_Function)
+	{
+	  /* We return by reference either if this is required by the semantics
+	     of the language or if this is the default for the function.  */
+	  const bool by_ref = return_by_direct_ref_p
+			      || return_by_invisi_ref_p
+			      || aggregate_value_p (gnu_return_type, gnu_type);
+	  Mechanism_Type mech = Mechanism (gnat_subprog);
+
+	  /* Warn if we are asked to return by copy but cannot.  */
+	  if (mech == By_Copy && by_ref)
+	    post_error ("??cannot return from & by copy", gnat_subprog);
+
+	  /* If no mechanism was specified, indicate what we will use.  */
+	  if (mech == Default)
+	    mech = by_ref ? By_Reference : By_Copy;
+
+	  /* Back-annotate the mechanism in all cases.  */
+	  Set_Mechanism (gnat_subprog, mech);
+	}
     }
 
   *param_list = gnu_param_list;
-- 
2.45.1


  parent reply	other threads:[~2024-06-14  7:36 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-06-14  7:36 [COMMITTED 01/16] ada: Remove unused name of aspect from Snames Marc Poulhiès
2024-06-14  7:36 ` [COMMITTED 02/16] ada: Allow implicit dereferenced for uses of 'Super Marc Poulhiès
2024-06-14  7:36 ` [COMMITTED 03/16] ada: Couple of small cleanups in semantic analysis of aspects Marc Poulhiès
2024-06-14  7:36 ` [COMMITTED 04/16] ada: Missing initialization of multidimensional array using sliding Marc Poulhiès
2024-06-14  7:36 ` [COMMITTED 05/16] ada: Minor tweaks to processing of Aggregate aspect Marc Poulhiès
2024-06-14  7:36 ` [COMMITTED 06/16] ada: Crash checking accessibility level on private type Marc Poulhiès
2024-06-14  7:36 ` [COMMITTED 07/16] ada: Add prototype for mutably tagged types Marc Poulhiès
2024-06-14  7:36 ` [COMMITTED 08/16] ada: Minor tweak in Snames Marc Poulhiès
2024-06-14  7:36 ` [COMMITTED 09/16] ada: Simplify handling of VxWorks-specific error codes for ENOENT Marc Poulhiès
2024-06-14  7:36 ` [COMMITTED 10/16] ada: Bad tree built for Obj.Discrim_Dep_Component'Loop_Entry in assertion Marc Poulhiès
2024-06-14  7:36 ` [COMMITTED 11/16] ada: Fix parts of classification of aspects Marc Poulhiès
2024-06-14  7:36 ` [COMMITTED 12/16] ada: Typo and indentation fix Marc Poulhiès
2024-06-14  7:36 ` [COMMITTED 13/16] ada: Do not create null GCC thunks Marc Poulhiès
2024-06-14  7:36 ` [COMMITTED 14/16] ada: Skip subprogram body entities inside scopes Marc Poulhiès
2024-06-14  7:36 ` Marc Poulhiès [this message]
2024-06-14  7:36 ` [COMMITTED 16/16] ada: Do not include target-specific makefile fragments Marc Poulhiès

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=20240614073633.2089692-15-poulhies@adacore.com \
    --to=poulhies@adacore.com \
    --cc=ebotcazou@adacore.com \
    --cc=gcc-patches@gcc.gnu.org \
    /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).