Bug 38936

Summary: [F03] ASSOCIATE construct / improved SELECT TYPE (a=>expr)
Product: gcc Reporter: Tobias Burnus <burnus>
Component: fortranAssignee: Daniel Kraft <domob>
Status: RESOLVED FIXED    
Severity: enhancement CC: domob, gcc-bugs, janus, norm.clerman, pault
Priority: P3    
Version: 4.4.0   
Target Milestone: ---   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed: 2010-05-08 13:24:00
Bug Depends on:    
Bug Blocks: 20585, 87477, 44044    

Description Tobias Burnus 2009-01-22 14:00:10 UTC
ASSOCIATE is a nice Fortran 2003 feature. I found the following at comp.lang.fortran and I want to make sure it gets tested when ASSOCIATE is implemented. Thus I opened this PR.
http://groups.google.com/group/comp.lang.fortran/browse_thread/thread/ebf1e5abedc91cdc

Test case:

implicit none
integer :: x(5), y(5), a(5), i
 x = [13.0, 23.0, 27.0, 0.0, 37.0 ]
 y = [1.0, 3.0, 5.0, 7.0, 11.0 ]
 a = 0.0
ASSOCIATE (z => x + y)
  do i = 1, 5
     a(i) = z(i)*3.0
  end do
END ASSOCIATE
print *, a
end

Using ifort 11, the result is:
42          78          96          21         144
Comment 1 Daniel Kraft 2009-01-22 17:27:02 UTC
I always liked the idea of associate...  Maybe I'll volunteer to work on it for gfortran, but don't take my word on it ;)  And of course, things like CLASS get higher priority.
Comment 2 Tobias Burnus 2009-10-09 12:08:09 UTC
  SELECT TYPE (name => expr)
and
  ASSOCIATE (name1 => expr1, name2 => expr2)
have similar syntax and issues. SELECT TYPE (name=>expr) is already implemented (via creating a new pointer variable), but it has some issues.

The idea is to replace the expression - at least when it is definable - by the variable (be it in resolve.c or in trans*.c).
Cf. also http://gcc.gnu.org/ml/fortran/2009-10/msg00075.html

Maybe one should do something alike for TYPE IS() and (when implemented) CLASS IS(), cf. http://gcc.gnu.org/ml/fortran/2009-10/msg00075.html
Comment 3 Daniel Kraft 2010-05-08 13:24:00 UTC
Taking this finally.
Comment 4 Daniel Kraft 2010-06-10 14:48:16 UTC
Subject: Bug 38936

Author: domob
Date: Thu Jun 10 14:47:49 2010
New Revision: 160550

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=160550
Log:
2010-06-10  Daniel Kraft  <d@domob.eu>

	PR fortran/38936
	* gfortran.h (enum gfc_statement): Add ST_ASSOCIATE, ST_END_ASSOCIATE.
	(struct gfc_symbol): New field `assoc'.
	(struct gfc_association_list): New struct.
	(struct gfc_code): New struct `block' in union, move `ns' there
	and add association list.
	(gfc_free_association_list): New method.
	(gfc_has_vector_subscript): Made public;
	* match.h (gfc_match_associate): New method.
	* parse.h (enum gfc_compile_state): Add COMP_ASSOCIATE.
	* decl.c (gfc_match_end): Handle ST_END_ASSOCIATE.
	* interface.c (gfc_has_vector_subscript): Made public.
	(compare_actual_formal): Rename `has_vector_subscript' accordingly.
	* match.c (gfc_match_associate): New method.
	(gfc_match_select_type): Change reference to gfc_code's `ns' field.
	* primary.c (match_variable): Don't allow names associated to expr here.
	* parse.c (decode_statement): Try matching ASSOCIATE statement.
	(case_exec_markers, case_end): Add ASSOCIATE statement.
	(gfc_ascii_statement): Hande ST_ASSOCIATE and ST_END_ASSOCIATE.
	(parse_associate): New method.
	(parse_executable): Handle ST_ASSOCIATE.
	(parse_block_construct): Change reference to gfc_code's `ns' field.
	* resolve.c (resolve_select_type): Ditto.
	(resolve_code): Ditto.
	(resolve_block_construct): Ditto and add comment.
	(resolve_select_type): Set association list in generated BLOCK to NULL.
	(resolve_symbol): Resolve associate names.
	* st.c (gfc_free_statement): Change reference to gfc_code's `ns' field
	and free association list.
	(gfc_free_association_list): New method.
	* symbol.c (gfc_new_symbol): NULL new field `assoc'.
	* trans-stmt.c (gfc_trans_block_construct): Change reference to
	gfc_code's `ns' field.

