public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Louis Krupp <louis.krupp@zoho.com>
To: "gcc-patches" <gcc-patches@gcc.gnu.org>, "fortran" <fortran@gcc.gnu.org>
Subject: Possible patch for pr62242 -- follow-up
Date: Tue, 15 Sep 2015 06:39:00 -0000	[thread overview]
Message-ID: <14fcf9597a8.102dd3e69123968.6989139803106041754@zoho.com> (raw)
In-Reply-To: <14fb0fefe25.1083784a532578.5562174689543109473@zoho.com>

[-- Attachment #1: Type: text/plain, Size: 4211 bytes --]

Would anyone like me to spend some more time on this and perhaps clear up some of the TODO items?

(Unlike some of you, I'm retired.  I have time for this.)

Louis

 == == == == == == Forwarded message == == == == == == 
From : Louis Krupp<louis.krupp@zoho.com>
To : "gcc-patches"<gcc-patches@gcc.gnu.org>,"fortran"<fortran@gcc.gnu.org>
Date : Wed, 09 Sep 2015 00:25:45 -0700
Subject : Possible patch for pr62242
 == == == == == == Forwarded message == == == == == == 
This was ... interesting. There were a couple of problems that triggered ICEs.

This patch fixes the reported file (I made sure this time) and causes no regressions as far as I can tell.

Dominique ... merci de votre patience.

Louis

Index: gcc/fortran/ChangeLog
===================================================================
--- gcc/fortran/ChangeLog    (revision 227571)
+++ gcc/fortran/ChangeLog    (working copy)
@@ -1,3 +1,12 @@
+2015-09-08 Louis Krupp <louis.krupp@zoho.com>
+
+    PR fortran/62242
+    * trans-array.c (get_array_ctor_all_strlen): Don't store length
+    tree pointer unless we know it's necessary
+    (trans_array_constructor): Create new gfc_charlen instance so
+    context-specific length expression isn't shared
+    (gfc_add_loop_ss_code): Don't try to convert non-constant length
+
 2015-09-04 Francois-Xavier Coudert <fxcoudert@gcc.gnu.org>
 
     * intrinsic.h (gfc_simplify_mvbits): Remove.
Index: gcc/fortran/trans-array.c
===================================================================
--- gcc/fortran/trans-array.c    (revision 227571)
+++ gcc/fortran/trans-array.c    (working copy)
@@ -1836,7 +1836,9 @@ get_array_ctor_all_strlen (stmtblock_t *block, gfc
 gfc_add_block_to_block (block, &se.pre);
 gfc_add_block_to_block (block, &se.post);
 
- e->ts.u.cl->backend_decl = *len;
+ /* TODO: No test cases failed when the "if (0)" was added.
+     Is there a reason to put this back the way it was? */
+ if (0) e->ts.u.cl->backend_decl = *len;
 }
 }
 
@@ -2226,6 +2228,7 @@ trans_array_constructor (gfc_ss * ss, locus * wher
 if (expr->ts.type == BT_CHARACTER)
 {
 bool const_string;
+ gfc_charlen *new_cl;
 
 /* get_array_ctor_strlen walks the elements of the constructor, if a
      typespec was given, we already know the string length and want the one
@@ -2251,8 +2254,36 @@ trans_array_constructor (gfc_ss * ss, locus * wher
      and not end up here. */
 gcc_assert (ss_info->string_length);
 
- expr->ts.u.cl->backend_decl = ss_info->string_length;
+ /* get_array_ctor_strlen can create a temporary variable in the
+     current context which will be part of string_length. If we share
+     the resulting gfc_charlen structure with a variable in a different
+     declaration context, we could trip the assertion in
+     expand_expr_real_1 when it sees that the temporary has been
+     created in one context and referenced in another:
 
+     if (exp)
+     context = decl_function_context (exp);
+     gcc_assert (!exp
+         || SCOPE_FILE_SCOPE_P (context)
+         || context == current_function_decl
+         || TREE_STATIC (exp)
+         || DECL_EXTERNAL (exp)
+         // ??? C++ creates functions that are not TREE_STATIC.
+         || TREE_CODE (exp) == FUNCTION_DECL);
+
+     So we create a new gfc_charlen structure and link it into what
+     looks like the current namespace.
+
+     TODO: Can we do this only when get_array_ctor_strlen has been
+     called? Does it matter? Are we using the right namespace (and
+     does it matter, as long as the gfc_charlen structure is cleaned
+     up)?
+ */
+
+ new_cl = gfc_new_charlen (gfc_current_ns, expr->ts.u.cl);
+ new_cl->backend_decl = ss_info->string_length;
+ expr->ts.u.cl = new_cl;
+
 type = gfc_get_character_type_len (expr->ts.kind, ss_info->string_length);
 if (const_string)
     type = build_pointer_type (type);
@@ -2589,7 +2620,8 @@ gfc_add_loop_ss_code (gfc_loopinfo * loop, gfc_ss
      if (expr->ts.type == BT_CHARACTER
      && ss_info->string_length == NULL
      && expr->ts.u.cl
-     && expr->ts.u.cl->length)
+     && expr->ts.u.cl->length
+     && expr->ts.u.cl->length->expr_type == EXPR_CONSTANT)
      {
      gfc_init_se (&se, NULL);
      gfc_conv_expr_type (&se, expr->ts.u.cl->length,


[-- Attachment #2: string_array_constructor_1.f90 --]
[-- Type: application/octet-stream, Size: 763 bytes --]

! { dg-do compile }
! PR 62242
! Array constructor with an array element whose value is a
! character function that is described in an interface block and which
! has an assumed-length result
module gfbug
    implicit none
    INTERFACE
      function UpperCase(string) result(upper) 
          character(*), intent(IN) :: string
          character(LEN(string)) :: upper
      end function
      function f2(string) result(upper) 
          character(*), intent(IN) :: string
          character(5) :: upper
      end function
    END INTERFACE
contains
    subroutine s1
        character(5) c
        character(5), dimension(1) :: ca
        ca = (/f2(c)/)  ! This compiles
        ca = (/Uppercase(c)/) ! This gets an ICE
    end subroutine
end module gfbug


[-- Attachment #3: string_array_constructor_2.f90 --]
[-- Type: application/octet-stream, Size: 1272 bytes --]

! { dg-do run }
! PR 62242
! Array constructor with an array element whose value is a
! character function that is described in an interface block and which
! has an assumed-length result
module gfbug
    implicit none
    INTERFACE
      function UpperCase(string) result(upper) 
          character(*), intent(IN) :: string
          character(LEN(string)) :: upper
      end function
      function f2(string) result(upper) 
          character(*), intent(IN) :: string
          character(5) :: upper
      end function
    END INTERFACE
contains
    subroutine s1
        character(5) c
        character(5), dimension(1) :: ca
        character(5), dimension(1) :: cb
        c = "12345"
        ca = (/f2(c)/) ! This works
        !print *, ca(1)
        cb = (/Uppercase(c)/) ! This gets an ICE
        if (ca(1) .ne. cb(1)) then
            call abort()
        end if
        !print *, ca(1)
    end subroutine
end module gfbug

function UpperCase(string) result(upper) 
    character(*), intent(IN) :: string
    character(LEN(string)) :: upper
    upper = string
end function
function f2(string) result(upper) 
    character(*), intent(IN) :: string
    character(5) :: upper
    upper = string
end function

program main
    use gfbug
    call s1
end program

[-- Attachment #4: string_array_constructor_3.f90 --]
[-- Type: application/octet-stream, Size: 614 bytes --]

! { dg-do compile }
! PR 62242
! A subprogram calling an array constructor with an array element whose
! value is the result of calling a character function with both an
! assumed-length argument and an assumed-length result
module gfbug
    implicit none
contains
    function inner(inner_str) result(upper)
        character(*), intent(IN) :: inner_str
        character(LEN(inner_str)) :: upper

        upper = '123'
    end function

    subroutine outer(outer_str)
        character(*), intent(IN) :: outer_str
        character(5) :: z(1)

        z = [inner(outer_str)]
    end subroutine
end module gfbug

  reply	other threads:[~2015-09-15  5:58 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-09-09  7:37 Possible patch for pr62242 Louis Krupp
2015-09-15  6:39 ` Louis Krupp [this message]
2015-09-15 13:41 Possible patch for pr62242 -- follow-up Dominique d'Humières

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=14fcf9597a8.102dd3e69123968.6989139803106041754@zoho.com \
    --to=louis.krupp@zoho.com \
    --cc=fortran@gcc.gnu.org \
    --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).