[PATCH, fortran] character array and constant substrings

David Edelsohn dje@watson.ibm.com
Fri Jul 22 20:00:00 GMT 2005


	Appended is a revised patch and this message is a request for
approval of the patch.  The patch includes both the character array and
constant substring patches, and incorporates the feedback received.  The
constant substring patch now NULL-terminates the new string.

	PR fortran/22491
	* expr.c (simplify_parameter_variable): Do not copy the subobject
	references if the expression value is a constant.

	* expr.c (gfc_simplify_expr): Evaluate constant substrings.

	New testcases appended as well.  Should I create a PR for the
constant substring issue so that it can be tracked?

Bootstrapped and regression tested on powerpc-ibm-aix5.2.0.0 with no
regressions. 

	Okay for mainline?

Thanks, David


Index: expr.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/fortran/expr.c,v
retrieving revision 1.27
diff -c -p -r1.27 expr.c
*** expr.c	7 Jul 2005 07:54:41 -0000	1.27
--- expr.c	22 Jul 2005 17:21:10 -0000
*************** simplify_parameter_variable (gfc_expr * 
*** 1068,1074 ****
    try t;
  
    e = gfc_copy_expr (p->symtree->n.sym->value);
!   if (p->ref)
      e->ref = copy_ref (p->ref);
    t = gfc_simplify_expr (e, type);
  
--- 1068,1075 ----
    try t;
  
    e = gfc_copy_expr (p->symtree->n.sym->value);
!   /* Do not copy subobject refs for constant.  */
!   if (e->expr_type != EXPR_CONSTANT && p->ref != NULL)
      e->ref = copy_ref (p->ref);
    t = gfc_simplify_expr (e, type);
  
*************** gfc_simplify_expr (gfc_expr * p, int typ
*** 1130,1136 ****
        if (simplify_ref_chain (p->ref, type) == FAILURE)
  	return FAILURE;
  
!       /* TODO: evaluate constant substrings.  */
        break;
  
      case EXPR_OP:
--- 1130,1157 ----
        if (simplify_ref_chain (p->ref, type) == FAILURE)
  	return FAILURE;
  
!       if (gfc_is_constant_expr (p))
! 	{
! 	  char *s;
! 	  int start, end;
! 
! 	  gfc_extract_int (p->ref->u.ss.start, &start);
! 	  start--;  /* Convert from one-based to zero-based.  */
! 	  gfc_extract_int (p->ref->u.ss.end, &end);
! 	  s = gfc_getmem (end - start + 1);
! 	  memcpy (s, p->value.character.string + start, end - start);
! 	  s[end] = '\0';  /* TODO: C-style string for debugging.  */
! 	  gfc_free (p->value.character.string);
! 	  p->value.character.string = s;
! 	  p->value.character.length = end - start;
! 	  p->ts.cl = gfc_get_charlen ();
! 	  p->ts.cl->next = gfc_current_ns->cl_list;
! 	  gfc_current_ns->cl_list = p->ts.cl;
! 	  p->ts.cl->length = gfc_int_expr (p->value.character.length);
! 	  gfc_free_ref_list (p->ref);
! 	  p->ref = NULL;
! 	  p->expr_type = EXPR_CONSTANT;
! 	}
        break;
  
      case EXPR_OP:


! PR fortran/22491
! {do-do run}
      character*2 a (1)
      character*4 b (1)
      character*6 c
      parameter (a="12")
      parameter (b = a)
      write (c,'("#",A,"#")') b
      if (c .ne. '#12  #') call abort
      end

! constant substring
! {do-do run}
      character*2 a
      character*4 b
      character*6 c
      parameter (a="12")
      parameter (b = a(1:2))
      write (c,'("#",A,"#")') b
      if (c .ne. '#12  #') call abort
      end



More information about the Gcc-patches mailing list