2010-06-10  Daniel Kraft  <d@domob.eu>

	PR fortran/38936
	* gfortran.dg/associate_1.f03: New test.
	* gfortran.dg/associate_2.f95: New test.
	* gfortran.dg/associate_3.f03: New test.
	* gfortran.dg/associate_4.f08: New test.

Added:
    trunk/gcc/testsuite/gfortran.dg/associate_1.f03
    trunk/gcc/testsuite/gfortran.dg/associate_2.f95
    trunk/gcc/testsuite/gfortran.dg/associate_3.f03
    trunk/gcc/testsuite/gfortran.dg/associate_4.f08
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/decl.c
    trunk/gcc/fortran/gfortran.h
    trunk/gcc/fortran/interface.c
    trunk/gcc/fortran/match.c
    trunk/gcc/fortran/match.h
    trunk/gcc/fortran/parse.c
    trunk/gcc/fortran/parse.h
    trunk/gcc/fortran/primary.c
    trunk/gcc/fortran/resolve.c
    trunk/gcc/fortran/st.c
    trunk/gcc/fortran/symbol.c
    trunk/gcc/fortran/trans-stmt.c
    trunk/gcc/testsuite/ChangeLog

Comment 5 Daniel Kraft 2010-06-10 16:50:37 UTC
This first commit implements association to scalar expressions as a first step.  More to follow.
Comment 6 Daniel Kraft 2010-08-15 19:46:42 UTC
Subject: Bug 38936

Author: domob
Date: Sun Aug 15 19:46:21 2010
New Revision: 163268

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=163268
Log:
2010-08-15  Daniel Kraft  <d@domob.eu>

	PR fortran/38936
	* gfortran.h (gfc_find_proc_namespace): New method.
	* expr.c (gfc_build_intrinsic_call): No need to build symtree messing
	around with namespace.
	* symbol.c (gfc_find_proc_namespace): New method.
	* trans-decl.c (gfc_build_qualified_array): Use it for correct
	value of nest.
	* primary.c (gfc_match_varspec): Handle associate-names as arrays.
	* parse.c (parse_associate): Removed assignment-generation here...
	* resolve.c (resolve_block_construct): ...and added it here.
	(resolve_variable): Handle names that are arrays but were not parsed
	as such because of association.
	(resolve_code): Fix BLOCK resolution.
	(resolve_symbol): Generate array-spec for associate-names.

2010-08-15  Daniel Kraft  <d@domob.eu>

	PR fortran/38936
	* gfortran.dg/associate_1.f03: Enable test for array expressions.
	* gfortran.dg/associate_3.f03: Clarify comment.
	* gfortran.dg/associate_5.f03: New test.
	* gfortran.dg/associate_6.f03: New test.

Added:
    trunk/gcc/testsuite/gfortran.dg/associate_5.f03
    trunk/gcc/testsuite/gfortran.dg/associate_6.f03
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/expr.c
    trunk/gcc/fortran/gfortran.h
    trunk/gcc/fortran/parse.c
    trunk/gcc/fortran/primary.c
    trunk/gcc/fortran/resolve.c
    trunk/gcc/fortran/symbol.c
    trunk/gcc/fortran/trans-decl.c
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/gfortran.dg/associate_1.f03
    trunk/gcc/testsuite/gfortran.dg/associate_3.f03

Comment 7 Daniel Kraft 2010-08-15 20:25:25 UTC
This extended the support to array-expressions -- the original example works now.  Next will be a rework to do the association in the trans phase, which is probably necessary to get full array support and association to variables working.
Comment 8 Daniel Kraft 2010-08-16 08:59:27 UTC
Also one thing to note is that associate-names associated to expressions (which must not appear in variable definition contexts) are currently accepted as actual arguments to INTENT([IN]OUT) arguments.  This should probably be checked and rejected.
Comment 9 Daniel Kraft 2010-08-17 08:20:20 UTC
Subject: Bug 38936

