From patchwork Fri Oct 28 20:12:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Harald Anlauf X-Patchwork-Id: 12531 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp1022809wru; Fri, 28 Oct 2022 13:14:05 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6HEn0GHpRXGfSEt2b6y9vH7J0IQXQ4nDOv4AtfVd0XAVJ4f6Ow0s5rbyVNk3OnTCLBUlzj X-Received: by 2002:a17:907:1c9e:b0:79e:6d97:5e0 with SMTP id nb30-20020a1709071c9e00b0079e6d9705e0mr921195ejc.534.1666988044954; Fri, 28 Oct 2022 13:14:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666988044; cv=none; d=google.com; s=arc-20160816; b=pSkR6xChKI895uw0CsaMuwdD8B6oISmUZyrdFyiDJuwmiaivqy6FDejERy3zFYNezv nL36+DAM0ryA+KCITgSt4QWEpmooRGM/kf9vTNrwwEoHfXXR1M+rHzNOY2Ol/gHY3aUw eAtD/9cGhj6Nf13feHa8FaNmBKT601UfwXsPESR/VcOaxLd9IufuEcbs2r7VhhySYIR1 N1RJxNVhvGjIQxLsP08vp5bnEtZ//AR9OoncjGjKJVklZFiB2+Wo+CETZ9D8KaqlJOrf AxrAXYGKv/pRw2vJd1c8ehuEy5wAjDitUU2kATLtAzP81EP7SMUc7lcjPWhhly+O/ISm xM4g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:ui-outboundreport :sensitivity:importance:date:subject:to:message-id:mime-version :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=I1uq3NSHdmQUmspDFKYTmGS1nPpOniUTde2CTOTWyI4=; b=l75aiW0QC9OZ4/rHfwgL5VPKkuUMowJ1g+dc2ct1xCp7KC3fyDYPt4Zs8dsgPIONg9 EKem+BWRYodimLpG7CXmD7Megm39bzb+tQcN2jnSlWDraxPoH/oIin7wYJxU1SyOKhvr 9OTeVhOktAjmWpFOvnnZybVkN+nZPzBtgZIg1Uvm11F5uH/Qp+4qh6IYjiaPwp9eIQPT m24OvUvpC8Sr6+mZ147kz6YDVsySvwappeH2WKps6wWOxu9Fu33vgQp0eMDjtcJj9V+F oc2yghA1pQvocGjcNhDyDcIo1ZTaGBpncwVcZ0fzRaazvatqNlwXrx0N/LM5czAOct4E KhrA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=BSkQlgMO; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id 28-20020a508e5c000000b004485081f004si6240973edx.598.2022.10.28.13.14.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Oct 2022 13:14:04 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=BSkQlgMO; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C13D63858427 for ; Fri, 28 Oct 2022 20:14:03 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C13D63858427 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1666988043; bh=I1uq3NSHdmQUmspDFKYTmGS1nPpOniUTde2CTOTWyI4=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=BSkQlgMO2z8W6+9IjqX5F8PEj+ieUA1UdtZcB85pLhQ9HbEA5nvBsyfbnUHP9jJV7 tAW5UcaiTYiTLOWvTF2kzR1iNhF/dkHSc8b+xi1/K1d0o7WpYRKVbIrle+MkETwWGN ZC+sB+BJh5IJiueniEzdrDMX+Ls2j+85WqNpb5cM= 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.15.15]) by sourceware.org (Postfix) with ESMTPS id 768D13858CDA; Fri, 28 Oct 2022 20:13:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 768D13858CDA X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from [79.232.145.17] ([79.232.145.17]) by web-mail.gmx.net (3c-app-gmx-bs25.server.lan [172.19.170.77]) (via HTTP); Fri, 28 Oct 2022 22:12:59 +0200 MIME-Version: 1.0 Message-ID: To: fortran , gcc-patches Subject: [PATCH] Fortran: ordering of hidden procedure arguments [PR107441] Date: Fri, 28 Oct 2022 22:12:59 +0200 Importance: normal Sensitivity: Normal X-Priority: 3 X-Provags-ID: V03:K1:2/DiH1gd9s5+9BltfaKPfTqdp7I0A1MeyEJ47sFg8fPtnSVxw2WaiiQsPpAf8RfgFA08D IC8J5VnWRZhoiwL+h0hu24ZTp7airnq96yhkNuIn9Yx3nJiRLjY7l5oNZ8XMsh0Od6SpWw0bcxRv rXUm1FhrCAN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Status: No, score=-12.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, RCVD_IN_BARRACUDACENTRAL, 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+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747963656407409701?= X-GMAIL-MSGID: =?utf-8?q?1747963656407409701?= Dear all, the passing of procedure arguments in Fortran sometimes requires ancillary parameters that are "hidden". Examples are string length and the presence status of scalar variables with optional+value attribute. The gfortran ABI is actually documented: https://gcc.gnu.org/onlinedocs/gfortran/Argument-passing-conventions.html The reporter found that there was a discrepancy between the caller and the callee. This is corrected by the attached patch. Regtested on x86_64-pc-linux-gnu. OK for mainline? Thanks, Harald From b7646403557eca19612c81437f381d4b4dcd51c8 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] 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. gcc/testsuite/ChangeLog: PR fortran/107441 * gfortran.dg/optional_absent_6.f90: New test. --- gcc/fortran/trans-decl.cc | 15 +++-- .../gfortran.dg/optional_absent_6.f90 | 60 +++++++++++++++++++ 2 files changed, 71 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/gfortran.dg/optional_absent_6.f90 diff --git a/gcc/fortran/trans-decl.cc b/gcc/fortran/trans-decl.cc index 63515b9072a..18842fe2c4b 100644 --- a/gcc/fortran/trans-decl.cc +++ b/gcc/fortran/trans-decl.cc @@ -2508,7 +2508,7 @@ create_function_arglist (gfc_symbol * sym) tree fndecl; gfc_formal_arglist *f; tree typelist, hidden_typelist; - tree arglist, hidden_arglist; + tree arglist, hidden_arglist, optional_arglist, strlen_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; + strlen_arglist = optional_arglist = NULL_TREE; typelist = TYPE_ARG_TYPES (TREE_TYPE (fndecl)); if (sym->attr.entry_master) @@ -2644,7 +2645,7 @@ create_function_arglist (gfc_symbol * sym) length = build_decl (input_location, PARM_DECL, get_identifier (name), len_type); - hidden_arglist = chainon (hidden_arglist, length); + strlen_arglist = chainon (strlen_arglist, length); DECL_CONTEXT (length) = fndecl; DECL_ARTIFICIAL (length) = 1; DECL_ARG_TYPE (length) = len_type; @@ -2712,7 +2713,7 @@ create_function_arglist (gfc_symbol * sym) PARM_DECL, get_identifier (name), boolean_type_node); - hidden_arglist = chainon (hidden_arglist, tmp); + optional_arglist = chainon (optional_arglist, tmp); DECL_CONTEXT (tmp) = fndecl; DECL_ARTIFICIAL (tmp) = 1; DECL_ARG_TYPE (tmp) = boolean_type_node; @@ -2863,10 +2864,16 @@ create_function_arglist (gfc_symbol * sym) typelist = TREE_CHAIN (typelist); } + /* Add hidden present status for optional+value arguments. */ + arglist = chainon (arglist, optional_arglist); + /* Add the hidden string length parameters, unless the procedure is bind(C). */ if (!sym->attr.is_bind_c) - arglist = chainon (arglist, hidden_arglist); + arglist = chainon (arglist, strlen_arglist); + + /* Add hidden extra arguments for the gfortran library. */ + arglist = chainon (arglist, hidden_arglist); gcc_assert (hidden_typelist == NULL_TREE || TREE_VALUE (hidden_typelist) == void_type_node); 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 -- 2.35.3