From patchwork Wed Aug 9 20:21:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mikael Morin X-Patchwork-Id: 133483 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp3053392vqr; Wed, 9 Aug 2023 13:23:56 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHxLwYdmiaeBnjwp0IIoZiLXP0+n9KOvpxRffT4ry7QRaReidAUMHmzw1298FzHzmey6gDo X-Received: by 2002:a05:6512:3136:b0:4fb:7666:3bbf with SMTP id p22-20020a056512313600b004fb76663bbfmr135305lfd.26.1691612635989; Wed, 09 Aug 2023 13:23:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691612635; cv=none; d=google.com; s=arc-20160816; b=nVNOPrKj3x+7oRbsn59c5tNcy+885gLrrKz6l7aNnH0vBjp/nQLHiyyqpnSi6vQ11R zvuxPLgeoglflkCb4e478DbmCjgY0kHzJPcfHgzbHN3LeGNhzXrHW0Rt8i70b1En7ABS YtvpvuSOysUdhqVzxW/lcPPC3s+6mUh+GYAj4CIke3nNTUVe6tx76BLMQSjwSVV2qU1O YZPCZfF+KAYKazSC1olgJg/SO06rpxm7rrjtEqQkWAAouumodn6vm2WlJjnhM5uV57kf FzphI+5jYPbCSr2dR4Kzs1evsIiNmN4gixx21WTjCsglhi/GCo5ORMB3MhhxH10BglIi k54Q== 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 :content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:dmarc-filter:delivered-to:dkim-signature :dkim-filter; bh=bEOGboPTZeLZPH3V3a3fo1Iu+qywHXPkfjc/aFsYHuc=; fh=EAqAZnhg4AYtcBjfPm18lEF5V0R2rkI9MSWQf+svVaI=; b=ro5F+Mhd++GJplJk+5uKpaVvCqBuZkkWj7x2yvwXETE7WRy4KM7h8cKToaVyKh0xXh cqawqJOgKkGARShjdjMLt7liM1RTF3h9fnWXNwQCawiSRKCkGeMp44qpm8OorPmCqzsn /Bl7zxBKbDGZhJPFdkxzllNyM40TN0oMe12yHeWaAVkYvt0FWussksG/URhZU9l9CxAV sBGWVs0bk/3QSA0pQu8SgqX8iAn5GeokxdXA1NETgR3WIS1qEWcc3FGYWZyhcHbF8KJh vEyt+qdLWRNpKhqul1D0fwZZjGEI6Dbgw7lSthfPxq0k7sz6NCdkR1nVQFipwaQgpCTs j3nw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=f7Oq47OY; 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 (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id c26-20020a170906155a00b00997bd26a559si8611272ejd.881.2023.08.09.13.23.55 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Aug 2023 13:23:55 -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=f7Oq47OY; 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 903A438313AC for ; Wed, 9 Aug 2023 20:22:34 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 903A438313AC DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1691612554; bh=bEOGboPTZeLZPH3V3a3fo1Iu+qywHXPkfjc/aFsYHuc=; h=To:Subject:Date:In-Reply-To:References:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To: From; b=f7Oq47OY7bq2I6kqdT8ttp2WRSoCwqx3p9y37bgtKQMOUKiT4guluoHupgFD8G5+s +I0hx5pb5mIppxGU4SufuRRFb3jh6txPxwOnPSsUphLdJoINPjQnhHo1lTeaGwwTFy BH5hOAOjOfcCfIaVTIlwm7MxfP+sOkuijbfPV1WY= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtp.smtpout.orange.fr (smtp-20.smtpout.orange.fr [80.12.242.20]) by sourceware.org (Postfix) with ESMTPS id AF3DD385E00E for ; Wed, 9 Aug 2023 20:21:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AF3DD385E00E Received: from cyrano.home ([86.215.161.51]) by smtp.orange.fr with ESMTPA id TpgJquxgNE5mdTpgQq56Yq; Wed, 09 Aug 2023 22:21:30 +0200 X-ME-Helo: cyrano.home X-ME-Auth: bW9yaW4tbWlrYWVsQG9yYW5nZS5mcg== X-ME-Date: Wed, 09 Aug 2023 22:21:30 +0200 X-ME-IP: 86.215.161.51 To: fortran@gcc.gnu.org, gcc-patches@gcc.gnu.org Subject: [PATCH 1/3] fortran: New predicate gfc_length_one_character_type_p Date: Wed, 9 Aug 2023 22:21:20 +0200 Message-Id: <20230809202122.695376-2-mikael@gcc.gnu.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230809202122.695376-1-mikael@gcc.gnu.org> References: <20230809202122.695376-1-mikael@gcc.gnu.org> MIME-Version: 1.0 X-Spam-Status: No, score=-11.7 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, FORGED_SPF_HELO, GIT_PATCH_0, JMQ_SPF_NEUTRAL, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_PASS, SPF_NEUTRAL, 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: Mikael Morin via Gcc-patches From: Mikael Morin Reply-To: Mikael Morin Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773784411519290879 X-GMAIL-MSGID: 1773784411519290879 Introduce a new predicate to simplify conditionals checking for a character type whose length is the constant one. gcc/fortran/ChangeLog: * gfortran.h (gfc_length_one_character_type_p): New inline function. * check.cc (is_c_interoperable): Use gfc_length_one_character_type_p. * decl.cc (verify_bind_c_sym): Same. * trans-expr.cc (gfc_conv_procedure_call): Same. --- gcc/fortran/check.cc | 7 +++---- gcc/fortran/decl.cc | 4 +--- gcc/fortran/gfortran.h | 15 +++++++++++++++ gcc/fortran/trans-expr.cc | 8 ++------ 4 files changed, 21 insertions(+), 13 deletions(-) diff --git a/gcc/fortran/check.cc b/gcc/fortran/check.cc index 4086dc71d34..6c45e6542f0 100644 --- a/gcc/fortran/check.cc +++ b/gcc/fortran/check.cc @@ -5250,10 +5250,9 @@ is_c_interoperable (gfc_expr *expr, const char **msg, bool c_loc, bool c_f_ptr) && !gfc_simplify_expr (expr->ts.u.cl->length, 0)) gfc_internal_error ("is_c_interoperable(): gfc_simplify_expr failed"); - if (!c_loc && expr->ts.u.cl - && (!expr->ts.u.cl->length - || expr->ts.u.cl->length->expr_type != EXPR_CONSTANT - || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1) != 0)) + if (!c_loc + && expr->ts.u.cl + && !gfc_length_one_character_type_p (&expr->ts)) { *msg = "Type shall have a character length of 1"; return false; diff --git a/gcc/fortran/decl.cc b/gcc/fortran/decl.cc index 844345df77e..8182ef29f43 100644 --- a/gcc/fortran/decl.cc +++ b/gcc/fortran/decl.cc @@ -6064,9 +6064,7 @@ verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts, /* BIND(C) functions cannot return a character string. */ if (bind_c_function && tmp_sym->ts.type == BT_CHARACTER) - if (tmp_sym->ts.u.cl == NULL || tmp_sym->ts.u.cl->length == NULL - || tmp_sym->ts.u.cl->length->expr_type != EXPR_CONSTANT - || mpz_cmp_si (tmp_sym->ts.u.cl->length->value.integer, 1) != 0) + if (!gfc_length_one_character_type_p (&tmp_sym->ts)) gfc_error ("Return type of BIND(C) function %qs of character " "type at %L must have length 1", tmp_sym->name, &(tmp_sym->declared_at)); diff --git a/gcc/fortran/gfortran.h b/gcc/fortran/gfortran.h index 6482a885211..d44e5286626 100644 --- a/gcc/fortran/gfortran.h +++ b/gcc/fortran/gfortran.h @@ -3181,6 +3181,21 @@ gfc_finalizer; /************************ Function prototypes *************************/ + +/* Returns true if the type specified in TS is a character type whose length + is the constant one. Otherwise returns false. */ + +inline bool +gfc_length_one_character_type_p (gfc_typespec *ts) +{ + return ts->type == BT_CHARACTER + && ts->u.cl + && ts->u.cl->length + && ts->u.cl->length->expr_type == EXPR_CONSTANT + && ts->u.cl->length->ts.type == BT_INTEGER + && mpz_cmp_ui (ts->u.cl->length->value.integer, 1) == 0; +} + /* decl.cc */ bool gfc_in_match_data (void); match gfc_match_char_spec (gfc_typespec *); diff --git a/gcc/fortran/trans-expr.cc b/gcc/fortran/trans-expr.cc index ef3e6d08f78..6da3975f77c 100644 --- a/gcc/fortran/trans-expr.cc +++ b/gcc/fortran/trans-expr.cc @@ -6453,12 +6453,8 @@ gfc_conv_procedure_call (gfc_se * se, gfc_symbol * sym, dummy arguments are actually passed by value. Strings are truncated to length 1. The BIND(C) case is handled elsewhere. */ - if (fsym->ts.type == BT_CHARACTER - && !fsym->ts.is_c_interop - && fsym->ts.u.cl->length->expr_type == EXPR_CONSTANT - && fsym->ts.u.cl->length->ts.type == BT_INTEGER - && (mpz_cmp_ui - (fsym->ts.u.cl->length->value.integer, 1) == 0)) + if (!fsym->ts.is_c_interop + && gfc_length_one_character_type_p (&fsym->ts)) { if (e->expr_type != EXPR_CONSTANT) {