From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mout.gmx.net (mout.gmx.net [212.227.15.15]) by sourceware.org (Postfix) with ESMTPS id AEDD4385770C; Tue, 11 Jul 2023 16:23:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AEDD4385770C Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmx.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmx.de DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gmx.de; s=s31663417; t=1689092633; x=1689697433; i=vehre@gmx.de; bh=7bSOJK8SwELPIrvXYqsQcl7llq5CTkna0VyGeD4Jh5k=; h=X-UI-Sender-Class:Date:From:To:Cc:Subject:In-Reply-To:References; b=UK2hsKOrFvnB3P6HLXtUGUMlAcUHdQq1WRI3af4pQ6rLSW4XfulM0cpe2LrPPksB2oIP9vX WIkbf0koU82ha/TJtielnNLRRGBFf5eC/WN6J5MPG2KeblLJ9Bp5nRk4aZmiNv2/JHEsd0gPr IkQWK7PAYmjszphWRW89wr1hhmaHAyzp/CWtK7aAkXh+tsA++h1tAtqiKpCLLGRNo0ugMCywK G5d6gMCi0v+rYiQn+RRI7X1uu/4aOt4FCkqc/HpStP881YGPyhZbKmYV+rgcyovXP9QO7JRtU SjMSTww/jkyJHuYYqWEJ0SHgaL31mbPMl7l+iZJ7c1O5cBGzzZww== X-UI-Sender-Class: 724b4f7f-cbec-4199-ad4e-598c01a50d3a Received: from vepi2 ([79.194.174.170]) by mail.gmx.net (mrgmx005 [212.227.17.190]) with ESMTPSA (Nemesis) id 1M7sHo-1qODxj0Rhy-0054uQ; Tue, 11 Jul 2023 18:23:53 +0200 Date: Tue, 11 Jul 2023 18:23:52 +0200 From: Andre Vehreschild To: Harald Anlauf Cc: GCC-Patches-ML , GCC-Fortran-ML , Paul Richard Thomas Subject: Re: [Patch, Fortran] Allow ref'ing PDT's len() in parameter-initializer [PR102003] Message-ID: <20230711182352.0e59f735@vepi2> In-Reply-To: <3114593a-0197-62cd-41e9-59f2a8ae9f29@gmx.de> References: <20230710174826.48f9230c@vepi2> <3114593a-0197-62cd-41e9-59f2a8ae9f29@gmx.de> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; x86_64-redhat-linux-gnu) MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="MP_/.iKKU3bwVFQaLlTiiU+GqVo" X-Provags-ID: V03:K1:wwxLKqbot08S26TNcaxglxgwsRlD6doM3M/y5+E7wKIPxdwt3dF jzLoJiE51DJVbK4/D5Ler5rKgUJ3KRqwc7qTU82L6M+w6dJ8xjWaPZqoG35ws9BNeiEE102 NjysRxnpevJZYSIZVWwYt7fS0NsvpzP7mjWUOJWvOVu1aJ9eaY0WkklQ0zZIUAerMBrASNB 5HHPO7og8407P8+JrUYYw== UI-OutboundReport: notjunk:1;M01:P0:BVuAvkE+6lY=;6RSgrBy+KjK3Ontgtk4CQ5hIdlD +FBWRhrR+kKPerPaJxf3jJCWnYDy4GGslhcMt4mTWW4yecAIQ2vMFXO0Mv31BRSgCuSFiA1Yv ezwDMw1stVVmZZGwCPkIXtOFWQAZcbzI5XrsDKg+CuZI7AMzGcgG39fUfLL51CCB0fMLQ1PYq ixKd7rOBUHelFw0TfkBXcQELg5bKv4GSq0Y/4efLS4H+SH+cxjFTZlAdNOHOH4HZ7vuV7TZxO LH6W4wZlDZRv150qs4AL9d1X29RWwtyfDJ8vCovlG7jEQZ3jeVvqGguZnFpQugqCMXXZbFu2q gRn1iK7DpdCh52s+gxdwjSRRyLZPszBG5O7kkR2YSZ1tsgIeNhEmBjBydNRYU8NS0GkYR2Feb Vl4EMWZJpjA6hbjOIyhtH0U39pqFoxYJSScWJj4a+pBjzROsAA1Tekyp/aV5fLyN7DEwp+x6d SxtefoRMLQHJw9hQlcgjO6QPO2rvImzGf833AjmqiDdfGMLSG4P/1g9ApSt4KL7FYQh3lT/bM CvXkK9zj+dTDAzrWW2gXHg/RM+VDVm0XGNDlWicjl0HOTrPXFvMEAeu2rh0CRKopGIJ8/UnZP ouJJXzdcWzRiyqpBe+oYkBh3wRxH8GEW8ahoM6fChrqDQXN/L1Hfxz7pZYCeqJvKRPcgCMm2h ZtiFod1hhMexc/zbIrvKpa5sDZcMMdLnq+j3Se9Vi+RGgNsArNJ39yWnU8sHvJfRoRPvzmTnQ wgkNg8zwV0CnvNWro/iGAoe/9UVidiyYSOj1Rx26nHjlQwxsDNcirg9nD47Bg6K2sRDyuBLfQ sjwHL1BiisxQ50HOd+L6pEqk3w8Xd7XuPzbUmaLTDCbhV7BGRm09rHzMCr7ff5APVLtv8FL14 jjjecq707Gr1QIN0X5FOhstHXBxzC098oolB7cgdnEMa72lTjUO0JqH+aHTM9Fn5ldiJI89dS wrf7y+agTvfb8saiNV3DYEAgy/Y= X-Spam-Status: No, score=-9.1 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,T_SCC_BODY_TEXT_LINE,WEIRD_PORT autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: --MP_/.iKKU3bwVFQaLlTiiU+GqVo Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Hi Harald, attached is a new version of the patch. This now also respects inquiry-LEN= . Btw, there is a potential memory leak in the simplify for inquiry function= s. I have added a note into the code. I tried to use a pdt within a derived type as a component. Is that not all= owed by the standard? I know, I could hunt in the standard for it, but when som= eone knows out of his head, he could greatly help me out. Regtests ok on x86_64-linux-gnu/F37. Regards, Andre On Mon, 10 Jul 2023 20:55:29 +0200 Harald Anlauf wrote: > Hi Andre, > > thanks for looking into this! > > While it fixes the original PR, here is a minor extension of the > testcase that ICEs here with your patch: > > program pr102003 > type pdt(n) > integer, len :: n =3D 8 > character(len=3Dn) :: c > end type pdt > type(pdt(42)) :: p > integer, parameter :: m =3D len (p% c) > integer, parameter :: n =3D p% c% len > > if (m /=3D 42) stop 1 > if (len (p% c) /=3D 42) stop 2 > print *, p% c% len ! OK > if (p% c% len /=3D 42) stop 3 ! OK > print *, n ! ICE > end > > I get: > > pdt_33.f03:14:27: > > 14 | integer, parameter :: n =3D p% c% len > | 1 > Error: non-constant initialization expression at (1) > pdt_33.f03:20:31: > > 20 | print *, n ! ICE > | 1 > internal compiler error: tree check: expected record_type or union_type > or qual_union_type, have integer_type in gfc_conv_component_ref, at > fortran/trans-expr.cc:2757 > 0x84286c tree_check_failed(tree_node const*, char const*, int, char > const*, ...) > ../../gcc-trunk/gcc/tree.cc:8899 > 0xa6d6fb tree_check3(tree_node*, char const*, int, char const*, > tree_code, tree_code, tree_code) > ../../gcc-trunk/gcc/tree.h:3617 > 0xa90847 gfc_conv_component_ref(gfc_se*, gfc_ref*) > ../../gcc-trunk/gcc/fortran/trans-expr.cc:2757 > 0xa91bbc gfc_conv_variable > ../../gcc-trunk/gcc/fortran/trans-expr.cc:3137 > 0xaa8e9c gfc_conv_expr(gfc_se*, gfc_expr*) > ../../gcc-trunk/gcc/fortran/trans-expr.cc:9594 > 0xaa92ae gfc_conv_expr_reference(gfc_se*, gfc_expr*) > ../../gcc-trunk/gcc/fortran/trans-expr.cc:9713 > 0xad67f6 gfc_trans_transfer(gfc_code*) > ../../gcc-trunk/gcc/fortran/trans-io.cc:2607 > 0xa43cb7 trans_code > ../../gcc-trunk/gcc/fortran/trans.cc:2449 > 0xad37c6 build_dt > ../../gcc-trunk/gcc/fortran/trans-io.cc:2051 > 0xa43cd7 trans_code > ../../gcc-trunk/gcc/fortran/trans.cc:2421 > 0xa84711 gfc_generate_function_code(gfc_namespace*) > ../../gcc-trunk/gcc/fortran/trans-decl.cc:7762 > 0x9d9ca7 translate_all_program_units > ../../gcc-trunk/gcc/fortran/parse.cc:6929 > 0x9d9ca7 gfc_parse_file() > ../../gcc-trunk/gcc/fortran/parse.cc:7235 > 0xa40a1f gfc_be_parse_file > ../../gcc-trunk/gcc/fortran/f95-lang.cc:229 > > The fortran-dump confirms that n is not simplified to a constant. > So while you're at it, do you also see a solution to this variant? > > Harald > > > Am 10.07.23 um 17:48 schrieb Andre Vehreschild via Gcc-patches: > > Hi all, > > > > while browsing the pdt meta-bug I came across 102003 and thought to my= self: > > Well, that one is easy. How foolish of me... > > > > Anyway, the solution attached prevents a pdt_len (or pdt_kind) express= ion > > in a function call (e.g. len() or kind()) to mark the whole expression= as a > > pdt one. The second part of the patch in simplify.cc then takes care o= f > > either generating the correct component ref or when a constant express= ion > > (i.e. gfc_init_expr_flag is set) is required to look this up from the > > actual symbol (not from the type, because there the default value is > > stored). > > > > Regtested ok on x86_64-linux-gnu/Fedora 37. > > > > Regards, > > Andre > > -- > > Andre Vehreschild * Email: vehre ad gmx dot de > =2D- Andre Vehreschild * Email: vehre ad gmx dot de --MP_/.iKKU3bwVFQaLlTiiU+GqVo Content-Type: text/plain Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename=pr102003_2.clog gcc/fortran/ChangeLog: * expr.cc (find_inquiry_ref): Replace len of pdt_string by constant. (gfc_match_init_expr): Prevent PDT analysis for function calls. (gfc_pdt_find_component_copy_initializer): Get the initializer value for given component. * gfortran.h (gfc_pdt_find_component_copy_initializer): New function. * simplify.cc (gfc_simplify_len): Replace len() of PDT with pdt component ref or constant. gcc/testsuite/ChangeLog: * gfortran.dg/pdt_33.f03: New test. --MP_/.iKKU3bwVFQaLlTiiU+GqVo Content-Type: text/x-patch Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename=pr102003_2.patch diff --git a/gcc/fortran/expr.cc b/gcc/fortran/expr.cc index e418f1f3301..23324517ff2 100644 =2D-- a/gcc/fortran/expr.cc +++ b/gcc/fortran/expr.cc @@ -1862,6 +1862,13 @@ find_inquiry_ref (gfc_expr *p, gfc_expr **newp) else if (tmp->expr_type =3D=3D EXPR_CONSTANT) *newp =3D gfc_get_int_expr (gfc_default_integer_kind, NULL, tmp->value.character.length); + else if (gfc_init_expr_flag + && tmp->ts.u.cl->length->symtree->n.sym->attr.pdt_len) + *newp =3D gfc_pdt_find_component_copy_initializer (tmp->symtree->n + .sym, + tmp->ts.u.cl + ->length->symtree + ->n.sym->name); else goto cleanup; @@ -1902,6 +1909,7 @@ find_inquiry_ref (gfc_expr *p, gfc_expr **newp) mpc_imagref (tmp->value.complex), GFC_RND_MODE); break; } + // TODO: Fix leaking expr tmp, when simplify is done twice. tmp =3D gfc_copy_expr (*newp); } @@ -3229,7 +3237,7 @@ gfc_match_init_expr (gfc_expr **result) return m; } - if (gfc_derived_parameter_expr (expr)) + if (expr->expr_type !=3D EXPR_FUNCTION && gfc_derived_parameter_expr (e= xpr)) { *result =3D expr; gfc_init_expr_flag =3D false; @@ -6556,3 +6564,19 @@ gfc_check_vardef_context (gfc_expr* e, bool pointer= , bool alloc_obj, return true; } + +gfc_expr* +gfc_pdt_find_component_copy_initializer (gfc_symbol *sym, const char *nam= e) +{ + /* The actual length of a pdt is in its components. In the + initializer of the current ref is only the default value. + Therefore traverse the chain of components and pick the correct + one's initializer expressions. */ + for (gfc_component *comp =3D sym->ts.u.derived->components; comp !=3D N= ULL; + comp =3D comp->next) + { + if (!strcmp (comp->name, name)) + return gfc_copy_expr (comp->initializer); + } + return NULL; +} diff --git a/gcc/fortran/gfortran.h b/gcc/fortran/gfortran.h index 30631abd788..74466c7f04c 100644 =2D-- a/gcc/fortran/gfortran.h +++ b/gcc/fortran/gfortran.h @@ -3727,6 +3727,7 @@ gfc_expr* gfc_find_stat_co (gfc_expr *); gfc_expr* gfc_build_intrinsic_call (gfc_namespace *, gfc_isym_id, const c= har*, locus, unsigned, ...); bool gfc_check_vardef_context (gfc_expr*, bool, bool, bool, const char*); +gfc_expr* gfc_pdt_find_component_copy_initializer (gfc_symbol *, const ch= ar *); /* st.cc */ diff --git a/gcc/fortran/simplify.cc b/gcc/fortran/simplify.cc index 81680117f70..87fefe46cfd 100644 =2D-- a/gcc/fortran/simplify.cc +++ b/gcc/fortran/simplify.cc @@ -4580,19 +4580,50 @@ gfc_simplify_len (gfc_expr *e, gfc_expr *kind) return range_check (result, "LEN"); } else if (e->expr_type =3D=3D EXPR_VARIABLE && e->ts.type =3D=3D BT_CHAR= ACTER - && e->symtree->n.sym - && e->symtree->n.sym->ts.type !=3D BT_DERIVED - && e->symtree->n.sym->assoc && e->symtree->n.sym->assoc->target - && e->symtree->n.sym->assoc->target->ts.type =3D=3D BT_DERIVED - && e->symtree->n.sym->assoc->target->symtree->n.sym - && UNLIMITED_POLY (e->symtree->n.sym->assoc->target->symtree->n.sym)) - - /* The expression in assoc->target points to a ref to the _data compo= nent - of the unlimited polymorphic entity. To get the _len component th= e last - _data ref needs to be stripped and a ref to the _len component add= ed. */ - return gfc_get_len_component (e->symtree->n.sym->assoc->target, k); - else - return NULL; + && e->symtree->n.sym) + { + if (e->symtree->n.sym->ts.type !=3D BT_DERIVED + && e->symtree->n.sym->assoc && e->symtree->n.sym->assoc->target + && e->symtree->n.sym->assoc->target->ts.type =3D=3D BT_DERIVED + && e->symtree->n.sym->assoc->target->symtree->n.sym + && UNLIMITED_POLY (e->symtree->n.sym->assoc->target->symtree->n.sym)) + /* The expression in assoc->target points to a ref to the _data + component of the unlimited polymorphic entity. To get the _len + component the last _data ref needs to be stripped and a ref to the + _len component added. */ + return gfc_get_len_component (e->symtree->n.sym->assoc->target, k); + else if (e->symtree->n.sym->ts.type =3D=3D BT_DERIVED + && e->ref && e->ref->type =3D=3D REF_COMPONENT + && e->ref->u.c.component->attr.pdt_string + && e->ref->u.c.component->ts.type =3D=3D BT_CHARACTER + && e->ref->u.c.component->ts.u.cl->length) + { + if (gfc_init_expr_flag) + { + gfc_expr* tmp; + tmp =3D gfc_pdt_find_component_copy_initializer (e->symtree->n.sym= , + e->ref->u.c + .component->ts.u.cl + ->length->symtree + ->name); + if (tmp) + return tmp; + } + else + { + gfc_expr *len_expr =3D gfc_copy_expr (e); + gfc_free_ref_list (len_expr->ref); + len_expr->ref =3D NULL; + gfc_find_component (len_expr->symtree->n.sym->ts.u.derived, e->ref + ->u.c.component->ts.u.cl->length->symtree + ->name, + false, true, &len_expr->ref); + len_expr->ts =3D len_expr->ref->u.c.component->ts; + return len_expr; + } + } + } + return NULL; } diff --git a/gcc/testsuite/gfortran.dg/pdt_33.f03 b/gcc/testsuite/gfortran= .dg/pdt_33.f03 new file mode 100644 index 00000000000..2637cacceea =2D-- /dev/null +++ b/gcc/testsuite/gfortran.dg/pdt_33.f03 @@ -0,0 +1,21 @@ +! { dg-do run } +! +! Test the fix for PR102003, where len parameters where not returned as c= onstants. +! +! Contributed by Harald Anlauf +! +program pr102003 + type pdt(n) + integer, len :: n =3D 8 + character(len=3Dn) :: c + end type pdt + type(pdt(42)) :: p + integer, parameter :: m =3D len (p% c) + integer, parameter :: lm =3D p% c% len + + if (m /=3D 42) stop 1 + if (len (p% c) /=3D 42) stop 2 + if (lm /=3D 42) stop 3 + if (p% ci% len /=3D 42) stop 4 +end + --MP_/.iKKU3bwVFQaLlTiiU+GqVo--