Author: domob
Date: Tue Aug 17 08:20:03 2010
New Revision: 163295

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=163295
Log:
2010-08-17  Daniel Kraft  <d@domob.eu>

	PR fortran/38936
	* gfortran.h (struct gfc_association_list): New member `where'.
	(gfc_is_associate_pointer) New method.
	* match.c (gfc_match_associate): Remember locus for each associate
	name matched and do not try to set variable flag.
	* parse.c (parse_associate): Use remembered locus for symbols.
	* primary.c (match_variable): Instead of variable-flag check for
	associate names set it for all such names used.
	* symbol.c (gfc_is_associate_pointer): New method.
	* resolve.c (resolve_block_construct): Don't generate assignments
	to give associate-names their values.
	(resolve_fl_var_and_proc): Allow associate-names to be deferred-shape.
	(resolve_symbol): Set some more attributes for associate variables,
	set variable flag here and check it and don't try to build an
	explicitely shaped array-spec for array associate variables.
	* trans-expr.c (gfc_conv_variable): Dereference in case of association
	to scalar variable.
	* trans-types.c (gfc_is_nodesc_array): Handle array association symbols.
	(gfc_sym_type): Return pointer type for association to scalar vars.
	* trans-decl.c (gfc_get_symbol_decl): Defer association symbols.
	(trans_associate_var): New method.
	(gfc_trans_deferred_vars): Handle association symbols.

2010-08-17  Daniel Kraft  <d@domob.eu>

	PR fortran/38936
	* gfortran.dg/associate_1.f03: Extended to test newly supported
	features like association to variables.
	* gfortran.dg/associate_3.f03: Removed check for illegal change
	of associate-name here...
	* gfortran.dg/associate_5.f03: ...and added it here.
	* gfortran.dg/associate_6.f03: No longer XFAIL'ed.
	* gfortran.dg/associate_7.f03: New test.

Added:
    trunk/gcc/testsuite/gfortran.dg/associate_7.f03
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/gfortran.h
    trunk/gcc/fortran/match.c
    trunk/gcc/fortran/parse.c
    trunk/gcc/fortran/primary.c
    trunk/gcc/fortran/resolve.c
    trunk/gcc/fortran/symbol.c
    trunk/gcc/fortran/trans-decl.c
    trunk/gcc/fortran/trans-expr.c
    trunk/gcc/fortran/trans-types.c
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/gfortran.dg/associate_1.f03
    trunk/gcc/testsuite/gfortran.dg/associate_3.f03
    trunk/gcc/testsuite/gfortran.dg/associate_5.f03
    trunk/gcc/testsuite/gfortran.dg/associate_6.f03

Comment 10 Daniel Kraft 2010-08-17 08:23:55 UTC
This implements association to variables and removes the problems that were still there with array expressions and bounds.  What's still missing is:

* Association to polymorphic entities
* Association to strings (works partially, but has a lot of problems)
* Association to derived types (should work in principle, but the parser does not like component references on the associate-names)
* More checks for what is and is not a variable definition context and which associate names may appear in it
Comment 11 Daniel Kraft 2010-08-21 16:57:19 UTC
*** Bug 45369 has been marked as a duplicate of this bug. ***
Comment 12 Daniel Kraft 2010-08-21 16:58:58 UTC
See also PR 45369 which has a rather complicated test-case for association to derived-types (as is currently not yet working).
Comment 13 Daniel Kraft 2010-08-26 19:49:31 UTC
Subject: Bug 38936

Author: domob
Date: Thu Aug 26 19:48:43 2010
New Revision: 163572

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=163572
Log:
2010-08-26  Daniel Kraft  <d@domob.eu>

	PR fortran/38936
	PR fortran/44047
	PR fortran/45384
	* gfortran.h (struct gfc_association_list): New flag `dangling'.
	(gfc_build_block_ns): Declared here...
	* parse.h (gfc_build_block_ns): ...instead of here.
	* trans.h (gfc_process_block_locals): Expect additionally the
	gfc_association_list of BLOCK (if present).
	* match.c (select_type_set_tmp): Create sym->assoc for temporary.
	* resolve.c (resolve_variable): Only check for invalid *array*
	references on associate-names.
	(resolve_assoc_var): New method with code previously in resolve_symbol.
	(resolve_select_type): Use association to give the selector and
	temporaries their values instead of ordinary assignment.
	(resolve_fl_var_and_proc): Allow CLASS associate-names.
	(resolve_symbol): Use new `resolve_assoc_var' instead of inlining here.
	* trans-stmt.c (gfc_trans_block_construct): Pass association-list
	to `gfc_process_block_locals' to match new interface.
	* trans-decl.c (gfc_get_symbol_decl): Don't defer associate-names
	here automatically.
	(gfc_process_block_locals): Defer them rather here when linked to
	from the BLOCK's association list.

2010-08-26  Daniel Kraft  <d@domob.eu>

	PR fortran/38936
	PR fortran/44047
	PR fortran/45384
	* gfortran.dg/associate_8.f03: New test.
	* gfortran.dg/select_type_13.f03: New test.
	* gfortran.dg/select_type_14.f03: New test.

