diff options
author | Andre Vehreschild <vehre@gcc.gnu.org> | 2023-07-19 11:57:43 +0200 |
---|---|---|
committer | Andre Vehreschild <vehre@gcc.gnu.org> | 2024-06-07 09:40:17 +0200 |
commit | 51046e46ae66ca95bf2b93ae60f0c4d6b338f8af (patch) | |
tree | 911f8c0f57378cee7e51767f3ca59f5eb204b6b2 /gcc/fortran | |
parent | 3472b5749df53b91bcb00a3e82cc85ef1f3b17ce (diff) | |
download | gcc-51046e46ae66ca95bf2b93ae60f0c4d6b338f8af.zip gcc-51046e46ae66ca95bf2b93ae60f0c4d6b338f8af.tar.gz gcc-51046e46ae66ca95bf2b93ae60f0c4d6b338f8af.tar.bz2 |
Fix returned type to be allocatable for user-functions.
The returned type of user-defined function returning a
class object was not detected and handled correctly, which
lead to memory leaks.
PR fortran/90072
gcc/fortran/ChangeLog:
* expr.cc (gfc_is_alloc_class_scalar_function): Detect
allocatable class return types also for user-defined
functions.
* trans-expr.cc (gfc_conv_procedure_call): Same.
(trans_class_vptr_len_assignment): Compute vptr len
assignment correctly for user-defined functions.
gcc/testsuite/ChangeLog:
* gfortran.dg/class_77.f90: New test.
Diffstat (limited to 'gcc/fortran')
-rw-r--r-- | gcc/fortran/expr.cc | 13 | ||||
-rw-r--r-- | gcc/fortran/trans-expr.cc | 35 |
2 files changed, 26 insertions, 22 deletions
diff --git a/gcc/fortran/expr.cc b/gcc/fortran/expr.cc index a162744..be138d1 100644 --- a/gcc/fortran/expr.cc +++ b/gcc/fortran/expr.cc @@ -5573,11 +5573,14 @@ bool gfc_is_alloc_class_scalar_function (gfc_expr *expr) { if (expr->expr_type == EXPR_FUNCTION - && expr->value.function.esym - && expr->value.function.esym->result - && expr->value.function.esym->result->ts.type == BT_CLASS - && !CLASS_DATA (expr->value.function.esym->result)->attr.dimension - && CLASS_DATA (expr->value.function.esym->result)->attr.allocatable) + && ((expr->value.function.esym + && expr->value.function.esym->result + && expr->value.function.esym->result->ts.type == BT_CLASS + && !CLASS_DATA (expr->value.function.esym->result)->attr.dimension + && CLASS_DATA (expr->value.function.esym->result)->attr.allocatable) + || (expr->ts.type == BT_CLASS + && CLASS_DATA (expr)->attr.allocatable + && !CLASS_DATA (expr)->attr.dimension))) return true; return false; diff --git a/gcc/fortran/trans-expr.cc b/gcc/fortran/trans-expr.cc index 9f6cc8f..d6f4d6b 100644 --- a/gcc/fortran/trans-expr.cc +++ b/gcc/fortran/trans-expr.cc @@ -8301,7 +8301,9 @@ gfc_conv_procedure_call (gfc_se * se, gfc_symbol * sym, } /* Finalize the result, if necessary. */ - attr = CLASS_DATA (expr->value.function.esym->result)->attr; + attr = expr->value.function.esym + ? CLASS_DATA (expr->value.function.esym->result)->attr + : CLASS_DATA (expr)->attr; if (!((gfc_is_class_array_function (expr) || gfc_is_alloc_class_scalar_function (expr)) && attr.pointer)) @@ -10085,27 +10087,26 @@ trans_class_vptr_len_assignment (stmtblock_t *block, gfc_expr * le, if (re->expr_type != EXPR_VARIABLE && re->expr_type != EXPR_NULL && rse->expr != NULL_TREE) { - if (re->ts.type == BT_CLASS && !GFC_CLASS_TYPE_P (TREE_TYPE (rse->expr))) - class_expr = gfc_get_class_from_expr (rse->expr); + if (!DECL_P (rse->expr)) + { + if (re->ts.type == BT_CLASS && !GFC_CLASS_TYPE_P (TREE_TYPE (rse->expr))) + class_expr = gfc_get_class_from_expr (rse->expr); - if (rse->loop) - pre = &rse->loop->pre; - else - pre = &rse->pre; + if (rse->loop) + pre = &rse->loop->pre; + else + pre = &rse->pre; - if (class_expr != NULL_TREE && UNLIMITED_POLY (re)) - { - tmp = TREE_OPERAND (rse->expr, 0); - tmp = gfc_create_var (TREE_TYPE (tmp), "rhs"); - gfc_add_modify (&rse->pre, tmp, TREE_OPERAND (rse->expr, 0)); + if (class_expr != NULL_TREE && UNLIMITED_POLY (re)) + tmp = gfc_evaluate_now (TREE_OPERAND (rse->expr, 0), &rse->pre); + else + tmp = gfc_evaluate_now (rse->expr, &rse->pre); + + rse->expr = tmp; } else - { - tmp = gfc_create_var (TREE_TYPE (rse->expr), "rhs"); - gfc_add_modify (&rse->pre, tmp, rse->expr); - } + pre = &rse->pre; - rse->expr = tmp; temp_rhs = true; } |