From patchwork Mon Nov 7 21:45:47 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Harald Anlauf X-Patchwork-Id: 60148 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 57AFB385828C for ; Mon, 7 Nov 2022 21:46:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 57AFB385828C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1667857587; bh=gkuIQS/Hk5umq2QTzhx4yK3tw9rqW8tMjX5rPABSVpU=; h=Date:Subject:To:Cc:References:In-Reply-To:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=obZND4CGftZ/WaTR5f0jBjUFcluZrza9QcMfzxgyp4+x1qDEeXJ+5WigEQZ84x8ur i7VGmOPZ1zNpcEOnCXLM/g/G7v7qiQE7Txy8+vgVnGBimNAYIOSbvl5WdlKA59G2Bp q/IzNnCdksz3kmHmP0f5w/mcrVSTEWcVyfefkaXM= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mout.gmx.net (mout.gmx.net [212.227.17.22]) by sourceware.org (Postfix) with ESMTPS id C521C3858D39; Mon, 7 Nov 2022 21:45:50 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C521C3858D39 X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from [192.168.178.29] ([93.207.86.30]) by mail.gmx.net (mrgmx104 [212.227.17.168]) with ESMTPSA (Nemesis) id 1MyKDU-1pD62K0c16-00ylPE; Mon, 07 Nov 2022 22:45:48 +0100 Message-ID: <24c6acfa-6745-c7a3-4bbd-54bd0fa31454@gmx.de> Date: Mon, 7 Nov 2022 22:45:47 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.4.0 Subject: [PATCH, v3] Fortran: ordering of hidden procedure arguments [PR107441] To: Mikael Morin , fortran@gcc.gnu.org Cc: gcc-patches@gcc.gnu.org Newsgroups: gmane.comp.gcc.fortran,gmane.comp.gcc.patches References: <7d8ddf07-e66d-2678-de99-0e575c70ea17@orange.fr> <327319ac-4ef9-1e48-e993-57113d802d3b@orange.fr> <85a5951a-7ea4-57b3-895a-ff7dbf1ef92e@orange.fr> <93a5f029-4411-3424-f6ee-3b2bcf210050@gmx.de> <8725411a-979b-dd53-d1fe-5b041482a8eb@gmx.de> <91afe6ef-e5f4-d3d8-ad15-3271fd4e61cd@orange.fr> <3ca46ea0-ee6a-cbc6-d3af-99b8db698307@orange.fr> Content-Language: en-US In-Reply-To: <3ca46ea0-ee6a-cbc6-d3af-99b8db698307@orange.fr> X-Provags-ID: V03:K1:p5aJn3GGsntBS2hICb+JlB4b9JWwh+vWS/Hpz49f5Y27hTI2UL9 asolO3WzxessrwNxGoCZzpyEJIGCh3m3ASDyC7ghhsvpXulvgbl1jbTtNsBUzhA7FFtSLC4 SIWuOHGtKqoH7tnJ+3T4vTpdtyFavM+KOABKukIa1RjXeSlSQ3tC04gc7vV6mJWVj7h84mM h+0hNoVUFANyaZWWy0iHg== UI-OutboundReport: notjunk:1;M01:P0:NrotoX2eAPQ=;1Qx/0X0M9sNwo+9ye99Zoi3yXXN DH9/7eVbMB63HzMLc28TqkUm53jesOCpIHEmvp/1tiNYTgRjpA4WgqyBn8ubuBL3SlECLlHLW 0MLXwaaCwFJ2DoMyScJkvC/OegNVoYv4r3XEArFxFqutBkQQqsRJShzhGFbZTLx4o/y/MWQfa bUB7Tl4+wWSIwQqy/v5eq0M5GWHL3fFc1luhIeEL1dDaIL9fwsDlAEY1G9NbV6gRSYYtypFQl qxU80aXOzcBlZeB+yKBGpkTr15/5/GkOKshiYA8gdV8eKUNqOVu/QIOreczMHdLgeeuI4rosA 0/KJCvPje5ckoG/N+Aw9MGgqJ1crOEfA7PVCA2Y7YVo9+nBfLTijUrPTC3YP85C/l32psVnKn WoKjFrN/CBq4Ujasb7CYZpOdoykXIJUoluNa0Jkx/BdB12TOpBIJ13VC/MHbrKi8jJ8S+RZHt /mitdPDhlamB8yPYCrZKRObhP198+M2JrzWLa7eXmQTnL9Qs3+Hx3W9xVTbFuZnbm79jDOw9l fm2ZCLe6QRBEjH8lYRAW8sdXSRH+tCw18cC2faeMeFF7oWivSzvLvHlnuNzFNheCVNREzIA3i +gSN7fQWA54+e7uP4Gz3/9BE4dWFdq/pJoX1OqdB3363uYADlgcnSEDTgnBZX1fqqJVxc3a14 N+NVzeSHUiqV+a25hNKfRpmaj5DNqzjYb7AdcyBHvqq4m9wxB6dmxlTt39/n77PF0I2+M6gTT QZmpKvL8kLUb3cyKd2w3V/mygnOv4GC/RcGN+80sqRj99kJ2koCLtb/ro5vS46UhkUkfpMWsK Wv2PWeaw6O0VrFhmMf49LAvkdkZT9JhNFoCmvCkOdUwjuw2c1Ox+T7XuxTRuvvKmhkCihpqBU llBYAPJOy0lMVjObfUFEaPnJC/I9lX27appzK9wkEDe+oL+HvWUdd/ysZdotdXNnY4HsT0Tig aXg4BrLgVp460/GZaClMoPWYJOA= X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Harald Anlauf via Gcc-patches From: Harald Anlauf Reply-To: Harald Anlauf Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Dear all, Am 04.11.22 um 10:53 schrieb Mikael Morin: > Le 03/11/2022 à 23:03, Harald Anlauf a écrit : >> I've spent some time not only staring at create_function_arglist, >> but trying several variations handling the declared hidden parms, >> and applying the necessary adjustments to gfc_get_function_type. >> (Managing linked trees is not the issue, just understanding them.) >> I've been unable to get the declarations in sync, and would need >> help how to debug the mess I've created.  Dropping my patch for >> the time being. >> > If you want, we can meet on IRC somewhen (tonight?). armed with the new knowledge, I could now understand what (more or less) trivially went wrong with my previous patch. The attached patch remedies that: gfc_get_function_type() now properly separates the types of the hidden parameters so that optional+value comes before string length and caf stuff, while in create_function_arglist we simply need to split the walking over the typelists so that the optional+value stuff, which is basically just booleans, is done separately from the other parts. Looking at the tree-dumps, the function decls now seem to be fine at least for the given testcases. I've adjusted one of the testcases to validate this. Regtests fine on x86_64-pc-linux-gnu. OK for mainline? Thanks, Harald From 7ba433c9c22e206532a9abcad8ff1b22d3f77b3a Mon Sep 17 00:00:00 2001 From: Harald Anlauf Date: Fri, 28 Oct 2022 21:58:08 +0200 Subject: [PATCH] Fortran: ordering of hidden procedure arguments [PR107441] The gfortran ABI specifies the order of given and hidden procedure arguments, where the hidden presence status flags of optional+value scalar arguments shall come before character length, coarray token and offset. gcc/fortran/ChangeLog: PR fortran/107441 * trans-decl.cc (create_function_arglist): Adjust the ordering of automatically generated hidden procedure arguments to match the documented ABI for gfortran. * trans-types.cc (gfc_get_function_type): Separate hidden parameters so that the presence flag for optional+value arguments come before string length, coarray token and offset, as required. gcc/testsuite/ChangeLog: PR fortran/107441 * gfortran.dg/coarray/pr107441-caf.f90: New test. * gfortran.dg/optional_absent_6.f90: New test. * gfortran.dg/optional_absent_7.f90: New test. --- gcc/fortran/trans-decl.cc | 23 +++++-- gcc/fortran/trans-types.cc | 11 +++- .../gfortran.dg/coarray/pr107441-caf.f90 | 27 +++++++++ .../gfortran.dg/optional_absent_6.f90 | 60 +++++++++++++++++++ .../gfortran.dg/optional_absent_7.f90 | 31 ++++++++++ 5 files changed, 145 insertions(+), 7 deletions(-) create mode 100644 gcc/testsuite/gfortran.dg/coarray/pr107441-caf.f90 create mode 100644 gcc/testsuite/gfortran.dg/optional_absent_6.f90 create mode 100644 gcc/testsuite/gfortran.dg/optional_absent_7.f90 diff --git a/gcc/fortran/trans-decl.cc b/gcc/fortran/trans-decl.cc index 63515b9072a..94988b8690e 100644 --- a/gcc/fortran/trans-decl.cc +++ b/gcc/fortran/trans-decl.cc @@ -2507,8 +2507,8 @@ create_function_arglist (gfc_symbol * sym) { tree fndecl; gfc_formal_arglist *f; - tree typelist, hidden_typelist; - tree arglist, hidden_arglist; + tree typelist, hidden_typelist, optval_typelist; + tree arglist, hidden_arglist, optval_arglist; tree type; tree parm; @@ -2518,6 +2518,7 @@ create_function_arglist (gfc_symbol * sym) the new FUNCTION_DECL node. */ arglist = NULL_TREE; hidden_arglist = NULL_TREE; + optval_arglist = NULL_TREE; typelist = TYPE_ARG_TYPES (TREE_TYPE (fndecl)); if (sym->attr.entry_master) @@ -2619,6 +2620,15 @@ create_function_arglist (gfc_symbol * sym) if (f->sym != NULL) /* Ignore alternate returns. */ hidden_typelist = TREE_CHAIN (hidden_typelist); + /* Advance hidden_typelist over optional+value argument presence flags. */ + optval_typelist = hidden_typelist; + for (f = gfc_sym_get_dummy_args (sym); f; f = f->next) + if (f->sym != NULL + && f->sym->attr.optional && f->sym->attr.value + && !f->sym->attr.dimension && f->sym->ts.type != BT_CLASS + && !gfc_bt_struct (f->sym->ts.type)) + hidden_typelist = TREE_CHAIN (hidden_typelist); + for (f = gfc_sym_get_dummy_args (sym); f; f = f->next) { char name[GFC_MAX_SYMBOL_LEN + 2]; @@ -2712,14 +2722,16 @@ create_function_arglist (gfc_symbol * sym) PARM_DECL, get_identifier (name), boolean_type_node); - hidden_arglist = chainon (hidden_arglist, tmp); + optval_arglist = chainon (optval_arglist, tmp); DECL_CONTEXT (tmp) = fndecl; DECL_ARTIFICIAL (tmp) = 1; DECL_ARG_TYPE (tmp) = boolean_type_node; TREE_READONLY (tmp) = 1; gfc_finish_decl (tmp); - hidden_typelist = TREE_CHAIN (hidden_typelist); + /* The presence flag must be boolean. */ + gcc_assert (TREE_VALUE (optval_typelist) == boolean_type_node); + optval_typelist = TREE_CHAIN (optval_typelist); } /* For non-constant length array arguments, make sure they use @@ -2863,6 +2875,9 @@ create_function_arglist (gfc_symbol * sym) typelist = TREE_CHAIN (typelist); } + /* Add hidden present status for optional+value arguments. */ + arglist = chainon (arglist, optval_arglist); + /* Add the hidden string length parameters, unless the procedure is bind(C). */ if (!sym->attr.is_bind_c) diff --git a/gcc/fortran/trans-types.cc b/gcc/fortran/trans-types.cc index def7552ac67..42907becd27 100644 --- a/gcc/fortran/trans-types.cc +++ b/gcc/fortran/trans-types.cc @@ -3105,6 +3105,7 @@ gfc_get_function_type (gfc_symbol * sym, gfc_actual_arglist *actual_args, { tree type; vec *typelist = NULL; + vec *hidden_typelist = NULL; gfc_formal_arglist *f; gfc_symbol *arg; int alternate_return = 0; @@ -3222,7 +3223,7 @@ gfc_get_function_type (gfc_symbol * sym, gfc_actual_arglist *actual_args, so that the value can be returned. */ type = build_pointer_type (gfc_charlen_type_node); - vec_safe_push (typelist, type); + vec_safe_push (hidden_typelist, type); } /* For noncharacter scalar intrinsic types, VALUE passes the value, hence, the optional status cannot be transferred via a NULL pointer. @@ -3245,11 +3246,15 @@ gfc_get_function_type (gfc_symbol * sym, gfc_actual_arglist *actual_args, && CLASS_DATA (arg)->attr.codimension && !CLASS_DATA (arg)->attr.allocatable))) { - vec_safe_push (typelist, pvoid_type_node); /* caf_token. */ - vec_safe_push (typelist, gfc_array_index_type); /* caf_offset. */ + vec_safe_push (hidden_typelist, pvoid_type_node); /* caf_token. */ + vec_safe_push (hidden_typelist, gfc_array_index_type); /* caf_offset. */ } } + /* Put hidden character length, caf_token, caf_offset at the end. */ + vec_safe_reserve (typelist, vec_safe_length (hidden_typelist)); + vec_safe_splice (typelist, hidden_typelist); + if (!vec_safe_is_empty (typelist) || sym->attr.is_main_program || sym->attr.if_source != IFSRC_UNKNOWN) diff --git a/gcc/testsuite/gfortran.dg/coarray/pr107441-caf.f90 b/gcc/testsuite/gfortran.dg/coarray/pr107441-caf.f90 new file mode 100644 index 00000000000..23b2242e217 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/coarray/pr107441-caf.f90 @@ -0,0 +1,27 @@ +! { dg-do run } +! +! PR fortran/107441 +! Check that with -fcoarray=lib, coarray metadata arguments are passed +! in the right order to procedures. +! +! Contributed by M.Morin + +program p + integer :: ci[*] + ci = 17 + call s(1, ci, "abcd") +contains + subroutine s(ra, ca, c) + integer :: ra, ca[*] + character(*) :: c + ca[1] = 13 + if (ra /= 1) stop 1 + if (this_image() == 1) then + if (ca /= 13) stop 2 + else + if (ca /= 17) stop 3 + end if + if (len(c) /= 4) stop 4 + if (c /= "abcd") stop 5 + end subroutine s +end program p diff --git a/gcc/testsuite/gfortran.dg/optional_absent_6.f90 b/gcc/testsuite/gfortran.dg/optional_absent_6.f90 new file mode 100644 index 00000000000..b8abb06980a --- /dev/null +++ b/gcc/testsuite/gfortran.dg/optional_absent_6.f90 @@ -0,0 +1,60 @@ +! { dg-do run } +! PR fortran/107441 +! +! Test VALUE + OPTIONAL for integer/real/... +! in the presence of non-optional character dummies + +program bugdemo + implicit none + character :: s = 'a' + integer :: t + + t = testoptional(s) + call test2 (s) + call test3 (s) + call test4 (w='123',x=42) + +contains + + function testoptional (w, x) result(t) + character, intent(in) :: w + integer, intent(in), value, optional :: x + integer :: t + print *, 'present(x) is', present(x) + t = 0 + if (present (x)) stop 1 + end function testoptional + + subroutine test2 (w, x) + character, intent(in) :: w + integer, intent(in), value, optional :: x + print*, 'present(x) is', present(x) + if (present (x)) stop 2 + end subroutine test2 + + subroutine test3 (w, x) + character, intent(in), optional :: w + integer, intent(in), value, optional :: x + print *, 'present(w) is', present(w) + print *, 'present(x) is', present(x) + if (.not. present (w)) stop 3 + if (present (x)) stop 4 + end subroutine test3 + + subroutine test4 (r, w, x) + real, value, optional :: r + character(*), intent(in), optional :: w + integer, value, optional :: x + print *, 'present(r) is', present(r) + print *, 'present(w) is', present(w) + print *, 'present(x) is', present(x) + if (present (r)) stop 5 + if (.not. present (w)) stop 6 + if (.not. present (x)) stop 7 + print *, 'x=', x + print *, 'len(w)=', len(w) + if (len(w) /= 3) stop 8 + if (x /= 42) stop 9 + end subroutine test4 + +end program bugdemo diff --git a/gcc/testsuite/gfortran.dg/optional_absent_7.f90 b/gcc/testsuite/gfortran.dg/optional_absent_7.f90 new file mode 100644 index 00000000000..1be981c88f6 --- /dev/null +++ b/gcc/testsuite/gfortran.dg/optional_absent_7.f90 @@ -0,0 +1,31 @@ +! { dg-do run } +! { dg-additional-options "-fdump-tree-original" } +! +! PR fortran/107441 +! Check that procedure types and procedure decls match when the procedure +! has both character-typed and optional value args. +! +! Contributed by M.Morin + +program p + interface + subroutine i(c, o) + character(*) :: c + integer, optional, value :: o + end subroutine i + end interface + procedure(i), pointer :: pp + pp => s + call pp("abcd") +contains + subroutine s(c, o) + character(*) :: c + integer, optional, value :: o + if (present(o)) stop 1 + if (len(c) /= 4) stop 2 + if (c /= "abcd") stop 3 + end subroutine s +end program p + +! { dg-final { scan-tree-dump "void s .* c, .* o, logical.* _o, integer.* _c" "original" } } +! { dg-final { scan-tree-dump ", integer.*, logical.*, integer.* pp" "original" } } -- 2.35.3