Added:
    trunk/gcc/testsuite/gfortran.dg/associate_8.f03
    trunk/gcc/testsuite/gfortran.dg/select_type_13.f03
    trunk/gcc/testsuite/gfortran.dg/select_type_14.f03
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/gfortran.h
    trunk/gcc/fortran/match.c
    trunk/gcc/fortran/parse.h
    trunk/gcc/fortran/resolve.c
    trunk/gcc/fortran/trans-decl.c
    trunk/gcc/fortran/trans-stmt.c
    trunk/gcc/fortran/trans.h
    trunk/gcc/testsuite/ChangeLog

Comment 14 Daniel Kraft 2010-08-26 19:54:09 UTC
This fixed association to CLASS entities and also reworked SELECT TYPE to use the same workings internally.  Still open:  Association to strings and derived-types, and the extended checks for illegal usage of names associated to expressions.
Comment 15 Daniel Kraft 2010-09-23 08:38:22 UTC
Subject: Bug 38936

Author: domob
Date: Thu Sep 23 08:37:54 2010
New Revision: 164550

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=164550
Log:
2010-09-23  Daniel Kraft  <d@domob.eu>

	PR fortran/38936
	PR fortran/44044
	PR fortran/45474
	* gfortran.h (gfc_check_vardef_context): New method.
	(struct symbol_attribute): New flag `select_type_temporary'.
	* primary.c (gfc_variable_attr): Clarify initialization of ref.
	(match_variable): Remove PROTECTED check and assignment check
	for PARAMETERs (this is now done later).
	* match.c (gfc_match_iterator): Remove INTENT(IN) check.
	(gfc_match_associate): Defer initialization of newAssoc->variable.
	(gfc_match_nullify): Remove PURE definability check.
	(select_type_set_tmp): Set new `select_type_temporary' flag.
	* expr.c (gfc_check_assign): Remove INTENT(IN) check here.
	(gfc_check_pointer_assign): Ditto (and other checks removed).
	(gfc_check_vardef_context): New method.
	* interface.c (compare_parameter_protected): Removed.
	(compare_actual_formal): Use `gfc_check_vardef_context' for checks
	related to INTENT([IN]OUT) arguments.
	* intrinsic.c (check_arglist): Check INTENT for intrinsics.
	* resolve.c (gfc_resolve_iterator): Use `gfc_check_vardef_context'.
	(remove_last_array_ref): New method.
	(resolve_deallocate_expr), (resolve_allocate_expr): Ditto.
	(resolve_allocate_deallocate): Ditto (for STAT and ERRMSG).
	(resolve_assoc_var): Remove checks for definability here.
	(resolve_select_type): Handle resolving of code->block here.
	(resolve_ordinary_assign): Remove PURE check.
	(resolve_code): Do not resolve code->blocks for SELECT TYPE here.
	Use `gfc_check_vardef_context' for assignments and pointer-assignments.

2010-09-23  Daniel Kraft  <d@domob.eu>

	PR fortran/38936
	PR fortran/44044
	PR fortran/45474
	* gfortran.dg/intrinsic_intent_1.f03: New test.
	* gfortran.dg/select_type_17.f03: New test.
	* gfortran.dg/associate_5.f03: More definability tests.
	* gfortran.dg/enum_2.f90: Check definability.
	* gfortran.dg/allocatable_dummy_2.f90: Change expected error message.
	* gfortran.dg/allocate_alloc_opt_2.f90: Ditto.
	* gfortran.dg/char_expr_2.f90: Ditto.
	* gfortran.dg/deallocate_alloc_opt_2.f90: Ditto.
	* gfortran.dg/enum_5.f90: Ditto.
	* gfortran.dg/equiv_constraint_8.f90: Ditto.
	* gfortran.dg/impure_assignment_2.f90: Ditto.
	* gfortran.dg/impure_assignment_3.f90: Ditto.
	* gfortran.dg/intent_out_1.f90: Ditto.
	* gfortran.dg/intent_out_3.f90: Ditto.
	* gfortran.dg/pointer_assign_7.f90: Ditto.
	* gfortran.dg/pointer_intent_3.f90: Ditto.
	* gfortran.dg/pr19936_1.f90: Ditto.
	* gfortran.dg/proc_ptr_comp_3.f90: Ditto.
	* gfortran.dg/simpleif_2.f90: Ditto.
	* gfortran.dg/protected_5.f90: Ditto.
	* gfortran.dg/protected_4.f90: Ditto and remove invalid error check.
	* gfortran.dg/protected_6.f90: Ditto.
	* gfortran.dg/protected_7.f90: Ditto.

Added:
    trunk/gcc/testsuite/gfortran.dg/intrinsic_intent_1.f03
    trunk/gcc/testsuite/gfortran.dg/select_type_17.f03
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/expr.c
    trunk/gcc/fortran/gfortran.h
    trunk/gcc/fortran/interface.c
    trunk/gcc/fortran/intrinsic.c
    trunk/gcc/fortran/match.c
    trunk/gcc/fortran/primary.c
    trunk/gcc/fortran/resolve.c
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/gfortran.dg/allocatable_dummy_2.f90
    trunk/gcc/testsuite/gfortran.dg/allocate_alloc_opt_2.f90
    trunk/gcc/testsuite/gfortran.dg/associate_5.f03
    trunk/gcc/testsuite/gfortran.dg/char_expr_2.f90
    trunk/gcc/testsuite/gfortran.dg/deallocate_alloc_opt_2.f90
    trunk/gcc/testsuite/gfortran.dg/enum_2.f90
    trunk/gcc/testsuite/gfortran.dg/enum_5.f90
    trunk/gcc/testsuite/gfortran.dg/equiv_constraint_8.f90
    trunk/gcc/testsuite/gfortran.dg/impure_assignment_2.f90
    trunk/gcc/testsuite/gfortran.dg/impure_assignment_3.f90
    trunk/gcc/testsuite/gfortran.dg/intent_out_1.f90
    trunk/gcc/testsuite/gfortran.dg/intent_out_3.f90
    trunk/gcc/testsuite/gfortran.dg/pointer_assign_7.f90
    trunk/gcc/testsuite/gfortran.dg/pointer_intent_3.f90
    trunk/gcc/testsuite/gfortran.dg/pr19936_1.f90
    trunk/gcc/testsuite/gfortran.dg/proc_ptr_comp_3.f90
    trunk/gcc/testsuite/gfortran.dg/protected_4.f90
    trunk/gcc/testsuite/gfortran.dg/protected_5.f90
    trunk/gcc/testsuite/gfortran.dg/protected_6.f90
    trunk/gcc/testsuite/gfortran.dg/protected_7.f90
    trunk/gcc/testsuite/gfortran.dg/simpleif_2.f90

Comment 16 Daniel Kraft 2010-09-24 08:10:38 UTC
The last commit partially implemented the missing definability checks also for ASSOCIATE names.  For the missing pieces, I opened PR 45776.

So here remains the missing pieces for ASSOCIATE to strings and derived-types.
Comment 17 Daniel Kraft 2010-10-12 13:30:57 UTC
Author: domob
Date: Tue Oct 12 13:30:53 2010
New Revision: 165378

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=165378
Log:
2010-10-12  Daniel Kraft  <d@domob.eu>

	PR fortran/38936
	* parse.c (parse_associate): Set typespec of associate-name if that of
	the target is already available.

2010-10-12  Daniel Kraft  <d@domob.eu>

	PR fortran/38936
	* gfortran.dg/associate_1.f03: More tests with derived-types.
	* gfortran.dg/associate_9.f03: New test (XFAIL for now).
	* gfortran.dg/associate_8.f03: Fix typo.
	* gfortran.dg/initialization_27.f90: Fix typo.

Added:
    trunk/gcc/testsuite/gfortran.dg/associate_9.f03
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/parse.c
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/gfortran.dg/associate_1.f03
    trunk/gcc/testsuite/gfortran.dg/associate_8.f03
    trunk/gcc/testsuite/gfortran.dg/initialization_27.f90
Comment 18 Daniel Kraft 2010-10-12 13:37:44 UTC
This commit did implement better handling for association to derived-types, but some cases are still not handled (see the XFAIL of associate_9.f03).  I wanted to test with the code of PR 45369, but that also uses CLASS(*) so I was not able to compile it still.

See http://gcc.gnu.org/ml/fortran/2010-10/msg00126.html for some discussion about what's still missing for derived-types, and in addition there's still proper handling of strings open (as in the commented-out section of associate_1.f03).
Comment 19 Dominique d'Humieres 2017-09-25 16:10:06 UTC
> This commit did implement better handling for association to derived-types,
> but some cases are still not handled (see the XFAIL of associate_9.f03). 
> I wanted to test with the code of PR 45369, but that also uses CLASS(*) so
> I was not able to compile it still.

The XFAIL has been removed at revision r252894. Is there anything left in this PR or could it be closed?
Comment 20 Jürgen Reuter 2018-10-01 08:46:31 UTC
Repeating Dominique's question: this seems complete (or almost complete). Is there anything left? This is one of the last three non-green items in 
https://gcc.gnu.org/wiki/Fortran2003Status
Comment 21 Paul Thomas 2018-10-01 11:49:13 UTC
Now replaced with the meta-bug set up by Janus.

Paul