| 1 |
/* Array things |
| 2 |
Copyright (C) 2000-2025 Free Software Foundation, Inc. |
| 3 |
Contributed by Andy Vaught |
| 4 |
|
| 5 |
This file is part of GCC. |
| 6 |
|
| 7 |
GCC is free software; you can redistribute it and/or modify it under |
| 8 |
the terms of the GNU General Public License as published by the Free |
| 9 |
Software Foundation; either version 3, or (at your option) any later |
| 10 |
version. |
| 11 |
|
| 12 |
GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
| 13 |
WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 14 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 15 |
for more details. |
| 16 |
|
| 17 |
You should have received a copy of the GNU General Public License |
| 18 |
along with GCC; see the file COPYING3. If not see |
| 19 |
<http://www.gnu.org/licenses/>. */ |
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
#include "system.h" |
| 23 |
#include "coretypes.h" |
| 24 |
#include "options.h" |
| 25 |
#include "gfortran.h" |
| 26 |
#include "parse.h" |
| 27 |
#include "match.h" |
| 28 |
#include "constructor.h" |
| 29 |
|
| 30 |
/**************** Array reference matching subroutines *****************/ |
| 31 |
|
| 32 |
/* Copy an array reference structure. */ |
| 33 |
|
| 34 |
gfc_array_ref * |
| 35 |
gfc_copy_array_ref (gfc_array_ref *src) |
| 36 |
{ |
| 37 |
gfc_array_ref *dest; |
| 38 |
int i; |
| 39 |
|
| 40 |
if (src == NULL) |
| 41 |
return NULL; |
| 42 |
|
| 43 |
dest = gfc_get_array_ref (); |
| 44 |
|
| 45 |
*dest = *src; |
| 46 |
|
| 47 |
for (i = 0; i < GFC_MAX_DIMENSIONS; i++) |
| 48 |
{ |
| 49 |
dest->start[i] = gfc_copy_expr (src->start[i]); |
| 50 |
dest->end[i] = gfc_copy_expr (src->end[i]); |
| 51 |
dest->stride[i] = gfc_copy_expr (src->stride[i]); |
| 52 |
} |
| 53 |
|
| 54 |
dest->stat = gfc_copy_expr (src->stat); |
| 55 |
dest->team = gfc_copy_expr (src->team); |
| 56 |
|
| 57 |
return dest; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
/* Match a single dimension of an array reference. This can be a |
| 62 |
single element or an array section. Any modifications we've made |
| 63 |
to the ar structure are cleaned up by the caller. If the init |
| 64 |
is set, we require the subscript to be a valid initialization |
| 65 |
expression. */ |
| 66 |
|
| 67 |
static match |
| 68 |
match_subscript (gfc_array_ref *ar, int init, bool match_star) |
| 69 |
{ |
| 70 |
match m = MATCH_ERROR; |
| 71 |
bool star = false; |
| 72 |
int i; |
| 73 |
bool saw_boz = false; |
| 74 |
|
| 75 |
i = ar->dimen + ar->codimen; |
| 76 |
|
| 77 |
gfc_gobble_whitespace (); |
| 78 |
ar->c_where[i] = gfc_current_locus; |
| 79 |
ar->start[i] = ar->end[i] = ar->stride[i] = NULL; |
| 80 |
|
| 81 |
/* We can't be sure of the difference between DIMEN_ELEMENT and |
| 82 |
DIMEN_VECTOR until we know the type of the element itself at |
| 83 |
resolution time. */ |
| 84 |
|
| 85 |
ar->dimen_type[i] = DIMEN_UNKNOWN; |
| 86 |
|
| 87 |
if (gfc_match_char (':') == MATCH_YES) |
| 88 |
goto end_element; |
| 89 |
|
| 90 |
/* Get start element. */ |
| 91 |
if (match_star && (m = gfc_match_char ('*')) == MATCH_YES) |
| 92 |
star = true; |
| 93 |
|
| 94 |
if (!star && init) |
| 95 |
m = gfc_match_init_expr (&ar->start[i]); |
| 96 |
else if (!star) |
| 97 |
m = gfc_match_expr (&ar->start[i]); |
| 98 |
|
| 99 |
if (ar->start[i] && ar->start[i]->ts.type == BT_BOZ) |
| 100 |
{ |
| 101 |
gfc_error ("Invalid BOZ literal constant used in subscript at %C"); |
| 102 |
saw_boz = true; |
| 103 |
} |
| 104 |
|
| 105 |
if (m == MATCH_NO) |
| 106 |
gfc_error ("Expected array subscript at %C"); |
| 107 |
if (m != MATCH_YES) |
| 108 |
return MATCH_ERROR; |
| 109 |
|
| 110 |
if (gfc_match_char (':') == MATCH_NO) |
| 111 |
goto matched; |
| 112 |
|
| 113 |
if (star) |
| 114 |
{ |
| 115 |
gfc_error ("Unexpected %<*%> in coarray subscript at %C"); |
| 116 |
return MATCH_ERROR; |
| 117 |
} |
| 118 |
|
| 119 |
/* Get an optional end element. Because we've seen the colon, we |
| 120 |
definitely have a range along this dimension. */ |
| 121 |
end_element: |
| 122 |
ar->dimen_type[i] = DIMEN_RANGE; |
| 123 |
|
| 124 |
if (match_star && (m = gfc_match_char ('*')) == MATCH_YES) |
| 125 |
star = true; |
| 126 |
else if (init) |
| 127 |
m = gfc_match_init_expr (&ar->end[i]); |
| 128 |
else |
| 129 |
m = gfc_match_expr (&ar->end[i]); |
| 130 |
|
| 131 |
if (ar->end[i] && ar->end[i]->ts.type == BT_BOZ) |
| 132 |
{ |
| 133 |
gfc_error ("Invalid BOZ literal constant used in subscript at %C"); |
| 134 |
saw_boz = true; |
| 135 |
} |
| 136 |
|
| 137 |
if (m == MATCH_ERROR) |
| 138 |
return MATCH_ERROR; |
| 139 |
|
| 140 |
if (star && ar->start[i] == NULL) |
| 141 |
{ |
| 142 |
gfc_error ("Missing lower bound in assumed size " |
| 143 |
"coarray specification at %C"); |
| 144 |
return MATCH_ERROR; |
| 145 |
} |
| 146 |
|
| 147 |
/* See if we have an optional stride. */ |
| 148 |
if (gfc_match_char (':') == MATCH_YES) |
| 149 |
{ |
| 150 |
if (star) |
| 151 |
{ |
| 152 |
gfc_error ("Strides not allowed in coarray subscript at %C"); |
| 153 |
return MATCH_ERROR; |
| 154 |
} |
| 155 |
|
| 156 |
m = init ? gfc_match_init_expr (&ar->stride[i]) |
| 157 |
: gfc_match_expr (&ar->stride[i]); |
| 158 |
|
| 159 |
if (ar->stride[i] && ar->stride[i]->ts.type == BT_BOZ) |
| 160 |
{ |
| 161 |
gfc_error ("Invalid BOZ literal constant used in subscript at %C"); |
| 162 |
saw_boz = true; |
| 163 |
} |
| 164 |
|
| 165 |
if (m == MATCH_NO) |
| 166 |
gfc_error ("Expected array subscript stride at %C"); |
| 167 |
if (m != MATCH_YES) |
| 168 |
return MATCH_ERROR; |
| 169 |
} |
| 170 |
|
| 171 |
matched: |
| 172 |
if (star) |
| 173 |
ar->dimen_type[i] = DIMEN_STAR; |
| 174 |
|
| 175 |
return (saw_boz ? MATCH_ERROR : MATCH_YES); |
| 176 |
} |
| 177 |
|
| 178 |
/** Match one of TEAM=, TEAM_NUMBER= or STAT=. */ |
| 179 |
|
| 180 |
match |
| 181 |
match_team_or_stat (gfc_array_ref *ar) |
| 182 |
{ |
| 183 |
gfc_expr *tmp; |
| 184 |
bool team_error = false; |
| 185 |
|
| 186 |
if (gfc_match (" team = %e", &tmp) == MATCH_YES) |
| 187 |
{ |
| 188 |
if (ar->team == NULL && ar->team_type == TEAM_UNSET) |
| 189 |
{ |
| 190 |
ar->team = tmp; |
| 191 |
ar->team_type = TEAM_TEAM; |
| 192 |
} |
| 193 |
else if (ar->team_type == TEAM_TEAM) |
| 194 |
{ |
| 195 |
gfc_error ("Duplicate TEAM= attribute in %C"); |
| 196 |
return MATCH_ERROR; |
| 197 |
} |
| 198 |
else |
| 199 |
team_error = true; |
| 200 |
} |
| 201 |
else if (gfc_match (" team_number = %e", &tmp) == MATCH_YES) |
| 202 |
{ |
| 203 |
if (!gfc_notify_std (GFC_STD_F2018, "TEAM_NUMBER= not supported at %C")) |
| 204 |
return MATCH_ERROR; |
| 205 |
if (ar->team == NULL && ar->team_type == TEAM_UNSET) |
| 206 |
{ |
| 207 |
ar->team = tmp; |
| 208 |
ar->team_type = TEAM_NUMBER; |
| 209 |
} |
| 210 |
else if (ar->team_type == TEAM_NUMBER) |
| 211 |
{ |
| 212 |
gfc_error ("Duplicate TEAM_NUMBER= attribute in %C"); |
| 213 |
return MATCH_ERROR; |
| 214 |
} |
| 215 |
else |
| 216 |
team_error = true; |
| 217 |
} |
| 218 |
else if (gfc_match (" stat = %e", &tmp) == MATCH_YES) |
| 219 |
{ |
| 220 |
if (ar->stat == NULL) |
| 221 |
{ |
| 222 |
if (gfc_is_coindexed (tmp)) |
| 223 |
{ |
| 224 |
gfc_error ("Expression in STAT= at %C must not be coindexed"); |
| 225 |
gfc_free_expr (tmp); |
| 226 |
return MATCH_ERROR; |
| 227 |
} |
| 228 |
ar->stat = tmp; |
| 229 |
} |
| 230 |
else |
| 231 |
{ |
| 232 |
gfc_error ("Duplicate STAT= attribute in %C"); |
| 233 |
return MATCH_ERROR; |
| 234 |
} |
| 235 |
} |
| 236 |
else |
| 237 |
return MATCH_NO; |
| 238 |
|
| 239 |
if (ar->team && team_error) |
| 240 |
{ |
| 241 |
gfc_error ("Only one of TEAM= or TEAM_NUMBER= may appear in a " |
| 242 |
"coarray reference at %C"); |
| 243 |
return MATCH_ERROR; |
| 244 |
} |
| 245 |
|
| 246 |
return MATCH_YES; |
| 247 |
} |
| 248 |
|
| 249 |
/* Match an array reference, whether it is the whole array or particular |
| 250 |
elements or a section. If init is set, the reference has to consist |
| 251 |
of init expressions. */ |
| 252 |
|
| 253 |
match |
| 254 |
gfc_match_array_ref (gfc_array_ref *ar, gfc_array_spec *as, int init, |
| 255 |
int corank, bool coarray_only) |
| 256 |
{ |
| 257 |
match m; |
| 258 |
bool matched_bracket = false; |
| 259 |
|
| 260 |
memset (ar, '\0', sizeof (*ar)); |
| 261 |
|
| 262 |
ar->where = gfc_current_locus; |
| 263 |
ar->as = as; |
| 264 |
ar->type = AR_UNKNOWN; |
| 265 |
|
| 266 |
if (gfc_match_char ('[') == MATCH_YES) |
| 267 |
{ |
| 268 |
matched_bracket = true; |
| 269 |
goto coarray; |
| 270 |
} |
| 271 |
else if (coarray_only && corank != 0) |
| 272 |
goto coarray; |
| 273 |
|
| 274 |
if (gfc_match_char ('(') != MATCH_YES) |
| 275 |
{ |
| 276 |
ar->type = AR_FULL; |
| 277 |
ar->dimen = 0; |
| 278 |
if (corank != 0) |
| 279 |
{ |
| 280 |
for (int i = 0; i < GFC_MAX_DIMENSIONS; ++i) |
| 281 |
ar->dimen_type[i] = DIMEN_THIS_IMAGE; |
| 282 |
ar->codimen = corank; |
| 283 |
} |
| 284 |
return MATCH_YES; |
| 285 |
} |
| 286 |
|
| 287 |
for (ar->dimen = 0; ar->dimen < GFC_MAX_DIMENSIONS; ar->dimen++) |
| 288 |
{ |
| 289 |
m = match_subscript (ar, init, false); |
| 290 |
if (m == MATCH_ERROR) |
| 291 |
return MATCH_ERROR; |
| 292 |
|
| 293 |
if (gfc_match_char (')') == MATCH_YES) |
| 294 |
{ |
| 295 |
ar->dimen++; |
| 296 |
goto coarray; |
| 297 |
} |
| 298 |
|
| 299 |
if (gfc_match_char (',') != MATCH_YES) |
| 300 |
{ |
| 301 |
gfc_error ("Invalid form of array reference at %C"); |
| 302 |
return MATCH_ERROR; |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
if (ar->dimen >= 7 |
| 307 |
&& !gfc_notify_std (GFC_STD_F2008, |
| 308 |
"Array reference at %C has more than 7 dimensions")) |
| 309 |
return MATCH_ERROR; |
| 310 |
|
| 311 |
gfc_error ("Array reference at %C cannot have more than %d dimensions", |
| 312 |
GFC_MAX_DIMENSIONS); |
| 313 |
return MATCH_ERROR; |
| 314 |
|
| 315 |
coarray: |
| 316 |
if (!matched_bracket && gfc_match_char ('[') != MATCH_YES) |
| 317 |
{ |
| 318 |
int dim = coarray_only ? 0 : ar->dimen; |
| 319 |
if (dim > 0 || coarray_only) |
| 320 |
{ |
| 321 |
if (corank != 0) |
| 322 |
{ |
| 323 |
for (int i = dim; i < GFC_MAX_DIMENSIONS; ++i) |
| 324 |
ar->dimen_type[i] = DIMEN_THIS_IMAGE; |
| 325 |
ar->codimen = corank; |
| 326 |
} |
| 327 |
return MATCH_YES; |
| 328 |
} |
| 329 |
else |
| 330 |
return MATCH_ERROR; |
| 331 |
} |
| 332 |
|
| 333 |
if (flag_coarray == GFC_FCOARRAY_NONE) |
| 334 |
{ |
| 335 |
gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable"); |
| 336 |
return MATCH_ERROR; |
| 337 |
} |
| 338 |
|
| 339 |
if (corank == 0) |
| 340 |
{ |
| 341 |
gfc_error ("Unexpected coarray designator at %C"); |
| 342 |
return MATCH_ERROR; |
| 343 |
} |
| 344 |
|
| 345 |
ar->team_type = TEAM_UNSET; |
| 346 |
|
| 347 |
for (ar->codimen = 0; ar->codimen + ar->dimen < GFC_MAX_DIMENSIONS; |
| 348 |
ar->codimen++) |
| 349 |
{ |
| 350 |
m = match_subscript (ar, init, true); |
| 351 |
if (m == MATCH_ERROR) |
| 352 |
return MATCH_ERROR; |
| 353 |
|
| 354 |
if (gfc_match_char (',') != MATCH_YES) |
| 355 |
{ |
| 356 |
if (gfc_match_char ('*') == MATCH_YES) |
| 357 |
gfc_error ("Unexpected %<*%> for codimension %d of %d at %C", |
| 358 |
ar->codimen + 1, corank); |
| 359 |
else |
| 360 |
{ |
| 361 |
goto image_selector; |
| 362 |
} |
| 363 |
return MATCH_ERROR; |
| 364 |
} |
| 365 |
else if (ar->dimen_type[ar->codimen + ar->dimen] == DIMEN_STAR) |
| 366 |
{ |
| 367 |
gfc_error ("Unexpected %<*%> for codimension %d of %d at %C", |
| 368 |
ar->codimen + 1, corank); |
| 369 |
return MATCH_ERROR; |
| 370 |
} |
| 371 |
|
| 372 |
m = match_team_or_stat (ar); |
| 373 |
if (m == MATCH_ERROR) |
| 374 |
return MATCH_ERROR; |
| 375 |
else if (m == MATCH_YES) |
| 376 |
goto image_selector; |
| 377 |
|
| 378 |
if (gfc_match_char (']') == MATCH_YES) |
| 379 |
goto rank_check; |
| 380 |
|
| 381 |
if (ar->codimen >= corank) |
| 382 |
{ |
| 383 |
gfc_error ("Invalid codimension %d at %C, only %d codimensions exist", |
| 384 |
ar->codimen + 1, corank); |
| 385 |
return MATCH_ERROR; |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
gfc_error ("Array reference at %C cannot have more than %d dimensions", |
| 390 |
GFC_MAX_DIMENSIONS); |
| 391 |
return MATCH_ERROR; |
| 392 |
|
| 393 |
image_selector: |
| 394 |
for (;;) |
| 395 |
{ |
| 396 |
m = match_team_or_stat (ar); |
| 397 |
if (m == MATCH_ERROR) |
| 398 |
return MATCH_ERROR; |
| 399 |
|
| 400 |
if (gfc_match_char (']') == MATCH_YES) |
| 401 |
goto rank_check; |
| 402 |
|
| 403 |
if (gfc_match_char (',') != MATCH_YES) |
| 404 |
{ |
| 405 |
gfc_error ("Invalid form of coarray reference at %C"); |
| 406 |
return MATCH_ERROR; |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
return MATCH_ERROR; |
| 411 |
|
| 412 |
rank_check: |
| 413 |
ar->codimen++; |
| 414 |
if (ar->codimen < corank) |
| 415 |
{ |
| 416 |
gfc_error ("Too few codimensions at %C, expected %d not %d", corank, |
| 417 |
ar->codimen); |
| 418 |
return MATCH_ERROR; |
| 419 |
} |
| 420 |
if (ar->codimen > corank) |
| 421 |
{ |
| 422 |
gfc_error ("Too many codimensions at %C, expected %d not %d", corank, |
| 423 |
ar->codimen); |
| 424 |
return MATCH_ERROR; |
| 425 |
} |
| 426 |
return MATCH_YES; |
| 427 |
} |
| 428 |
|
| 429 |
|
| 430 |
/************** Array specification matching subroutines ***************/ |
| 431 |
|
| 432 |
/* Free all of the expressions associated with array bounds |
| 433 |
specifications. */ |
| 434 |
|
| 435 |
void |
| 436 |
gfc_free_array_spec (gfc_array_spec *as) |
| 437 |
{ |
| 438 |
int i; |
| 439 |
|
| 440 |
if (as == NULL) |
| 441 |
return; |
| 442 |
|
| 443 |
if (as->corank == 0) |
| 444 |
{ |
| 445 |
for (i = 0; i < as->rank; i++) |
| 446 |
{ |
| 447 |
gfc_free_expr (as->lower[i]); |
| 448 |
gfc_free_expr (as->upper[i]); |
| 449 |
} |
| 450 |
} |
| 451 |
else |
| 452 |
{ |
| 453 |
int n = as->rank + as->corank - (as->cotype == AS_EXPLICIT ? 1 : 0); |
| 454 |
for (i = 0; i < n; i++) |
| 455 |
{ |
| 456 |
gfc_free_expr (as->lower[i]); |
| 457 |
gfc_free_expr (as->upper[i]); |
| 458 |
} |
| 459 |
} |
| 460 |
|
| 461 |
free (as); |
| 462 |
} |
| 463 |
|
| 464 |
|
| 465 |
/* Take an array bound, resolves the expression, that make up the |
| 466 |
shape and check associated constraints. */ |
| 467 |
|
| 468 |
static bool |
| 469 |
resolve_array_bound (gfc_expr *e, int check_constant) |
| 470 |
{ |
| 471 |
if (e == NULL) |
| 472 |
return true; |
| 473 |
|
| 474 |
if (e->ts.type == BT_DERIVED || e->ts.type == BT_CLASS) |
| 475 |
{ |
| 476 |
gfc_error ("Derived type or class expression for array bound at %L", |
| 477 |
&e->where); |
| 478 |
return false; |
| 479 |
} |
| 480 |
|
| 481 |
if (!gfc_resolve_expr (e) |
| 482 |
|| !gfc_specification_expr (e)) |
| 483 |
return false; |
| 484 |
|
| 485 |
if (check_constant && !gfc_is_constant_expr (e)) |
| 486 |
{ |
| 487 |
if (e->expr_type == EXPR_VARIABLE) |
| 488 |
gfc_error ("Variable %qs at %L in this context must be constant", |
| 489 |
e->symtree->n.sym->name, &e->where); |
| 490 |
else |
| 491 |
gfc_error ("Expression at %L in this context must be constant", |
| 492 |
&e->where); |
| 493 |
return false; |
| 494 |
} |
| 495 |
|
| 496 |
return true; |
| 497 |
} |
| 498 |
|
| 499 |
|
| 500 |
/* Takes an array specification, resolves the expressions that make up |
| 501 |
the shape and make sure everything is integral. */ |
| 502 |
|
| 503 |
bool |
| 504 |
gfc_resolve_array_spec (gfc_array_spec *as, int check_constant) |
| 505 |
{ |
| 506 |
gfc_expr *e; |
| 507 |
int i; |
| 508 |
|
| 509 |
if (as == NULL) |
| 510 |
return true; |
| 511 |
|
| 512 |
if (as->resolved) |
| 513 |
return true; |
| 514 |
|
| 515 |
for (i = 0; i < as->rank + as->corank; i++) |
| 516 |
{ |
| 517 |
if (i == GFC_MAX_DIMENSIONS) |
| 518 |
return false; |
| 519 |
|
| 520 |
e = as->lower[i]; |
| 521 |
if (!resolve_array_bound (e, check_constant)) |
| 522 |
return false; |
| 523 |
|
| 524 |
e = as->upper[i]; |
| 525 |
if (!resolve_array_bound (e, check_constant)) |
| 526 |
return false; |
| 527 |
|
| 528 |
if ((as->lower[i] == NULL) || (as->upper[i] == NULL)) |
| 529 |
continue; |
| 530 |
|
| 531 |
/* If the size is negative in this dimension, set it to zero. */ |
| 532 |
if (as->lower[i]->expr_type == EXPR_CONSTANT |
| 533 |
&& as->upper[i]->expr_type == EXPR_CONSTANT |
| 534 |
&& mpz_cmp (as->upper[i]->value.integer, |
| 535 |
as->lower[i]->value.integer) < 0) |
| 536 |
{ |
| 537 |
gfc_free_expr (as->upper[i]); |
| 538 |
as->upper[i] = gfc_copy_expr (as->lower[i]); |
| 539 |
mpz_sub_ui (as->upper[i]->value.integer, |
| 540 |
as->upper[i]->value.integer, 1); |
| 541 |
} |
| 542 |
} |
| 543 |
|
| 544 |
as->resolved = true; |
| 545 |
|
| 546 |
return true; |
| 547 |
} |
| 548 |
|
| 549 |
|
| 550 |
/* Match a single array element specification. The return values as |
| 551 |
well as the upper and lower bounds of the array spec are filled |
| 552 |
in according to what we see on the input. The caller makes sure |
| 553 |
individual specifications make sense as a whole. |
| 554 |
|
| 555 |
|
| 556 |
Parsed Lower Upper Returned |
| 557 |
------------------------------------ |
| 558 |
: NULL NULL AS_DEFERRED (*) |
| 559 |
x 1 x AS_EXPLICIT |
| 560 |
x: x NULL AS_ASSUMED_SHAPE |
| 561 |
x:y x y AS_EXPLICIT |
| 562 |
x:* x NULL AS_ASSUMED_SIZE |
| 563 |
* 1 NULL AS_ASSUMED_SIZE |
| 564 |
|
| 565 |
(*) For non-pointer dummy arrays this is AS_ASSUMED_SHAPE. This |
| 566 |
is fixed during the resolution of formal interfaces. |
| 567 |
|
| 568 |
Anything else AS_UNKNOWN. */ |
| 569 |
|
| 570 |
static array_type |
| 571 |
match_array_element_spec (gfc_array_spec *as) |
| 572 |
{ |
| 573 |
gfc_expr **upper, **lower; |
| 574 |
match m; |
| 575 |
int rank; |
| 576 |
|
| 577 |
rank = as->rank == -1 ? 0 : as->rank; |
| 578 |
lower = &as->lower[rank + as->corank - 1]; |
| 579 |
upper = &as->upper[rank + as->corank - 1]; |
| 580 |
|
| 581 |
if (gfc_match_char ('*') == MATCH_YES) |
| 582 |
{ |
| 583 |
*lower = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1); |
| 584 |
return AS_ASSUMED_SIZE; |
| 585 |
} |
| 586 |
|
| 587 |
if (gfc_match_char (':') == MATCH_YES) |
| 588 |
{ |
| 589 |
locus old_loc = gfc_current_locus; |
| 590 |
if (gfc_match_char ('*') == MATCH_YES) |
| 591 |
{ |
| 592 |
/* F2018:R821: "assumed-implied-spec is [ lower-bound : ] *". */ |
| 593 |
gfc_error ("A lower bound must precede colon in " |
| 594 |
"assumed-size array specification at %L", &old_loc); |
| 595 |
return AS_UNKNOWN; |
| 596 |
} |
| 597 |
else |
| 598 |
{ |
| 599 |
return AS_DEFERRED; |
| 600 |
} |
| 601 |
} |
| 602 |
|
| 603 |
m = gfc_match_expr (upper); |
| 604 |
if (m == MATCH_NO) |
| 605 |
gfc_error ("Expected expression in array specification at %C"); |
| 606 |
if (m != MATCH_YES) |
| 607 |
return AS_UNKNOWN; |
| 608 |
if (!gfc_expr_check_typed (*upper, gfc_current_ns, false)) |
| 609 |
return AS_UNKNOWN; |
| 610 |
|
| 611 |
if (((*upper)->expr_type == EXPR_CONSTANT |
| 612 |
&& (*upper)->ts.type != BT_INTEGER) || |
| 613 |
((*upper)->expr_type == EXPR_FUNCTION |
| 614 |
&& (*upper)->ts.type == BT_UNKNOWN |
| 615 |
&& (*upper)->symtree |
| 616 |
&& strcmp ((*upper)->symtree->name, "null") == 0)) |
| 617 |
{ |
| 618 |
gfc_error ("Expecting a scalar INTEGER expression at %C, found %s", |
| 619 |
gfc_basic_typename ((*upper)->ts.type)); |
| 620 |
return AS_UNKNOWN; |
| 621 |
} |
| 622 |
|
| 623 |
if (gfc_match_char (':') == MATCH_NO) |
| 624 |
{ |
| 625 |
*lower = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1); |
| 626 |
return AS_EXPLICIT; |
| 627 |
} |
| 628 |
|
| 629 |
*lower = *upper; |
| 630 |
*upper = NULL; |
| 631 |
|
| 632 |
if (gfc_match_char ('*') == MATCH_YES) |
| 633 |
return AS_ASSUMED_SIZE; |
| 634 |
|
| 635 |
m = gfc_match_expr (upper); |
| 636 |
if (m == MATCH_ERROR) |
| 637 |
return AS_UNKNOWN; |
| 638 |
if (m == MATCH_NO) |
| 639 |
return AS_ASSUMED_SHAPE; |
| 640 |
if (!gfc_expr_check_typed (*upper, gfc_current_ns, false)) |
| 641 |
return AS_UNKNOWN; |
| 642 |
|
| 643 |
if (((*upper)->expr_type == EXPR_CONSTANT |
| 644 |
&& (*upper)->ts.type != BT_INTEGER) || |
| 645 |
((*upper)->expr_type == EXPR_FUNCTION |
| 646 |
&& (*upper)->ts.type == BT_UNKNOWN |
| 647 |
&& (*upper)->symtree |
| 648 |
&& strcmp ((*upper)->symtree->name, "null") == 0)) |
| 649 |
{ |
| 650 |
gfc_error ("Expecting a scalar INTEGER expression at %C, found %s", |
| 651 |
gfc_basic_typename ((*upper)->ts.type)); |
| 652 |
return AS_UNKNOWN; |
| 653 |
} |
| 654 |
|
| 655 |
return AS_EXPLICIT; |
| 656 |
} |
| 657 |
|
| 658 |
|
| 659 |
/* Matches an array specification, incidentally figuring out what sort |
| 660 |
it is. Match either a normal array specification, or a coarray spec |
| 661 |
or both. Optionally allow [:] for coarrays. */ |
| 662 |
|
| 663 |
match |
| 664 |
gfc_match_array_spec (gfc_array_spec **asp, bool match_dim, bool match_codim) |
| 665 |
{ |
| 666 |
array_type current_type; |
| 667 |
gfc_array_spec *as; |
| 668 |
int i; |
| 669 |
|
| 670 |
as = gfc_get_array_spec (); |
| 671 |
|
| 672 |
if (!match_dim) |
| 673 |
goto coarray; |
| 674 |
|
| 675 |
if (gfc_match_char ('(') != MATCH_YES) |
| 676 |
{ |
| 677 |
if (!match_codim) |
| 678 |
goto done; |
| 679 |
goto coarray; |
| 680 |
} |
| 681 |
|
| 682 |
if (gfc_match (" .. )") == MATCH_YES) |
| 683 |
{ |
| 684 |
as->type = AS_ASSUMED_RANK; |
| 685 |
as->rank = -1; |
| 686 |
|
| 687 |
if (!gfc_notify_std (GFC_STD_F2018, "Assumed-rank array at %C")) |
| 688 |
goto cleanup; |
| 689 |
|
| 690 |
if (!match_codim) |
| 691 |
goto done; |
| 692 |
goto coarray; |
| 693 |
} |
| 694 |
|
| 695 |
for (;;) |
| 696 |
{ |
| 697 |
as->rank++; |
| 698 |
current_type = match_array_element_spec (as); |
| 699 |
if (current_type == AS_UNKNOWN) |
| 700 |
goto cleanup; |
| 701 |
|
| 702 |
/* Note that current_type == AS_ASSUMED_SIZE for both assumed-size |
| 703 |
and implied-shape specifications. If the rank is at least 2, we can |
| 704 |
distinguish between them. But for rank 1, we currently return |
| 705 |
ASSUMED_SIZE; this gets adjusted later when we know for sure |
| 706 |
whether the symbol parsed is a PARAMETER or not. */ |
| 707 |
|
| 708 |
if (as->rank == 1) |
| 709 |
{ |
| 710 |
as->type = current_type; |
| 711 |
} |
| 712 |
else |
| 713 |
switch (as->type) |
| 714 |
{ /* See how current spec meshes with the existing. */ |
| 715 |
case AS_UNKNOWN: |
| 716 |
goto cleanup; |
| 717 |
|
| 718 |
case AS_IMPLIED_SHAPE: |
| 719 |
if (current_type != AS_ASSUMED_SIZE) |
| 720 |
{ |
| 721 |
gfc_error ("Bad array specification for implied-shape" |
| 722 |
" array at %C"); |
| 723 |
goto cleanup; |
| 724 |
} |
| 725 |
break; |
| 726 |
|
| 727 |
case AS_EXPLICIT: |
| 728 |
if (current_type == AS_ASSUMED_SIZE) |
| 729 |
{ |
| 730 |
as->type = AS_ASSUMED_SIZE; |
| 731 |
break; |
| 732 |
} |
| 733 |
|
| 734 |
if (current_type == AS_EXPLICIT) |
| 735 |
break; |
| 736 |
|
| 737 |
gfc_error ("Bad array specification for an explicitly shaped " |
| 738 |
"array at %C"); |
| 739 |
|
| 740 |
goto cleanup; |
| 741 |
|
| 742 |
case AS_ASSUMED_SHAPE: |
| 743 |
if ((current_type == AS_ASSUMED_SHAPE) |
| 744 |
|| (current_type == AS_DEFERRED)) |
| 745 |
break; |
| 746 |
|
| 747 |
gfc_error ("Bad array specification for assumed shape " |
| 748 |
"array at %C"); |
| 749 |
goto cleanup; |
| 750 |
|
| 751 |
case AS_DEFERRED: |
| 752 |
if (current_type == AS_DEFERRED) |
| 753 |
break; |
| 754 |
|
| 755 |
if (current_type == AS_ASSUMED_SHAPE) |
| 756 |
{ |
| 757 |
as->type = AS_ASSUMED_SHAPE; |
| 758 |
break; |
| 759 |
} |
| 760 |
|
| 761 |
gfc_error ("Bad specification for deferred shape array at %C"); |
| 762 |
goto cleanup; |
| 763 |
|
| 764 |
case AS_ASSUMED_SIZE: |
| 765 |
if (as->rank == 2 && current_type == AS_ASSUMED_SIZE) |
| 766 |
{ |
| 767 |
as->type = AS_IMPLIED_SHAPE; |
| 768 |
break; |
| 769 |
} |
| 770 |
|
| 771 |
gfc_error ("Bad specification for assumed size array at %C"); |
| 772 |
goto cleanup; |
| 773 |
|
| 774 |
case AS_ASSUMED_RANK: |
| 775 |
gcc_unreachable (); |
| 776 |
} |
| 777 |
|
| 778 |
if (gfc_match_char (')') == MATCH_YES) |
| 779 |
break; |
| 780 |
|
| 781 |
if (gfc_match_char (',') != MATCH_YES) |
| 782 |
{ |
| 783 |
gfc_error ("Expected another dimension in array declaration at %C"); |
| 784 |
goto cleanup; |
| 785 |
} |
| 786 |
|
| 787 |
if (as->rank + as->corank >= GFC_MAX_DIMENSIONS) |
| 788 |
{ |
| 789 |
gfc_error ("Array specification at %C has more than %d dimensions", |
| 790 |
GFC_MAX_DIMENSIONS); |
| 791 |
goto cleanup; |
| 792 |
} |
| 793 |
|
| 794 |
if (as->corank + as->rank >= 7 |
| 795 |
&& !gfc_notify_std (GFC_STD_F2008, "Array specification at %C " |
| 796 |
"with more than 7 dimensions")) |
| 797 |
goto cleanup; |
| 798 |
} |
| 799 |
|
| 800 |
if (!match_codim) |
| 801 |
goto done; |
| 802 |
|
| 803 |
coarray: |
| 804 |
if (gfc_match_char ('[') != MATCH_YES) |
| 805 |
goto done; |
| 806 |
|
| 807 |
if (!gfc_notify_std (GFC_STD_F2008, "Coarray declaration at %C")) |
| 808 |
goto cleanup; |
| 809 |
|
| 810 |
if (flag_coarray == GFC_FCOARRAY_NONE) |
| 811 |
{ |
| 812 |
gfc_fatal_error ("Coarrays disabled at %C, use %<-fcoarray=%> to enable"); |
| 813 |
goto cleanup; |
| 814 |
} |
| 815 |
|
| 816 |
if (as->rank >= GFC_MAX_DIMENSIONS) |
| 817 |
{ |
| 818 |
gfc_error ("Array specification at %C has more than %d " |
| 819 |
"dimensions", GFC_MAX_DIMENSIONS); |
| 820 |
goto cleanup; |
| 821 |
} |
| 822 |
|
| 823 |
for (;;) |
| 824 |
{ |
| 825 |
as->corank++; |
| 826 |
current_type = match_array_element_spec (as); |
| 827 |
|
| 828 |
if (current_type == AS_UNKNOWN) |
| 829 |
goto cleanup; |
| 830 |
|
| 831 |
if (as->corank == 1) |
| 832 |
as->cotype = current_type; |
| 833 |
else |
| 834 |
switch (as->cotype) |
| 835 |
{ /* See how current spec meshes with the existing. */ |
| 836 |
case AS_IMPLIED_SHAPE: |
| 837 |
case AS_UNKNOWN: |
| 838 |
goto cleanup; |
| 839 |
|
| 840 |
case AS_EXPLICIT: |
| 841 |
if (current_type == AS_ASSUMED_SIZE) |
| 842 |
{ |
| 843 |
as->cotype = AS_ASSUMED_SIZE; |
| 844 |
break; |
| 845 |
} |
| 846 |
|
| 847 |
if (current_type == AS_EXPLICIT) |
| 848 |
break; |
| 849 |
|
| 850 |
gfc_error ("Bad array specification for an explicitly " |
| 851 |
"shaped array at %C"); |
| 852 |
|
| 853 |
goto cleanup; |
| 854 |
|
| 855 |
case AS_ASSUMED_SHAPE: |
| 856 |
if ((current_type == AS_ASSUMED_SHAPE) |
| 857 |
|| (current_type == AS_DEFERRED)) |
| 858 |
break; |
| 859 |
|
| 860 |
gfc_error ("Bad array specification for assumed shape " |
| 861 |
"array at %C"); |
| 862 |
goto cleanup; |
| 863 |
|
| 864 |
case AS_DEFERRED: |
| 865 |
if (current_type == AS_DEFERRED) |
| 866 |
break; |
| 867 |
|
| 868 |
if (current_type == AS_ASSUMED_SHAPE) |
| 869 |
{ |
| 870 |
as->cotype = AS_ASSUMED_SHAPE; |
| 871 |
break; |
| 872 |
} |
| 873 |
|
| 874 |
gfc_error ("Bad specification for deferred shape array at %C"); |
| 875 |
goto cleanup; |
| 876 |
|
| 877 |
case AS_ASSUMED_SIZE: |
| 878 |
gfc_error ("Bad specification for assumed size array at %C"); |
| 879 |
goto cleanup; |
| 880 |
|
| 881 |
case AS_ASSUMED_RANK: |
| 882 |
gcc_unreachable (); |
| 883 |
} |
| 884 |
|
| 885 |
if (gfc_match_char (']') == MATCH_YES) |
| 886 |
break; |
| 887 |
|
| 888 |
if (gfc_match_char (',') != MATCH_YES) |
| 889 |
{ |
| 890 |
gfc_error ("Expected another dimension in array declaration at %C"); |
| 891 |
goto cleanup; |
| 892 |
} |
| 893 |
|
| 894 |
if (as->rank + as->corank >= GFC_MAX_DIMENSIONS) |
| 895 |
{ |
| 896 |
gfc_error ("Array specification at %C has more than %d " |
| 897 |
"dimensions", GFC_MAX_DIMENSIONS); |
| 898 |
goto cleanup; |
| 899 |
} |
| 900 |
} |
| 901 |
|
| 902 |
if (current_type == AS_EXPLICIT) |
| 903 |
{ |
| 904 |
gfc_error ("Upper bound of last coarray dimension must be %<*%> at %C"); |
| 905 |
goto cleanup; |
| 906 |
} |
| 907 |
|
| 908 |
if (as->cotype == AS_ASSUMED_SIZE) |
| 909 |
as->cotype = AS_EXPLICIT; |
| 910 |
|
| 911 |
if (as->rank == 0) |
| 912 |
as->type = as->cotype; |
| 913 |
|
| 914 |
done: |
| 915 |
if (as->rank == 0 && as->corank == 0) |
| 916 |
{ |
| 917 |
*asp = NULL; |
| 918 |
gfc_free_array_spec (as); |
| 919 |
return MATCH_NO; |
| 920 |
} |
| 921 |
|
| 922 |
/* If a lower bounds of an assumed shape array is blank, put in one. */ |
| 923 |
if (as->type == AS_ASSUMED_SHAPE) |
| 924 |
{ |
| 925 |
for (i = 0; i < as->rank + as->corank; i++) |
| 926 |
{ |
| 927 |
if (as->lower[i] == NULL) |
| 928 |
as->lower[i] = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1); |
| 929 |
} |
| 930 |
} |
| 931 |
|
| 932 |
*asp = as; |
| 933 |
|
| 934 |
return MATCH_YES; |
| 935 |
|
| 936 |
cleanup: |
| 937 |
/* Something went wrong. */ |
| 938 |
gfc_free_array_spec (as); |
| 939 |
return MATCH_ERROR; |
| 940 |
} |
| 941 |
|
| 942 |
/* Given a symbol and an array specification, modify the symbol to |
| 943 |
have that array specification. The error locus is needed in case |
| 944 |
something goes wrong. On failure, the caller must free the spec. */ |
| 945 |
|
| 946 |
bool |
| 947 |
gfc_set_array_spec (gfc_symbol *sym, gfc_array_spec *as, locus *error_loc) |
| 948 |
{ |
| 949 |
int i; |
| 950 |
symbol_attribute *attr; |
| 951 |
|
| 952 |
if (as == NULL) |
| 953 |
return true; |
| 954 |
|
| 955 |
/* If the symbol corresponds to a submodule module procedure the array spec is |
| 956 |
already set, so do not attempt to set it again here. */ |
| 957 |
attr = &sym->attr; |
| 958 |
if (gfc_submodule_procedure(attr)) |
| 959 |
return true; |
| 960 |
|
| 961 |
if (as->rank |
| 962 |
&& !gfc_add_dimension (&sym->attr, sym->name, error_loc)) |
| 963 |
return false; |
| 964 |
|
| 965 |
if (as->corank |
| 966 |
&& !gfc_add_codimension (&sym->attr, sym->name, error_loc)) |
| 967 |
return false; |
| 968 |
|
| 969 |
if (sym->as == NULL) |
| 970 |
{ |
| 971 |
sym->as = as; |
| 972 |
return true; |
| 973 |
} |
| 974 |
|
| 975 |
if ((sym->as->type == AS_ASSUMED_RANK && as->corank) |
| 976 |
|| (as->type == AS_ASSUMED_RANK && sym->as->corank)) |
| 977 |
{ |
| 978 |
gfc_error ("The assumed-rank array %qs at %L shall not have a " |
| 979 |
"codimension", sym->name, error_loc); |
| 980 |
return false; |
| 981 |
} |
| 982 |
|
| 983 |
/* Check F2018:C822. */ |
| 984 |
if (sym->as->rank + sym->as->corank > GFC_MAX_DIMENSIONS) |
| 985 |
goto too_many; |
| 986 |
|
| 987 |
if (as->corank) |
| 988 |
{ |
| 989 |
sym->as->cotype = as->cotype; |
| 990 |
sym->as->corank = as->corank; |
| 991 |
/* Check F2018:C822. */ |
| 992 |
if (sym->as->rank + sym->as->corank > GFC_MAX_DIMENSIONS) |
| 993 |
goto too_many; |
| 994 |
|
| 995 |
for (i = 0; i < as->corank; i++) |
| 996 |
{ |
| 997 |
sym->as->lower[sym->as->rank + i] = as->lower[i]; |
| 998 |
sym->as->upper[sym->as->rank + i] = as->upper[i]; |
| 999 |
} |
| 1000 |
} |
| 1001 |
else |
| 1002 |
{ |
| 1003 |
/* The "sym" has no rank (checked via gfc_add_dimension). Thus |
| 1004 |
the dimension is added - but first the codimensions (if existing |
| 1005 |
need to be shifted to make space for the dimension. */ |
| 1006 |
gcc_assert (as->corank == 0 && sym->as->rank == 0); |
| 1007 |
|
| 1008 |
sym->as->rank = as->rank; |
| 1009 |
sym->as->type = as->type; |
| 1010 |
sym->as->cray_pointee = as->cray_pointee; |
| 1011 |
sym->as->cp_was_assumed = as->cp_was_assumed; |
| 1012 |
|
| 1013 |
/* Check F2018:C822. */ |
| 1014 |
if (sym->as->rank + sym->as->corank > GFC_MAX_DIMENSIONS) |
| 1015 |
goto too_many; |
| 1016 |
|
| 1017 |
for (i = sym->as->corank - 1; i >= 0; i--) |
| 1018 |
{ |
| 1019 |
sym->as->lower[as->rank + i] = sym->as->lower[i]; |
| 1020 |
sym->as->upper[as->rank + i] = sym->as->upper[i]; |
| 1021 |
} |
| 1022 |
for (i = 0; i < as->rank; i++) |
| 1023 |
{ |
| 1024 |
sym->as->lower[i] = as->lower[i]; |
| 1025 |
sym->as->upper[i] = as->upper[i]; |
| 1026 |
} |
| 1027 |
} |
| 1028 |
|
| 1029 |
free (as); |
| 1030 |
return true; |
| 1031 |
|
| 1032 |
too_many: |
| 1033 |
|
| 1034 |
gfc_error ("rank + corank of %qs exceeds %d at %C", sym->name, |
| 1035 |
GFC_MAX_DIMENSIONS); |
| 1036 |
return false; |
| 1037 |
} |
| 1038 |
|
| 1039 |
|
| 1040 |
/* Copy an array specification. */ |
| 1041 |
|
| 1042 |
gfc_array_spec * |
| 1043 |
gfc_copy_array_spec (gfc_array_spec *src) |
| 1044 |
{ |
| 1045 |
gfc_array_spec *dest; |
| 1046 |
int i; |
| 1047 |
|
| 1048 |
if (src == NULL) |
| 1049 |
return NULL; |
| 1050 |
|
| 1051 |
dest = gfc_get_array_spec (); |
| 1052 |
|
| 1053 |
*dest = *src; |
| 1054 |
|
| 1055 |
for (i = 0; i < dest->rank + dest->corank; i++) |
| 1056 |
{ |
| 1057 |
dest->lower[i] = gfc_copy_expr (dest->lower[i]); |
| 1058 |
dest->upper[i] = gfc_copy_expr (dest->upper[i]); |
| 1059 |
} |
| 1060 |
|
| 1061 |
return dest; |
| 1062 |
} |
| 1063 |
|
| 1064 |
|
| 1065 |
/* Returns nonzero if the two expressions are equal. |
| 1066 |
We should not need to support more than constant values, as that's what is |
| 1067 |
allowed in derived type component array spec. However, we may create types |
| 1068 |
with non-constant array spec for dummy variable class container types, for |
| 1069 |
which the _data component holds the array spec of the variable declaration. |
| 1070 |
So we have to support non-constant bounds as well. */ |
| 1071 |
|
| 1072 |
static bool |
| 1073 |
compare_bounds (gfc_expr *bound1, gfc_expr *bound2) |
| 1074 |
{ |
| 1075 |
if (bound1 == NULL || bound2 == NULL |
| 1076 |
|| bound1->ts.type != BT_INTEGER |
| 1077 |
|| bound2->ts.type != BT_INTEGER) |
| 1078 |
return false; |
| 1079 |
|
| 1080 |
/* What qualifies as identical bounds? We could probably just check that the |
| 1081 |
expressions are exact clones. We avoid rewriting a specific comparison |
| 1082 |
function and re-use instead the rather involved gfc_dep_compare_expr which |
| 1083 |
is just a bit more permissive, as it can also detect identical values for |
| 1084 |
some mismatching expressions (extra parenthesis, swapped operands, unary |
| 1085 |
plus, etc). It probably only makes a difference in corner cases. */ |
| 1086 |
return gfc_dep_compare_expr (bound1, bound2) == 0; |
| 1087 |
} |
| 1088 |
|
| 1089 |
|
| 1090 |
/* Compares two array specifications. They must be constant or deferred |
| 1091 |
shape. */ |
| 1092 |
|
| 1093 |
bool |
| 1094 |
gfc_compare_array_spec (gfc_array_spec *as1, gfc_array_spec *as2) |
| 1095 |
{ |
| 1096 |
int i; |
| 1097 |
|
| 1098 |
if (as1 == NULL && as2 == NULL) |
| 1099 |
return 1; |
| 1100 |
|
| 1101 |
if (as1 == NULL || as2 == NULL) |
| 1102 |
return 0; |
| 1103 |
|
| 1104 |
if (as1->rank != as2->rank) |
| 1105 |
return 0; |
| 1106 |
|
| 1107 |
if (as1->corank != as2->corank) |
| 1108 |
return 0; |
| 1109 |
|
| 1110 |
if (as1->rank == 0) |
| 1111 |
return 1; |
| 1112 |
|
| 1113 |
if (as1->type != as2->type) |
| 1114 |
return 0; |
| 1115 |
|
| 1116 |
if (as1->cotype != as2->cotype) |
| 1117 |
return 0; |
| 1118 |
|
| 1119 |
if (as1->type == AS_EXPLICIT) |
| 1120 |
for (i = 0; i < as1->rank + as1->corank; i++) |
| 1121 |
{ |
| 1122 |
if (!compare_bounds (as1->lower[i], as2->lower[i])) |
| 1123 |
return 0; |
| 1124 |
|
| 1125 |
if (!compare_bounds (as1->upper[i], as2->upper[i])) |
| 1126 |
return 0; |
| 1127 |
} |
| 1128 |
|
| 1129 |
return 1; |
| 1130 |
} |
| 1131 |
|
| 1132 |
|
| 1133 |
/****************** Array constructor functions ******************/ |
| 1134 |
|
| 1135 |
|
| 1136 |
/* Given an expression node that might be an array constructor and a |
| 1137 |
symbol, make sure that no iterators in this or child constructors |
| 1138 |
use the symbol as an implied-DO iterator. Returns nonzero if a |
| 1139 |
duplicate was found. */ |
| 1140 |
|
| 1141 |
static bool |
| 1142 |
check_duplicate_iterator (gfc_constructor_base base, gfc_symbol *master) |
| 1143 |
{ |
| 1144 |
gfc_constructor *c; |
| 1145 |
gfc_expr *e; |
| 1146 |
|
| 1147 |
for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c)) |
| 1148 |
{ |
| 1149 |
e = c->expr; |
| 1150 |
|
| 1151 |
if (e->expr_type == EXPR_ARRAY |
| 1152 |
&& check_duplicate_iterator (e->value.constructor, master)) |
| 1153 |
return 1; |
| 1154 |
|
| 1155 |
if (c->iterator == NULL) |
| 1156 |
continue; |
| 1157 |
|
| 1158 |
if (c->iterator->var->symtree->n.sym == master) |
| 1159 |
{ |
| 1160 |
gfc_error ("DO-iterator %qs at %L is inside iterator of the " |
| 1161 |
"same name", master->name, &c->where); |
| 1162 |
|
| 1163 |
return 1; |
| 1164 |
} |
| 1165 |
} |
| 1166 |
|
| 1167 |
return 0; |
| 1168 |
} |
| 1169 |
|
| 1170 |
|
| 1171 |
/* Forward declaration because these functions are mutually recursive. */ |
| 1172 |
static match match_array_cons_element (gfc_constructor_base *); |
| 1173 |
|
| 1174 |
/* Match a list of array elements. */ |
| 1175 |
|
| 1176 |
static match |
| 1177 |
match_array_list (gfc_constructor_base *result) |
| 1178 |
{ |
| 1179 |
gfc_constructor_base head; |
| 1180 |
gfc_constructor *p; |
| 1181 |
gfc_iterator iter; |
| 1182 |
locus old_loc; |
| 1183 |
gfc_expr *e; |
| 1184 |
match m; |
| 1185 |
int n; |
| 1186 |
|
| 1187 |
old_loc = gfc_current_locus; |
| 1188 |
|
| 1189 |
if (gfc_match_char ('(') == MATCH_NO) |
| 1190 |
return MATCH_NO; |
| 1191 |
|
| 1192 |
memset (&iter, '\0', sizeof (gfc_iterator)); |
| 1193 |
head = NULL; |
| 1194 |
|
| 1195 |
m = match_array_cons_element (&head); |
| 1196 |
if (m != MATCH_YES) |
| 1197 |
goto cleanup; |
| 1198 |
|
| 1199 |
if (gfc_match_char (',') != MATCH_YES) |
| 1200 |
{ |
| 1201 |
m = MATCH_NO; |
| 1202 |
goto cleanup; |
| 1203 |
} |
| 1204 |
|
| 1205 |
for (n = 1;; n++) |
| 1206 |
{ |
| 1207 |
m = gfc_match_iterator (&iter, 0); |
| 1208 |
if (m == MATCH_YES) |
| 1209 |
break; |
| 1210 |
if (m == MATCH_ERROR) |
| 1211 |
goto cleanup; |
| 1212 |
|
| 1213 |
m = match_array_cons_element (&head); |
| 1214 |
if (m == MATCH_ERROR) |
| 1215 |
goto cleanup; |
| 1216 |
if (m == MATCH_NO) |
| 1217 |
{ |
| 1218 |
if (n > 2) |
| 1219 |
goto syntax; |
| 1220 |
m = MATCH_NO; |
| 1221 |
goto cleanup; /* Could be a complex constant */ |
| 1222 |
} |
| 1223 |
|
| 1224 |
if (gfc_match_char (',') != MATCH_YES) |
| 1225 |
{ |
| 1226 |
if (n > 2) |
| 1227 |
goto syntax; |
| 1228 |
m = MATCH_NO; |
| 1229 |
goto cleanup; |
| 1230 |
} |
| 1231 |
} |
| 1232 |
|
| 1233 |
if (gfc_match_char (')') != MATCH_YES) |
| 1234 |
goto syntax; |
| 1235 |
|
| 1236 |
if (check_duplicate_iterator (head, iter.var->symtree->n.sym)) |
| 1237 |
{ |
| 1238 |
m = MATCH_ERROR; |
| 1239 |
goto cleanup; |
| 1240 |
} |
| 1241 |
|
| 1242 |
e = gfc_get_array_expr (BT_UNKNOWN, 0, &old_loc); |
| 1243 |
e->value.constructor = head; |
| 1244 |
|
| 1245 |
p = gfc_constructor_append_expr (result, e, &gfc_current_locus); |
| 1246 |
p->iterator = gfc_get_iterator (); |
| 1247 |
*p->iterator = iter; |
| 1248 |
|
| 1249 |
return MATCH_YES; |
| 1250 |
|
| 1251 |
syntax: |
| 1252 |
gfc_error ("Syntax error in array constructor at %C"); |
| 1253 |
m = MATCH_ERROR; |
| 1254 |
|
| 1255 |
cleanup: |
| 1256 |
gfc_constructor_free (head); |
| 1257 |
gfc_free_iterator (&iter, 0); |
| 1258 |
gfc_current_locus = old_loc; |
| 1259 |
return m; |
| 1260 |
} |
| 1261 |
|
| 1262 |
|
| 1263 |
/* Match a single element of an array constructor, which can be a |
| 1264 |
single expression or a list of elements. */ |
| 1265 |
|
| 1266 |
static match |
| 1267 |
match_array_cons_element (gfc_constructor_base *result) |
| 1268 |
{ |
| 1269 |
gfc_expr *expr; |
| 1270 |
match m; |
| 1271 |
|
| 1272 |
m = match_array_list (result); |
| 1273 |
if (m != MATCH_NO) |
| 1274 |
return m; |
| 1275 |
|
| 1276 |
m = gfc_match_expr (&expr); |
| 1277 |
if (m != MATCH_YES) |
| 1278 |
return m; |
| 1279 |
|
| 1280 |
if (expr->ts.type == BT_BOZ) |
| 1281 |
{ |
| 1282 |
gfc_error ("BOZ literal constant at %L cannot appear in an " |
| 1283 |
"array constructor", &expr->where); |
| 1284 |
goto done; |
| 1285 |
} |
| 1286 |
|
| 1287 |
if (expr->expr_type == EXPR_FUNCTION |
| 1288 |
&& expr->ts.type == BT_UNKNOWN |
| 1289 |
&& strcmp(expr->symtree->name, "null") == 0) |
| 1290 |
{ |
| 1291 |
gfc_error ("NULL() at %C cannot appear in an array constructor"); |
| 1292 |
goto done; |
| 1293 |
} |
| 1294 |
|
| 1295 |
gfc_constructor_append_expr (result, expr, &gfc_current_locus); |
| 1296 |
return MATCH_YES; |
| 1297 |
|
| 1298 |
done: |
| 1299 |
gfc_free_expr (expr); |
| 1300 |
return MATCH_ERROR; |
| 1301 |
} |
| 1302 |
|
| 1303 |
|
| 1304 |
/* Convert components of an array constructor to the type in ts. */ |
| 1305 |
|
| 1306 |
static match |
| 1307 |
walk_array_constructor (gfc_typespec *ts, gfc_constructor_base head) |
| 1308 |
{ |
| 1309 |
gfc_constructor *c; |
| 1310 |
gfc_expr *e; |
| 1311 |
match m; |
| 1312 |
|
| 1313 |
for (c = gfc_constructor_first (head); c; c = gfc_constructor_next (c)) |
| 1314 |
{ |
| 1315 |
e = c->expr; |
| 1316 |
if (e->expr_type == EXPR_ARRAY && e->ts.type == BT_UNKNOWN |
| 1317 |
&& !e->ref && e->value.constructor) |
| 1318 |
{ |
| 1319 |
m = walk_array_constructor (ts, e->value.constructor); |
| 1320 |
if (m == MATCH_ERROR) |
| 1321 |
return m; |
| 1322 |
} |
| 1323 |
else if (!gfc_convert_type_warn (e, ts, 1, 1, true) |
| 1324 |
&& e->ts.type != BT_UNKNOWN) |
| 1325 |
return MATCH_ERROR; |
| 1326 |
} |
| 1327 |
return MATCH_YES; |
| 1328 |
} |
| 1329 |
|
| 1330 |
/* Match an array constructor. */ |
| 1331 |
|
| 1332 |
match |
| 1333 |
gfc_match_array_constructor (gfc_expr **result) |
| 1334 |
{ |
| 1335 |
gfc_constructor *c; |
| 1336 |
gfc_constructor_base head; |
| 1337 |
gfc_expr *expr; |
| 1338 |
gfc_typespec ts; |
| 1339 |
locus where; |
| 1340 |
match m; |
| 1341 |
const char *end_delim; |
| 1342 |
bool seen_ts; |
| 1343 |
|
| 1344 |
head = NULL; |
| 1345 |
seen_ts = false; |
| 1346 |
|
| 1347 |
if (gfc_match (" (/") == MATCH_NO) |
| 1348 |
{ |
| 1349 |
if (gfc_match (" [") == MATCH_NO) |
| 1350 |
return MATCH_NO; |
| 1351 |
else |
| 1352 |
{ |
| 1353 |
if (!gfc_notify_std (GFC_STD_F2003, "[...] " |
| 1354 |
"style array constructors at %C")) |
| 1355 |
return MATCH_ERROR; |
| 1356 |
end_delim = " ]"; |
| 1357 |
} |
| 1358 |
} |
| 1359 |
else |
| 1360 |
end_delim = " /)"; |
| 1361 |
|
| 1362 |
where = gfc_current_locus; |
| 1363 |
|
| 1364 |
/* Try to match an optional "type-spec ::" */ |
| 1365 |
gfc_clear_ts (&ts); |
| 1366 |
m = gfc_match_type_spec (&ts); |
| 1367 |
if (m == MATCH_YES) |
| 1368 |
{ |
| 1369 |
seen_ts = (gfc_match (" ::") == MATCH_YES); |
| 1370 |
|
| 1371 |
if (seen_ts) |
| 1372 |
{ |
| 1373 |
if (!gfc_notify_std (GFC_STD_F2003, "Array constructor " |
| 1374 |
"including type specification at %C")) |
| 1375 |
goto cleanup; |
| 1376 |
|
| 1377 |
if (ts.deferred) |
| 1378 |
{ |
| 1379 |
gfc_error ("Type-spec at %L cannot contain a deferred " |
| 1380 |
"type parameter", &where); |
| 1381 |
goto cleanup; |
| 1382 |
} |
| 1383 |
|
| 1384 |
if (ts.type == BT_CHARACTER |
| 1385 |
&& ts.u.cl && !ts.u.cl->length && !ts.u.cl->length_from_typespec) |
| 1386 |
{ |
| 1387 |
gfc_error ("Type-spec at %L cannot contain an asterisk for a " |
| 1388 |
"type parameter", &where); |
| 1389 |
goto cleanup; |
| 1390 |
} |
| 1391 |
} |
| 1392 |
} |
| 1393 |
else if (m == MATCH_ERROR) |
| 1394 |
goto cleanup; |
| 1395 |
|
| 1396 |
if (!seen_ts) |
| 1397 |
gfc_current_locus = where; |
| 1398 |
|
| 1399 |
if (gfc_match (end_delim) == MATCH_YES) |
| 1400 |
{ |
| 1401 |
if (seen_ts) |
| 1402 |
goto done; |
| 1403 |
else |
| 1404 |
{ |
| 1405 |
gfc_error ("Empty array constructor at %C is not allowed"); |
| 1406 |
goto cleanup; |
| 1407 |
} |
| 1408 |
} |
| 1409 |
|
| 1410 |
for (;;) |
| 1411 |
{ |
| 1412 |
m = match_array_cons_element (&head); |
| 1413 |
if (m == MATCH_ERROR) |
| 1414 |
goto cleanup; |
| 1415 |
if (m == MATCH_NO) |
| 1416 |
goto syntax; |
| 1417 |
|
| 1418 |
if (gfc_match_char (',') == MATCH_NO) |
| 1419 |
break; |
| 1420 |
} |
| 1421 |
|
| 1422 |
if (gfc_match (end_delim) == MATCH_NO) |
| 1423 |
goto syntax; |
| 1424 |
|
| 1425 |
done: |
| 1426 |
/* Size must be calculated at resolution time. */ |
| 1427 |
if (seen_ts) |
| 1428 |
{ |
| 1429 |
expr = gfc_get_array_expr (ts.type, ts.kind, &where); |
| 1430 |
expr->ts = ts; |
| 1431 |
|
| 1432 |
/* If the typespec is CHARACTER, check that array elements can |
| 1433 |
be converted. See PR fortran/67803. */ |
| 1434 |
if (ts.type == BT_CHARACTER) |
| 1435 |
{ |
| 1436 |
c = gfc_constructor_first (head); |
| 1437 |
for (; c; c = gfc_constructor_next (c)) |
| 1438 |
{ |
| 1439 |
if (gfc_numeric_ts (&c->expr->ts) |
| 1440 |
|| c->expr->ts.type == BT_LOGICAL) |
| 1441 |
{ |
| 1442 |
gfc_error ("Incompatible typespec for array element at %L", |
| 1443 |
&c->expr->where); |
| 1444 |
return MATCH_ERROR; |
| 1445 |
} |
| 1446 |
|
| 1447 |
/* Special case null(). */ |
| 1448 |
if (c->expr->expr_type == EXPR_FUNCTION |
| 1449 |
&& c->expr->ts.type == BT_UNKNOWN |
| 1450 |
&& strcmp (c->expr->symtree->name, "null") == 0) |
| 1451 |
{ |
| 1452 |
gfc_error ("Incompatible typespec for array element at %L", |
| 1453 |
&c->expr->where); |
| 1454 |
return MATCH_ERROR; |
| 1455 |
} |
| 1456 |
} |
| 1457 |
} |
| 1458 |
|
| 1459 |
/* Walk the constructor, and if possible, do type conversion for |
| 1460 |
numeric types. */ |
| 1461 |
if (gfc_numeric_ts (&ts)) |
| 1462 |
{ |
| 1463 |
m = walk_array_constructor (&ts, head); |
| 1464 |
if (m == MATCH_ERROR) |
| 1465 |
return m; |
| 1466 |
} |
| 1467 |
} |
| 1468 |
else |
| 1469 |
expr = gfc_get_array_expr (BT_UNKNOWN, 0, &where); |
| 1470 |
|
| 1471 |
expr->value.constructor = head; |
| 1472 |
if (expr->ts.type == BT_CHARACTER && expr->ts.u.cl) |
| 1473 |
expr->ts.u.cl->length_from_typespec = seen_ts; |
| 1474 |
|
| 1475 |
*result = expr; |
| 1476 |
|
| 1477 |
return MATCH_YES; |
| 1478 |
|
| 1479 |
syntax: |
| 1480 |
gfc_error ("Syntax error in array constructor at %C"); |
| 1481 |
|
| 1482 |
cleanup: |
| 1483 |
gfc_constructor_free (head); |
| 1484 |
return MATCH_ERROR; |
| 1485 |
} |
| 1486 |
|
| 1487 |
|
| 1488 |
|
| 1489 |
/************** Check array constructors for correctness **************/ |
| 1490 |
|
| 1491 |
/* Given an expression, compare it's type with the type of the current |
| 1492 |
constructor. Returns nonzero if an error was issued. The |
| 1493 |
cons_state variable keeps track of whether the type of the |
| 1494 |
constructor being read or resolved is known to be good, bad or just |
| 1495 |
starting out. */ |
| 1496 |
|
| 1497 |
static gfc_typespec constructor_ts; |
| 1498 |
static enum |
| 1499 |
{ CONS_START, CONS_GOOD, CONS_BAD } |
| 1500 |
cons_state; |
| 1501 |
|
| 1502 |
static int |
| 1503 |
check_element_type (gfc_expr *expr, bool convert) |
| 1504 |
{ |
| 1505 |
if (cons_state == CONS_BAD) |
| 1506 |
return 0; /* Suppress further errors */ |
| 1507 |
|
| 1508 |
if (cons_state == CONS_START) |
| 1509 |
{ |
| 1510 |
if (expr->ts.type == BT_UNKNOWN) |
| 1511 |
cons_state = CONS_BAD; |
| 1512 |
else |
| 1513 |
{ |
| 1514 |
cons_state = CONS_GOOD; |
| 1515 |
constructor_ts = expr->ts; |
| 1516 |
} |
| 1517 |
|
| 1518 |
return 0; |
| 1519 |
} |
| 1520 |
|
| 1521 |
if (gfc_compare_types (&constructor_ts, &expr->ts)) |
| 1522 |
return 0; |
| 1523 |
|
| 1524 |
if (convert) |
| 1525 |
return gfc_convert_type_warn (expr, &constructor_ts, 1, 1, true) ? 0 : 1; |
| 1526 |
|
| 1527 |
gfc_error ("Element in %s array constructor at %L is %s", |
| 1528 |
gfc_typename (&constructor_ts), &expr->where, |
| 1529 |
gfc_typename (expr)); |
| 1530 |
|
| 1531 |
cons_state = CONS_BAD; |
| 1532 |
return 1; |
| 1533 |
} |
| 1534 |
|
| 1535 |
|
| 1536 |
/* Recursive work function for gfc_check_constructor_type(). */ |
| 1537 |
|
| 1538 |
static bool |
| 1539 |
check_constructor_type (gfc_constructor_base base, bool convert) |
| 1540 |
{ |
| 1541 |
gfc_constructor *c; |
| 1542 |
gfc_expr *e; |
| 1543 |
|
| 1544 |
for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c)) |
| 1545 |
{ |
| 1546 |
e = c->expr; |
| 1547 |
|
| 1548 |
if (e->expr_type == EXPR_ARRAY) |
| 1549 |
{ |
| 1550 |
if (!check_constructor_type (e->value.constructor, convert)) |
| 1551 |
return false; |
| 1552 |
|
| 1553 |
continue; |
| 1554 |
} |
| 1555 |
|
| 1556 |
if (check_element_type (e, convert)) |
| 1557 |
return false; |
| 1558 |
} |
| 1559 |
|
| 1560 |
return true; |
| 1561 |
} |
| 1562 |
|
| 1563 |
|
| 1564 |
/* Check that all elements of an array constructor are the same type. |
| 1565 |
On false, an error has been generated. */ |
| 1566 |
|
| 1567 |
bool |
| 1568 |
gfc_check_constructor_type (gfc_expr *e) |
| 1569 |
{ |
| 1570 |
bool t; |
| 1571 |
|
| 1572 |
if (e->ts.type != BT_UNKNOWN) |
| 1573 |
{ |
| 1574 |
cons_state = CONS_GOOD; |
| 1575 |
constructor_ts = e->ts; |
| 1576 |
} |
| 1577 |
else |
| 1578 |
{ |
| 1579 |
cons_state = CONS_START; |
| 1580 |
gfc_clear_ts (&constructor_ts); |
| 1581 |
} |
| 1582 |
|
| 1583 |
/* If e->ts.type != BT_UNKNOWN, the array constructor included a |
| 1584 |
typespec, and we will now convert the values on the fly. */ |
| 1585 |
t = check_constructor_type (e->value.constructor, e->ts.type != BT_UNKNOWN); |
| 1586 |
if (t && e->ts.type == BT_UNKNOWN) |
| 1587 |
e->ts = constructor_ts; |
| 1588 |
|
| 1589 |
return t; |
| 1590 |
} |
| 1591 |
|
| 1592 |
|
| 1593 |
|
| 1594 |
typedef struct cons_stack |
| 1595 |
{ |
| 1596 |
gfc_iterator *iterator; |
| 1597 |
struct cons_stack *previous; |
| 1598 |
} |
| 1599 |
cons_stack; |
| 1600 |
|
| 1601 |
static cons_stack *base; |
| 1602 |
|
| 1603 |
static bool check_constructor (gfc_constructor_base, bool (*) (gfc_expr *)); |
| 1604 |
|
| 1605 |
/* Check an EXPR_VARIABLE expression in a constructor to make sure |
| 1606 |
that that variable is an iteration variable. */ |
| 1607 |
|
| 1608 |
bool |
| 1609 |
gfc_check_iter_variable (gfc_expr *expr) |
| 1610 |
{ |
| 1611 |
gfc_symbol *sym; |
| 1612 |
cons_stack *c; |
| 1613 |
|
| 1614 |
sym = expr->symtree->n.sym; |
| 1615 |
|
| 1616 |
for (c = base; c && c->iterator; c = c->previous) |
| 1617 |
if (sym == c->iterator->var->symtree->n.sym) |
| 1618 |
return true; |
| 1619 |
|
| 1620 |
return false; |
| 1621 |
} |
| 1622 |
|
| 1623 |
|
| 1624 |
/* Recursive work function for gfc_check_constructor(). This amounts |
| 1625 |
to calling the check function for each expression in the |
| 1626 |
constructor, giving variables with the names of iterators a pass. */ |
| 1627 |
|
| 1628 |
static bool |
| 1629 |
check_constructor (gfc_constructor_base ctor, bool (*check_function) (gfc_expr *)) |
| 1630 |
{ |
| 1631 |
cons_stack element; |
| 1632 |
gfc_expr *e; |
| 1633 |
bool t; |
| 1634 |
gfc_constructor *c; |
| 1635 |
|
| 1636 |
for (c = gfc_constructor_first (ctor); c; c = gfc_constructor_next (c)) |
| 1637 |
{ |
| 1638 |
e = c->expr; |
| 1639 |
|
| 1640 |
if (!e) |
| 1641 |
continue; |
| 1642 |
|
| 1643 |
if (e->expr_type != EXPR_ARRAY) |
| 1644 |
{ |
| 1645 |
if (!(*check_function)(e)) |
| 1646 |
return false; |
| 1647 |
continue; |
| 1648 |
} |
| 1649 |
|
| 1650 |
element.previous = base; |
| 1651 |
element.iterator = c->iterator; |
| 1652 |
|
| 1653 |
base = &element; |
| 1654 |
t = check_constructor (e->value.constructor, check_function); |
| 1655 |
base = element.previous; |
| 1656 |
|
| 1657 |
if (!t) |
| 1658 |
return false; |
| 1659 |
} |
| 1660 |
|
| 1661 |
/* Nothing went wrong, so all OK. */ |
| 1662 |
return true; |
| 1663 |
} |
| 1664 |
|
| 1665 |
|
| 1666 |
/* Checks a constructor to see if it is a particular kind of |
| 1667 |
expression -- specification, restricted, or initialization as |
| 1668 |
determined by the check_function. */ |
| 1669 |
|
| 1670 |
bool |
| 1671 |
gfc_check_constructor (gfc_expr *expr, bool (*check_function) (gfc_expr *)) |
| 1672 |
{ |
| 1673 |
cons_stack *base_save; |
| 1674 |
bool t; |
| 1675 |
|
| 1676 |
base_save = base; |
| 1677 |
base = NULL; |
| 1678 |
|
| 1679 |
t = check_constructor (expr->value.constructor, check_function); |
| 1680 |
base = base_save; |
| 1681 |
|
| 1682 |
return t; |
| 1683 |
} |
| 1684 |
|
| 1685 |
|
| 1686 |
|
| 1687 |
/**************** Simplification of array constructors ****************/ |
| 1688 |
|
| 1689 |
iterator_stack *iter_stack; |
| 1690 |
|
| 1691 |
typedef struct |
| 1692 |
{ |
| 1693 |
gfc_constructor_base base; |
| 1694 |
int extract_count, extract_n; |
| 1695 |
gfc_expr *extracted; |
| 1696 |
mpz_t *count; |
| 1697 |
|
| 1698 |
mpz_t *offset; |
| 1699 |
gfc_component *component; |
| 1700 |
mpz_t *repeat; |
| 1701 |
|
| 1702 |
bool (*expand_work_function) (gfc_expr *); |
| 1703 |
} |
| 1704 |
expand_info; |
| 1705 |
|
| 1706 |
static expand_info current_expand; |
| 1707 |
|
| 1708 |
static bool expand_constructor (gfc_constructor_base); |
| 1709 |
|
| 1710 |
|
| 1711 |
/* Work function that counts the number of elements present in a |
| 1712 |
constructor. */ |
| 1713 |
|
| 1714 |
static bool |
| 1715 |
count_elements (gfc_expr *e) |
| 1716 |
{ |
| 1717 |
mpz_t result; |
| 1718 |
|
| 1719 |
if (e->rank == 0) |
| 1720 |
mpz_add_ui (*current_expand.count, *current_expand.count, 1); |
| 1721 |
else |
| 1722 |
{ |
| 1723 |
if (!gfc_array_size (e, &result)) |
| 1724 |
{ |
| 1725 |
gfc_free_expr (e); |
| 1726 |
return false; |
| 1727 |
} |
| 1728 |
|
| 1729 |
mpz_add (*current_expand.count, *current_expand.count, result); |
| 1730 |
mpz_clear (result); |
| 1731 |
} |
| 1732 |
|
| 1733 |
gfc_free_expr (e); |
| 1734 |
return true; |
| 1735 |
} |
| 1736 |
|
| 1737 |
|
| 1738 |
/* Work function that extracts a particular element from an array |
| 1739 |
constructor, freeing the rest. */ |
| 1740 |
|
| 1741 |
static bool |
| 1742 |
extract_element (gfc_expr *e) |
| 1743 |
{ |
| 1744 |
if (e->rank != 0) |
| 1745 |
{ /* Something unextractable */ |
| 1746 |
gfc_free_expr (e); |
| 1747 |
return false; |
| 1748 |
} |
| 1749 |
|
| 1750 |
if (current_expand.extract_count == current_expand.extract_n) |
| 1751 |
current_expand.extracted = e; |
| 1752 |
else |
| 1753 |
gfc_free_expr (e); |
| 1754 |
|
| 1755 |
current_expand.extract_count++; |
| 1756 |
|
| 1757 |
return true; |
| 1758 |
} |
| 1759 |
|
| 1760 |
|
| 1761 |
/* Work function that constructs a new constructor out of the old one, |
| 1762 |
stringing new elements together. */ |
| 1763 |
|
| 1764 |
static bool |
| 1765 |
expand (gfc_expr *e) |
| 1766 |
{ |
| 1767 |
gfc_constructor *c = gfc_constructor_append_expr (¤t_expand.base, |
| 1768 |
e, &e->where); |
| 1769 |
|
| 1770 |
c->n.component = current_expand.component; |
| 1771 |
return true; |
| 1772 |
} |
| 1773 |
|
| 1774 |
|
| 1775 |
/* Given an initialization expression that is a variable reference, |
| 1776 |
substitute the current value of the iteration variable. */ |
| 1777 |
|
| 1778 |
void |
| 1779 |
gfc_simplify_iterator_var (gfc_expr *e) |
| 1780 |
{ |
| 1781 |
iterator_stack *p; |
| 1782 |
|
| 1783 |
for (p = iter_stack; p; p = p->prev) |
| 1784 |
if (e->symtree == p->variable) |
| 1785 |
break; |
| 1786 |
|
| 1787 |
if (p == NULL) |
| 1788 |
return; /* Variable not found */ |
| 1789 |
|
| 1790 |
gfc_replace_expr (e, gfc_get_int_expr (gfc_default_integer_kind, NULL, 0)); |
| 1791 |
|
| 1792 |
mpz_set (e->value.integer, p->value); |
| 1793 |
|
| 1794 |
return; |
| 1795 |
} |
| 1796 |
|
| 1797 |
|
| 1798 |
/* Expand an expression with that is inside of a constructor, |
| 1799 |
recursing into other constructors if present. */ |
| 1800 |
|
| 1801 |
static bool |
| 1802 |
expand_expr (gfc_expr *e) |
| 1803 |
{ |
| 1804 |
if (e->expr_type == EXPR_ARRAY) |
| 1805 |
return expand_constructor (e->value.constructor); |
| 1806 |
|
| 1807 |
e = gfc_copy_expr (e); |
| 1808 |
|
| 1809 |
if (!gfc_simplify_expr (e, 1)) |
| 1810 |
{ |
| 1811 |
gfc_free_expr (e); |
| 1812 |
return false; |
| 1813 |
} |
| 1814 |
|
| 1815 |
return current_expand.expand_work_function (e); |
| 1816 |
} |
| 1817 |
|
| 1818 |
|
| 1819 |
static bool |
| 1820 |
expand_iterator (gfc_constructor *c) |
| 1821 |
{ |
| 1822 |
gfc_expr *start, *end, *step; |
| 1823 |
iterator_stack frame; |
| 1824 |
mpz_t trip; |
| 1825 |
bool t; |
| 1826 |
|
| 1827 |
end = step = NULL; |
| 1828 |
|
| 1829 |
t = false; |
| 1830 |
|
| 1831 |
mpz_init (trip); |
| 1832 |
mpz_init (frame.value); |
| 1833 |
frame.prev = NULL; |
| 1834 |
|
| 1835 |
start = gfc_copy_expr (c->iterator->start); |
| 1836 |
if (!gfc_simplify_expr (start, 1)) |
| 1837 |
goto cleanup; |
| 1838 |
|
| 1839 |
if (start->expr_type != EXPR_CONSTANT || start->ts.type != BT_INTEGER) |
| 1840 |
goto cleanup; |
| 1841 |
|
| 1842 |
end = gfc_copy_expr (c->iterator->end); |
| 1843 |
if (!gfc_simplify_expr (end, 1)) |
| 1844 |
goto cleanup; |
| 1845 |
|
| 1846 |
if (end->expr_type != EXPR_CONSTANT || end->ts.type != BT_INTEGER) |
| 1847 |
goto cleanup; |
| 1848 |
|
| 1849 |
step = gfc_copy_expr (c->iterator->step); |
| 1850 |
if (!gfc_simplify_expr (step, 1)) |
| 1851 |
goto cleanup; |
| 1852 |
|
| 1853 |
if (step->expr_type != EXPR_CONSTANT || step->ts.type != BT_INTEGER) |
| 1854 |
goto cleanup; |
| 1855 |
|
| 1856 |
if (mpz_sgn (step->value.integer) == 0) |
| 1857 |
{ |
| 1858 |
gfc_error ("Iterator step at %L cannot be zero", &step->where); |
| 1859 |
goto cleanup; |
| 1860 |
} |
| 1861 |
|
| 1862 |
/* Calculate the trip count of the loop. */ |
| 1863 |
mpz_sub (trip, end->value.integer, start->value.integer); |
| 1864 |
mpz_add (trip, trip, step->value.integer); |
| 1865 |
mpz_tdiv_q (trip, trip, step->value.integer); |
| 1866 |
|
| 1867 |
mpz_set (frame.value, start->value.integer); |
| 1868 |
|
| 1869 |
frame.prev = iter_stack; |
| 1870 |
frame.variable = c->iterator->var->symtree; |
| 1871 |
iter_stack = &frame; |
| 1872 |
|
| 1873 |
while (mpz_sgn (trip) > 0) |
| 1874 |
{ |
| 1875 |
if (!expand_expr (c->expr)) |
| 1876 |
goto cleanup; |
| 1877 |
|
| 1878 |
mpz_add (frame.value, frame.value, step->value.integer); |
| 1879 |
mpz_sub_ui (trip, trip, 1); |
| 1880 |
} |
| 1881 |
|
| 1882 |
t = true; |
| 1883 |
|
| 1884 |
cleanup: |
| 1885 |
gfc_free_expr (start); |
| 1886 |
gfc_free_expr (end); |
| 1887 |
gfc_free_expr (step); |
| 1888 |
|
| 1889 |
mpz_clear (trip); |
| 1890 |
mpz_clear (frame.value); |
| 1891 |
|
| 1892 |
iter_stack = frame.prev; |
| 1893 |
|
| 1894 |
return t; |
| 1895 |
} |
| 1896 |
|
| 1897 |
/* Variables for noticing if all constructors are empty, and |
| 1898 |
if any of them had a type. */ |
| 1899 |
|
| 1900 |
static bool empty_constructor; |
| 1901 |
static gfc_typespec empty_ts; |
| 1902 |
|
| 1903 |
/* Expand a constructor into constant constructors without any |
| 1904 |
iterators, calling the work function for each of the expanded |
| 1905 |
expressions. The work function needs to either save or free the |
| 1906 |
passed expression. */ |
| 1907 |
|
| 1908 |
static bool |
| 1909 |
expand_constructor (gfc_constructor_base base) |
| 1910 |
{ |
| 1911 |
gfc_constructor *c; |
| 1912 |
gfc_expr *e; |
| 1913 |
|
| 1914 |
for (c = gfc_constructor_first (base); c; c = gfc_constructor_next(c)) |
| 1915 |
{ |
| 1916 |
if (c->iterator != NULL) |
| 1917 |
{ |
| 1918 |
if (!expand_iterator (c)) |
| 1919 |
return false; |
| 1920 |
continue; |
| 1921 |
} |
| 1922 |
|
| 1923 |
e = c->expr; |
| 1924 |
|
| 1925 |
if (e == NULL) |
| 1926 |
return false; |
| 1927 |
|
| 1928 |
if (empty_constructor) |
| 1929 |
empty_ts = e->ts; |
| 1930 |
|
| 1931 |
/* Simplify constant array expression/section within constructor. */ |
| 1932 |
if (e->expr_type == EXPR_VARIABLE && e->rank > 0 && e->ref |
| 1933 |
&& e->symtree && e->symtree->n.sym |
| 1934 |
&& e->symtree->n.sym->attr.flavor == FL_PARAMETER) |
| 1935 |
gfc_simplify_expr (e, 0); |
| 1936 |
|
| 1937 |
if (e->expr_type == EXPR_ARRAY) |
| 1938 |
{ |
| 1939 |
if (!expand_constructor (e->value.constructor)) |
| 1940 |
return false; |
| 1941 |
|
| 1942 |
continue; |
| 1943 |
} |
| 1944 |
|
| 1945 |
empty_constructor = false; |
| 1946 |
e = gfc_copy_expr (e); |
| 1947 |
if (!gfc_simplify_expr (e, 1)) |
| 1948 |
{ |
| 1949 |
gfc_free_expr (e); |
| 1950 |
return false; |
| 1951 |
} |
| 1952 |
e->from_constructor = 1; |
| 1953 |
current_expand.offset = &c->offset; |
| 1954 |
current_expand.repeat = &c->repeat; |
| 1955 |
current_expand.component = c->n.component; |
| 1956 |
if (!current_expand.expand_work_function(e)) |
| 1957 |
return false; |
| 1958 |
} |
| 1959 |
return true; |
| 1960 |
} |
| 1961 |
|
| 1962 |
|
| 1963 |
/* Given an array expression and an element number (starting at zero), |
| 1964 |
return a pointer to the array element. NULL is returned if the |
| 1965 |
size of the array has been exceeded. The expression node returned |
| 1966 |
remains a part of the array and should not be freed. Access is not |
| 1967 |
efficient at all, but this is another place where things do not |
| 1968 |
have to be particularly fast. */ |
| 1969 |
|
| 1970 |
static gfc_expr * |
| 1971 |
gfc_get_array_element (gfc_expr *array, int element) |
| 1972 |
{ |
| 1973 |
expand_info expand_save; |
| 1974 |
gfc_expr *e; |
| 1975 |
bool rc; |
| 1976 |
|
| 1977 |
expand_save = current_expand; |
| 1978 |
current_expand.extract_n = element; |
| 1979 |
current_expand.expand_work_function = extract_element; |
| 1980 |
current_expand.extracted = NULL; |
| 1981 |
current_expand.extract_count = 0; |
| 1982 |
|
| 1983 |
iter_stack = NULL; |
| 1984 |
|
| 1985 |
rc = expand_constructor (array->value.constructor); |
| 1986 |
e = current_expand.extracted; |
| 1987 |
current_expand = expand_save; |
| 1988 |
|
| 1989 |
if (!rc) |
| 1990 |
return NULL; |
| 1991 |
|
| 1992 |
return e; |
| 1993 |
} |
| 1994 |
|
| 1995 |
|
| 1996 |
/* Top level subroutine for expanding constructors. We only expand |
| 1997 |
constructor if they are small enough. */ |
| 1998 |
|
| 1999 |
bool |
| 2000 |
gfc_expand_constructor (gfc_expr *e, bool fatal) |
| 2001 |
{ |
| 2002 |
expand_info expand_save; |
| 2003 |
gfc_expr *f; |
| 2004 |
bool rc; |
| 2005 |
|
| 2006 |
if (gfc_is_size_zero_array (e)) |
| 2007 |
return true; |
| 2008 |
|
| 2009 |
/* If we can successfully get an array element at the max array size then |
| 2010 |
the array is too big to expand, so we just return. */ |
| 2011 |
f = gfc_get_array_element (e, flag_max_array_constructor); |
| 2012 |
if (f != NULL) |
| 2013 |
{ |
| 2014 |
gfc_free_expr (f); |
| 2015 |
if (fatal) |
| 2016 |
{ |
| 2017 |
gfc_error ("The number of elements in the array constructor " |
| 2018 |
"at %L requires an increase of the allowed %d " |
| 2019 |
"upper limit. See %<-fmax-array-constructor%> " |
| 2020 |
"option", &e->where, flag_max_array_constructor); |
| 2021 |
return false; |
| 2022 |
} |
| 2023 |
return true; |
| 2024 |
} |
| 2025 |
|
| 2026 |
/* We now know the array is not too big so go ahead and try to expand it. */ |
| 2027 |
expand_save = current_expand; |
| 2028 |
current_expand.base = NULL; |
| 2029 |
|
| 2030 |
iter_stack = NULL; |
| 2031 |
|
| 2032 |
empty_constructor = true; |
| 2033 |
gfc_clear_ts (&empty_ts); |
| 2034 |
current_expand.expand_work_function = expand; |
| 2035 |
|
| 2036 |
if (!expand_constructor (e->value.constructor)) |
| 2037 |
{ |
| 2038 |
gfc_constructor_free (current_expand.base); |
| 2039 |
rc = false; |
| 2040 |
goto done; |
| 2041 |
} |
| 2042 |
|
| 2043 |
/* If we don't have an explicit constructor type, and there |
| 2044 |
were only empty constructors, then take the type from |
| 2045 |
them. */ |
| 2046 |
|
| 2047 |
if (constructor_ts.type == BT_UNKNOWN && empty_constructor) |
| 2048 |
e->ts = empty_ts; |
| 2049 |
|
| 2050 |
gfc_constructor_free (e->value.constructor); |
| 2051 |
e->value.constructor = current_expand.base; |
| 2052 |
|
| 2053 |
rc = true; |
| 2054 |
|
| 2055 |
done: |
| 2056 |
current_expand = expand_save; |
| 2057 |
|
| 2058 |
return rc; |
| 2059 |
} |
| 2060 |
|
| 2061 |
|
| 2062 |
/* Work function for checking that an element of a constructor is a |
| 2063 |
constant, after removal of any iteration variables. We return |
| 2064 |
false if not so. */ |
| 2065 |
|
| 2066 |
static bool |
| 2067 |
is_constant_element (gfc_expr *e) |
| 2068 |
{ |
| 2069 |
int rv; |
| 2070 |
|
| 2071 |
rv = gfc_is_constant_expr (e); |
| 2072 |
gfc_free_expr (e); |
| 2073 |
|
| 2074 |
return rv ? true : false; |
| 2075 |
} |
| 2076 |
|
| 2077 |
|
| 2078 |
/* Given an array constructor, determine if the constructor is |
| 2079 |
constant or not by expanding it and making sure that all elements |
| 2080 |
are constants. This is a bit of a hack since something like (/ (i, |
| 2081 |
i=1,100000000) /) will take a while as* opposed to a more clever |
| 2082 |
function that traverses the expression tree. FIXME. */ |
| 2083 |
|
| 2084 |
bool |
| 2085 |
gfc_constant_ac (gfc_expr *e) |
| 2086 |
{ |
| 2087 |
expand_info expand_save; |
| 2088 |
bool rc; |
| 2089 |
|
| 2090 |
iter_stack = NULL; |
| 2091 |
expand_save = current_expand; |
| 2092 |
current_expand.expand_work_function = is_constant_element; |
| 2093 |
|
| 2094 |
rc = expand_constructor (e->value.constructor); |
| 2095 |
|
| 2096 |
current_expand = expand_save; |
| 2097 |
if (!rc) |
| 2098 |
return 0; |
| 2099 |
|
| 2100 |
return 1; |
| 2101 |
} |
| 2102 |
|
| 2103 |
|
| 2104 |
/* Returns nonzero if an array constructor has been completely |
| 2105 |
expanded (no iterators) and zero if iterators are present. */ |
| 2106 |
|
| 2107 |
bool |
| 2108 |
gfc_expanded_ac (gfc_expr *e) |
| 2109 |
{ |
| 2110 |
gfc_constructor *c; |
| 2111 |
|
| 2112 |
if (e->expr_type == EXPR_ARRAY) |
| 2113 |
for (c = gfc_constructor_first (e->value.constructor); |
| 2114 |
c; c = gfc_constructor_next (c)) |
| 2115 |
if (c->iterator != NULL || !gfc_expanded_ac (c->expr)) |
| 2116 |
return 0; |
| 2117 |
|
| 2118 |
return 1; |
| 2119 |
} |
| 2120 |
|
| 2121 |
|
| 2122 |
/*************** Type resolution of array constructors ***************/ |
| 2123 |
|
| 2124 |
|
| 2125 |
/* The symbol expr_is_sought_symbol_ref will try to find. */ |
| 2126 |
static const gfc_symbol *sought_symbol = NULL; |
| 2127 |
|
| 2128 |
|
| 2129 |
/* Tells whether the expression E is a variable reference to the symbol |
| 2130 |
in the static variable SOUGHT_SYMBOL, and sets the locus pointer WHERE |
| 2131 |
accordingly. |
| 2132 |
To be used with gfc_expr_walker: if a reference is found we don't need |
| 2133 |
to look further so we return 1 to skip any further walk. */ |
| 2134 |
|
| 2135 |
static int |
| 2136 |
expr_is_sought_symbol_ref (gfc_expr **e, int *walk_subtrees ATTRIBUTE_UNUSED, |
| 2137 |
void *where) |
| 2138 |
{ |
| 2139 |
gfc_expr *expr = *e; |
| 2140 |
locus *sym_loc = (locus *)where; |
| 2141 |
|
| 2142 |
if (expr->expr_type == EXPR_VARIABLE |
| 2143 |
&& expr->symtree->n.sym == sought_symbol) |
| 2144 |
{ |
| 2145 |
*sym_loc = expr->where; |
| 2146 |
return 1; |
| 2147 |
} |
| 2148 |
|
| 2149 |
return 0; |
| 2150 |
} |
| 2151 |
|
| 2152 |
|
| 2153 |
/* Tells whether the expression EXPR contains a reference to the symbol |
| 2154 |
SYM and in that case sets the position SYM_LOC where the reference is. */ |
| 2155 |
|
| 2156 |
static bool |
| 2157 |
find_symbol_in_expr (gfc_symbol *sym, gfc_expr *expr, locus *sym_loc) |
| 2158 |
{ |
| 2159 |
int ret; |
| 2160 |
|
| 2161 |
sought_symbol = sym; |
| 2162 |
ret = gfc_expr_walker (&expr, &expr_is_sought_symbol_ref, sym_loc); |
| 2163 |
sought_symbol = NULL; |
| 2164 |
return ret; |
| 2165 |
} |
| 2166 |
|
| 2167 |
|
| 2168 |
/* Recursive array list resolution function. All of the elements must |
| 2169 |
be of the same type. */ |
| 2170 |
|
| 2171 |
static bool |
| 2172 |
resolve_array_list (gfc_constructor_base base) |
| 2173 |
{ |
| 2174 |
bool t; |
| 2175 |
gfc_constructor *c; |
| 2176 |
gfc_iterator *iter; |
| 2177 |
|
| 2178 |
t = true; |
| 2179 |
|
| 2180 |
for (c = gfc_constructor_first (base); c; c = gfc_constructor_next (c)) |
| 2181 |
{ |
| 2182 |
iter = c->iterator; |
| 2183 |
if (iter != NULL) |
| 2184 |
{ |
| 2185 |
gfc_symbol *iter_var; |
| 2186 |
locus iter_var_loc; |
| 2187 |
|
| 2188 |
if (!gfc_resolve_iterator (iter, false, true)) |
| 2189 |
t = false; |
| 2190 |
|
| 2191 |
/* Check for bounds referencing the iterator variable. */ |
| 2192 |
gcc_assert (iter->var->expr_type == EXPR_VARIABLE); |
| 2193 |
iter_var = iter->var->symtree->n.sym; |
| 2194 |
if (find_symbol_in_expr (iter_var, iter->start, &iter_var_loc)) |
| 2195 |
{ |
| 2196 |
if (!gfc_notify_std (GFC_STD_LEGACY, "AC-IMPLIED-DO initial " |
| 2197 |
"expression references control variable " |
| 2198 |
"at %L", &iter_var_loc)) |
| 2199 |
t = false; |
| 2200 |
} |
| 2201 |
if (find_symbol_in_expr (iter_var, iter->end, &iter_var_loc)) |
| 2202 |
{ |
| 2203 |
if (!gfc_notify_std (GFC_STD_LEGACY, "AC-IMPLIED-DO final " |
| 2204 |
"expression references control variable " |
| 2205 |
"at %L", &iter_var_loc)) |
| 2206 |
t = false; |
| 2207 |
} |
| 2208 |
if (find_symbol_in_expr (iter_var, iter->step, &iter_var_loc)) |
| 2209 |
{ |
| 2210 |
if (!gfc_notify_std (GFC_STD_LEGACY, "AC-IMPLIED-DO step " |
| 2211 |
"expression references control variable " |
| 2212 |
"at %L", &iter_var_loc)) |
| 2213 |
t = false; |
| 2214 |
} |
| 2215 |
} |
| 2216 |
|
| 2217 |
if (!gfc_resolve_expr (c->expr)) |
| 2218 |
t = false; |
| 2219 |
|
| 2220 |
if (UNLIMITED_POLY (c->expr)) |
| 2221 |
{ |
| 2222 |
gfc_error ("Array constructor value at %L shall not be unlimited " |
| 2223 |
"polymorphic [F2008: C4106]", &c->expr->where); |
| 2224 |
t = false; |
| 2225 |
} |
| 2226 |
|
| 2227 |
/* F2018:C7114 The declared type of an ac-value shall not be abstract. */ |
| 2228 |
if (c->expr->ts.type == BT_CLASS |
| 2229 |
&& c->expr->ts.u.derived |
| 2230 |
&& c->expr->ts.u.derived->attr.abstract |
| 2231 |
&& CLASS_DATA (c->expr)) |
| 2232 |
{ |
| 2233 |
gfc_error ("Array constructor value %qs at %L is of the ABSTRACT " |
| 2234 |
"type %qs", c->expr->symtree->name, &c->expr->where, |
| 2235 |
CLASS_DATA (c->expr)->ts.u.derived->name); |
| 2236 |
t = false; |
| 2237 |
} |
| 2238 |
|
| 2239 |
} |
| 2240 |
|
| 2241 |
return t; |
| 2242 |
} |
| 2243 |
|
| 2244 |
/* Resolve character array constructor. If it has a specified constant character |
| 2245 |
length, pad/truncate the elements here; if the length is not specified and |
| 2246 |
all elements are of compile-time known length, emit an error as this is |
| 2247 |
invalid. */ |
| 2248 |
|
| 2249 |
bool |
| 2250 |
gfc_resolve_character_array_constructor (gfc_expr *expr) |
| 2251 |
{ |
| 2252 |
gfc_constructor *p; |
| 2253 |
HOST_WIDE_INT found_length; |
| 2254 |
|
| 2255 |
gcc_assert (expr->expr_type == EXPR_ARRAY); |
| 2256 |
gcc_assert (expr->ts.type == BT_CHARACTER); |
| 2257 |
|
| 2258 |
if (expr->ts.u.cl == NULL) |
| 2259 |
{ |
| 2260 |
for (p = gfc_constructor_first (expr->value.constructor); |
| 2261 |
p; p = gfc_constructor_next (p)) |
| 2262 |
if (p->expr->ts.u.cl != NULL) |
| 2263 |
{ |
| 2264 |
/* Ensure that if there is a char_len around that it is |
| 2265 |
used; otherwise the middle-end confuses them! */ |
| 2266 |
expr->ts.u.cl = p->expr->ts.u.cl; |
| 2267 |
goto got_charlen; |
| 2268 |
} |
| 2269 |
|
| 2270 |
expr->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL); |
| 2271 |
} |
| 2272 |
|
| 2273 |
got_charlen: |
| 2274 |
|
| 2275 |
/* Early exit for zero size arrays. */ |
| 2276 |
if (expr->shape) |
| 2277 |
{ |
| 2278 |
mpz_t size; |
| 2279 |
HOST_WIDE_INT arraysize; |
| 2280 |
|
| 2281 |
gfc_array_size (expr, &size); |
| 2282 |
arraysize = mpz_get_ui (size); |
| 2283 |
mpz_clear (size); |
| 2284 |
|
| 2285 |
if (arraysize == 0) |
| 2286 |
return true; |
| 2287 |
} |
| 2288 |
|
| 2289 |
found_length = -1; |
| 2290 |
|
| 2291 |
if (expr->ts.u.cl->length == NULL) |
| 2292 |
{ |
| 2293 |
/* Check that all constant string elements have the same length until |
| 2294 |
we reach the end or find a variable-length one. */ |
| 2295 |
|
| 2296 |
for (p = gfc_constructor_first (expr->value.constructor); |
| 2297 |
p; p = gfc_constructor_next (p)) |
| 2298 |
{ |
| 2299 |
HOST_WIDE_INT current_length = -1; |
| 2300 |
gfc_ref *ref; |
| 2301 |
for (ref = p->expr->ref; ref; ref = ref->next) |
| 2302 |
if (ref->type == REF_SUBSTRING |
| 2303 |
&& ref->u.ss.start |
| 2304 |
&& ref->u.ss.start->expr_type == EXPR_CONSTANT |
| 2305 |
&& ref->u.ss.end |
| 2306 |
&& ref->u.ss.end->expr_type == EXPR_CONSTANT) |
| 2307 |
break; |
| 2308 |
|
| 2309 |
if (p->expr->expr_type == EXPR_CONSTANT) |
| 2310 |
current_length = p->expr->value.character.length; |
| 2311 |
else if (ref) |
| 2312 |
current_length = gfc_mpz_get_hwi (ref->u.ss.end->value.integer) |
| 2313 |
- gfc_mpz_get_hwi (ref->u.ss.start->value.integer) + 1; |
| 2314 |
else if (p->expr->ts.u.cl && p->expr->ts.u.cl->length |
| 2315 |
&& p->expr->ts.u.cl->length->expr_type == EXPR_CONSTANT) |
| 2316 |
current_length = gfc_mpz_get_hwi (p->expr->ts.u.cl->length->value.integer); |
| 2317 |
else |
| 2318 |
return true; |
| 2319 |
|
| 2320 |
if (current_length < 0) |
| 2321 |
current_length = 0; |
| 2322 |
|
| 2323 |
if (found_length == -1) |
| 2324 |
found_length = current_length; |
| 2325 |
else if (found_length != current_length) |
| 2326 |
{ |
| 2327 |
gfc_error ("Different CHARACTER lengths (%wd/%wd) in array" |
| 2328 |
" constructor at %L", found_length, |
| 2329 |
current_length, &p->expr->where); |
| 2330 |
return false; |
| 2331 |
} |
| 2332 |
|
| 2333 |
gcc_assert (found_length == current_length); |
| 2334 |
} |
| 2335 |
|
| 2336 |
gcc_assert (found_length != -1); |
| 2337 |
|
| 2338 |
/* Update the character length of the array constructor. */ |
| 2339 |
expr->ts.u.cl->length = gfc_get_int_expr (gfc_charlen_int_kind, |
| 2340 |
NULL, found_length); |
| 2341 |
} |
| 2342 |
else |
| 2343 |
{ |
| 2344 |
/* We've got a character length specified. It should be an integer, |
| 2345 |
otherwise an error is signalled elsewhere. */ |
| 2346 |
gcc_assert (expr->ts.u.cl->length); |
| 2347 |
|
| 2348 |
/* If we've got a constant character length, pad according to this. |
| 2349 |
gfc_extract_int does check for BT_INTEGER and EXPR_CONSTANT and sets |
| 2350 |
max_length only if they pass. */ |
| 2351 |
gfc_extract_hwi (expr->ts.u.cl->length, &found_length); |
| 2352 |
|
| 2353 |
/* Now pad/truncate the elements accordingly to the specified character |
| 2354 |
length. This is ok inside this conditional, as in the case above |
| 2355 |
(without typespec) all elements are verified to have the same length |
| 2356 |
anyway. */ |
| 2357 |
if (found_length != -1) |
| 2358 |
for (p = gfc_constructor_first (expr->value.constructor); |
| 2359 |
p; p = gfc_constructor_next (p)) |
| 2360 |
if (p->expr->expr_type == EXPR_CONSTANT) |
| 2361 |
{ |
| 2362 |
gfc_expr *cl = NULL; |
| 2363 |
HOST_WIDE_INT current_length = -1; |
| 2364 |
bool has_ts; |
| 2365 |
|
| 2366 |
if (p->expr->ts.u.cl && p->expr->ts.u.cl->length) |
| 2367 |
{ |
| 2368 |
cl = p->expr->ts.u.cl->length; |
| 2369 |
gfc_extract_hwi (cl, ¤t_length); |
| 2370 |
} |
| 2371 |
|
| 2372 |
/* If gfc_extract_int above set current_length, we implicitly |
| 2373 |
know the type is BT_INTEGER and it's EXPR_CONSTANT. */ |
| 2374 |
|
| 2375 |
has_ts = expr->ts.u.cl->length_from_typespec; |
| 2376 |
|
| 2377 |
if (! cl |
| 2378 |
|| (current_length != -1 && current_length != found_length)) |
| 2379 |
gfc_set_constant_character_len (found_length, p->expr, |
| 2380 |
has_ts ? -1 : found_length); |
| 2381 |
} |
| 2382 |
} |
| 2383 |
|
| 2384 |
return true; |
| 2385 |
} |
| 2386 |
|
| 2387 |
|
| 2388 |
/* Resolve all of the expressions in an array list. */ |
| 2389 |
|
| 2390 |
bool |
| 2391 |
gfc_resolve_array_constructor (gfc_expr *expr) |
| 2392 |
{ |
| 2393 |
bool t; |
| 2394 |
|
| 2395 |
t = resolve_array_list (expr->value.constructor); |
| 2396 |
if (t) |
| 2397 |
t = gfc_check_constructor_type (expr); |
| 2398 |
|
| 2399 |
/* gfc_resolve_character_array_constructor is called in gfc_resolve_expr after |
| 2400 |
the call to this function, so we don't need to call it here; if it was |
| 2401 |
called twice, an error message there would be duplicated. */ |
| 2402 |
|
| 2403 |
return t; |
| 2404 |
} |
| 2405 |
|
| 2406 |
|
| 2407 |
/* Copy an iterator structure. */ |
| 2408 |
|
| 2409 |
gfc_iterator * |
| 2410 |
gfc_copy_iterator (gfc_iterator *src) |
| 2411 |
{ |
| 2412 |
gfc_iterator *dest; |
| 2413 |
|
| 2414 |
if (src == NULL) |
| 2415 |
return NULL; |
| 2416 |
|
| 2417 |
dest = gfc_get_iterator (); |
| 2418 |
|
| 2419 |
dest->var = gfc_copy_expr (src->var); |
| 2420 |
dest->start = gfc_copy_expr (src->start); |
| 2421 |
dest->end = gfc_copy_expr (src->end); |
| 2422 |
dest->step = gfc_copy_expr (src->step); |
| 2423 |
dest->annot = src->annot; |
| 2424 |
|
| 2425 |
return dest; |
| 2426 |
} |
| 2427 |
|
| 2428 |
|
| 2429 |
/********* Subroutines for determining the size of an array *********/ |
| 2430 |
|
| 2431 |
/* These are needed just to accommodate RESHAPE(). There are no |
| 2432 |
diagnostics here, we just return false if something goes wrong. */ |
| 2433 |
|
| 2434 |
|
| 2435 |
/* Get the size of single dimension of an array specification. The |
| 2436 |
array is guaranteed to be one dimensional. */ |
| 2437 |
|
| 2438 |
bool |
| 2439 |
spec_dimen_size (gfc_array_spec *as, int dimen, mpz_t *result) |
| 2440 |
{ |
| 2441 |
if (as == NULL) |
| 2442 |
return false; |
| 2443 |
|
| 2444 |
if (dimen < 0 || dimen > as->rank - 1) |
| 2445 |
gfc_internal_error ("spec_dimen_size(): Bad dimension"); |
| 2446 |
|
| 2447 |
if (as->type != AS_EXPLICIT |
| 2448 |
|| !as->lower[dimen] |
| 2449 |
|| !as->upper[dimen]) |
| 2450 |
return false; |
| 2451 |
|
| 2452 |
if (as->lower[dimen]->expr_type != EXPR_CONSTANT |
| 2453 |
|| as->upper[dimen]->expr_type != EXPR_CONSTANT |
| 2454 |
|| as->lower[dimen]->ts.type != BT_INTEGER |
| 2455 |
|| as->upper[dimen]->ts.type != BT_INTEGER) |
| 2456 |
return false; |
| 2457 |
|
| 2458 |
mpz_init (*result); |
| 2459 |
|
| 2460 |
mpz_sub (*result, as->upper[dimen]->value.integer, |
| 2461 |
as->lower[dimen]->value.integer); |
| 2462 |
|
| 2463 |
mpz_add_ui (*result, *result, 1); |
| 2464 |
|
| 2465 |
if (mpz_cmp_si (*result, 0) < 0) |
| 2466 |
mpz_set_si (*result, 0); |
| 2467 |
|
| 2468 |
return true; |
| 2469 |
} |
| 2470 |
|
| 2471 |
|
| 2472 |
bool |
| 2473 |
spec_size (gfc_array_spec *as, mpz_t *result) |
| 2474 |
{ |
| 2475 |
mpz_t size; |
| 2476 |
int d; |
| 2477 |
|
| 2478 |
if (!as || as->type == AS_ASSUMED_RANK) |
| 2479 |
return false; |
| 2480 |
|
| 2481 |
mpz_init_set_ui (*result, 1); |
| 2482 |
|
| 2483 |
for (d = 0; d < as->rank; d++) |
| 2484 |
{ |
| 2485 |
if (!spec_dimen_size (as, d, &size)) |
| 2486 |
{ |
| 2487 |
mpz_clear (*result); |
| 2488 |
return false; |
| 2489 |
} |
| 2490 |
|
| 2491 |
mpz_mul (*result, *result, size); |
| 2492 |
mpz_clear (size); |
| 2493 |
} |
| 2494 |
|
| 2495 |
return true; |
| 2496 |
} |
| 2497 |
|
| 2498 |
|
| 2499 |
/* Get the number of elements in an array section. Optionally, also supply |
| 2500 |
the end value. */ |
| 2501 |
|
| 2502 |
bool |
| 2503 |
gfc_ref_dimen_size (gfc_array_ref *ar, int dimen, mpz_t *result, mpz_t *end) |
| 2504 |
{ |
| 2505 |
mpz_t upper, lower, stride; |
| 2506 |
mpz_t diff; |
| 2507 |
bool t; |
| 2508 |
gfc_expr *stride_expr = NULL; |
| 2509 |
|
| 2510 |
if (dimen < 0 || ar == NULL) |
| 2511 |
gfc_internal_error ("gfc_ref_dimen_size(): Bad dimension"); |
| 2512 |
|
| 2513 |
if (dimen > ar->dimen - 1) |
| 2514 |
{ |
| 2515 |
gfc_error ("Bad array dimension at %L", &ar->c_where[dimen]); |
| 2516 |
return false; |
| 2517 |
} |
| 2518 |
|
| 2519 |
switch (ar->dimen_type[dimen]) |
| 2520 |
{ |
| 2521 |
case DIMEN_ELEMENT: |
| 2522 |
mpz_init (*result); |
| 2523 |
mpz_set_ui (*result, 1); |
| 2524 |
t = true; |
| 2525 |
break; |
| 2526 |
|
| 2527 |
case DIMEN_VECTOR: |
| 2528 |
t = gfc_array_size (ar->start[dimen], result); /* Recurse! */ |
| 2529 |
break; |
| 2530 |
|
| 2531 |
case DIMEN_RANGE: |
| 2532 |
|
| 2533 |
mpz_init (stride); |
| 2534 |
|
| 2535 |
if (ar->stride[dimen] == NULL) |
| 2536 |
mpz_set_ui (stride, 1); |
| 2537 |
else |
| 2538 |
{ |
| 2539 |
stride_expr = gfc_copy_expr(ar->stride[dimen]); |
| 2540 |
|
| 2541 |
if (!gfc_simplify_expr (stride_expr, 1) |
| 2542 |
|| stride_expr->expr_type != EXPR_CONSTANT |
| 2543 |
|| mpz_cmp_ui (stride_expr->value.integer, 0) == 0) |
| 2544 |
{ |
| 2545 |
gfc_free_expr (stride_expr); |
| 2546 |
mpz_clear (stride); |
| 2547 |
return false; |
| 2548 |
} |
| 2549 |
mpz_set (stride, stride_expr->value.integer); |
| 2550 |
gfc_free_expr(stride_expr); |
| 2551 |
} |
| 2552 |
|
| 2553 |
/* Calculate the number of elements via gfc_dep_difference, but only if |
| 2554 |
start and end are both supplied in the reference or the array spec. |
| 2555 |
This is to guard against strange but valid code like |
| 2556 |
|
| 2557 |
subroutine foo(a,n) |
| 2558 |
real a(1:n) |
| 2559 |
n = 3 |
| 2560 |
print *,size(a(n-1:)) |
| 2561 |
|
| 2562 |
where the user changes the value of a variable. If we have to |
| 2563 |
determine end as well, we cannot do this using gfc_dep_difference. |
| 2564 |
Fall back to the constants-only code then. */ |
| 2565 |
|
| 2566 |
if (end == NULL) |
| 2567 |
{ |
| 2568 |
bool use_dep; |
| 2569 |
|
| 2570 |
use_dep = gfc_dep_difference (ar->end[dimen], ar->start[dimen], |
| 2571 |
&diff); |
| 2572 |
if (!use_dep && ar->end[dimen] == NULL && ar->start[dimen] == NULL) |
| 2573 |
use_dep = gfc_dep_difference (ar->as->upper[dimen], |
| 2574 |
ar->as->lower[dimen], &diff); |
| 2575 |
|
| 2576 |
if (use_dep) |
| 2577 |
{ |
| 2578 |
mpz_init (*result); |
| 2579 |
mpz_add (*result, diff, stride); |
| 2580 |
mpz_div (*result, *result, stride); |
| 2581 |
if (mpz_cmp_ui (*result, 0) < 0) |
| 2582 |
mpz_set_ui (*result, 0); |
| 2583 |
|
| 2584 |
mpz_clear (stride); |
| 2585 |
mpz_clear (diff); |
| 2586 |
return true; |
| 2587 |
} |
| 2588 |
|
| 2589 |
} |
| 2590 |
|
| 2591 |
/* Constant-only code here, which covers more cases |
| 2592 |
like a(:4) etc. */ |
| 2593 |
mpz_init (upper); |
| 2594 |
mpz_init (lower); |
| 2595 |
t = false; |
| 2596 |
|
| 2597 |
if (ar->start[dimen] == NULL) |
| 2598 |
{ |
| 2599 |
if (ar->as->lower[dimen] == NULL |
| 2600 |
|| ar->as->lower[dimen]->expr_type != EXPR_CONSTANT |
| 2601 |
|| ar->as->lower[dimen]->ts.type != BT_INTEGER) |
| 2602 |
goto cleanup; |
| 2603 |
mpz_set (lower, ar->as->lower[dimen]->value.integer); |
| 2604 |
} |
| 2605 |
else |
| 2606 |
{ |
| 2607 |
if (ar->start[dimen]->expr_type != EXPR_CONSTANT) |
| 2608 |
goto cleanup; |
| 2609 |
mpz_set (lower, ar->start[dimen]->value.integer); |
| 2610 |
} |
| 2611 |
|
| 2612 |
if (ar->end[dimen] == NULL) |
| 2613 |
{ |
| 2614 |
if (ar->as->upper[dimen] == NULL |
| 2615 |
|| ar->as->upper[dimen]->expr_type != EXPR_CONSTANT |
| 2616 |
|| ar->as->upper[dimen]->ts.type != BT_INTEGER) |
| 2617 |
goto cleanup; |
| 2618 |
mpz_set (upper, ar->as->upper[dimen]->value.integer); |
| 2619 |
} |
| 2620 |
else |
| 2621 |
{ |
| 2622 |
if (ar->end[dimen]->expr_type != EXPR_CONSTANT) |
| 2623 |
goto cleanup; |
| 2624 |
mpz_set (upper, ar->end[dimen]->value.integer); |
| 2625 |
} |
| 2626 |
|
| 2627 |
mpz_init (*result); |
| 2628 |
mpz_sub (*result, upper, lower); |
| 2629 |
mpz_add (*result, *result, stride); |
| 2630 |
mpz_div (*result, *result, stride); |
| 2631 |
|
| 2632 |
/* Zero stride caught earlier. */ |
| 2633 |
if (mpz_cmp_ui (*result, 0) < 0) |
| 2634 |
mpz_set_ui (*result, 0); |
| 2635 |
t = true; |
| 2636 |
|
| 2637 |
if (end) |
| 2638 |
{ |
| 2639 |
mpz_init (*end); |
| 2640 |
|
| 2641 |
mpz_sub_ui (*end, *result, 1UL); |
| 2642 |
mpz_mul (*end, *end, stride); |
| 2643 |
mpz_add (*end, *end, lower); |
| 2644 |
} |
| 2645 |
|
| 2646 |
cleanup: |
| 2647 |
mpz_clear (upper); |
| 2648 |
mpz_clear (lower); |
| 2649 |
mpz_clear (stride); |
| 2650 |
return t; |
| 2651 |
|
| 2652 |
default: |
| 2653 |
gfc_internal_error ("gfc_ref_dimen_size(): Bad dimen_type"); |
| 2654 |
} |
| 2655 |
|
| 2656 |
return t; |
| 2657 |
} |
| 2658 |
|
| 2659 |
|
| 2660 |
static bool |
| 2661 |
ref_size (gfc_array_ref *ar, mpz_t *result) |
| 2662 |
{ |
| 2663 |
mpz_t size; |
| 2664 |
int d; |
| 2665 |
|
| 2666 |
mpz_init_set_ui (*result, 1); |
| 2667 |
|
| 2668 |
for (d = 0; d < ar->dimen; d++) |
| 2669 |
{ |
| 2670 |
if (!gfc_ref_dimen_size (ar, d, &size, NULL)) |
| 2671 |
{ |
| 2672 |
mpz_clear (*result); |
| 2673 |
return false; |
| 2674 |
} |
| 2675 |
|
| 2676 |
mpz_mul (*result, *result, size); |
| 2677 |
mpz_clear (size); |
| 2678 |
} |
| 2679 |
|
| 2680 |
return true; |
| 2681 |
} |
| 2682 |
|
| 2683 |
|
| 2684 |
/* Given an array expression and a dimension, figure out how many |
| 2685 |
elements it has along that dimension. Returns true if we were |
| 2686 |
able to return a result in the 'result' variable, false |
| 2687 |
otherwise. */ |
| 2688 |
|
| 2689 |
bool |
| 2690 |
gfc_array_dimen_size (gfc_expr *array, int dimen, mpz_t *result) |
| 2691 |
{ |
| 2692 |
gfc_ref *ref; |
| 2693 |
int i; |
| 2694 |
|
| 2695 |
gcc_assert (array != NULL); |
| 2696 |
|
| 2697 |
if (array->ts.type == BT_CLASS) |
| 2698 |
return false; |
| 2699 |
|
| 2700 |
if (array->rank == -1) |
| 2701 |
return false; |
| 2702 |
|
| 2703 |
if (dimen < 0 || dimen > array->rank - 1) |
| 2704 |
gfc_internal_error ("gfc_array_dimen_size(): Bad dimension"); |
| 2705 |
|
| 2706 |
switch (array->expr_type) |
| 2707 |
{ |
| 2708 |
case EXPR_VARIABLE: |
| 2709 |
case EXPR_FUNCTION: |
| 2710 |
for (ref = array->ref; ref; ref = ref->next) |
| 2711 |
{ |
| 2712 |
/* Ultimate component is a procedure pointer. */ |
| 2713 |
if (ref->type == REF_COMPONENT |
| 2714 |
&& !ref->next |
| 2715 |
&& ref->u.c.component->attr.function |
| 2716 |
&& IS_PROC_POINTER (ref->u.c.component)) |
| 2717 |
return false; |
| 2718 |
|
| 2719 |
if (ref->type != REF_ARRAY) |
| 2720 |
continue; |
| 2721 |
|
| 2722 |
if (ref->u.ar.type == AR_FULL) |
| 2723 |
return spec_dimen_size (ref->u.ar.as, dimen, result); |
| 2724 |
|
| 2725 |
if (ref->u.ar.type == AR_SECTION) |
| 2726 |
{ |
| 2727 |
for (i = 0; dimen >= 0; i++) |
| 2728 |
if (ref->u.ar.dimen_type[i] != DIMEN_ELEMENT) |
| 2729 |
dimen--; |
| 2730 |
|
| 2731 |
return gfc_ref_dimen_size (&ref->u.ar, i - 1, result, NULL); |
| 2732 |
} |
| 2733 |
} |
| 2734 |
|
| 2735 |
if (array->shape) |
| 2736 |
{ |
| 2737 |
mpz_init_set (*result, array->shape[dimen]); |
| 2738 |
return true; |
| 2739 |
} |
| 2740 |
|
| 2741 |
if (array->symtree->n.sym->attr.generic |
| 2742 |
&& array->value.function.esym != NULL) |
| 2743 |
{ |
| 2744 |
if (!spec_dimen_size (array->value.function.esym->as, dimen, result)) |
| 2745 |
return false; |
| 2746 |
} |
| 2747 |
else if (!spec_dimen_size (array->symtree->n.sym->as, dimen, result)) |
| 2748 |
return false; |
| 2749 |
|
| 2750 |
break; |
| 2751 |
|
| 2752 |
case EXPR_ARRAY: |
| 2753 |
if (array->shape == NULL) { |
| 2754 |
/* Expressions with rank > 1 should have "shape" properly set */ |
| 2755 |
if ( array->rank != 1 ) |
| 2756 |
gfc_internal_error ("gfc_array_dimen_size(): Bad EXPR_ARRAY expr"); |
| 2757 |
return gfc_array_size(array, result); |
| 2758 |
} |
| 2759 |
|
| 2760 |
/* Fall through */ |
| 2761 |
default: |
| 2762 |
if (array->shape == NULL) |
| 2763 |
return false; |
| 2764 |
|
| 2765 |
mpz_init_set (*result, array->shape[dimen]); |
| 2766 |
|
| 2767 |
break; |
| 2768 |
} |
| 2769 |
|
| 2770 |
return true; |
| 2771 |
} |
| 2772 |
|
| 2773 |
|
| 2774 |
/* Given an array expression, figure out how many elements are in the |
| 2775 |
array. Returns true if this is possible, and sets the 'result' |
| 2776 |
variable. Otherwise returns false. */ |
| 2777 |
|
| 2778 |
bool |
| 2779 |
gfc_array_size (gfc_expr *array, mpz_t *result) |
| 2780 |
{ |
| 2781 |
expand_info expand_save; |
| 2782 |
gfc_ref *ref; |
| 2783 |
int i; |
| 2784 |
bool t; |
| 2785 |
|
| 2786 |
if (array->ts.type == BT_CLASS) |
| 2787 |
return false; |
| 2788 |
|
| 2789 |
switch (array->expr_type) |
| 2790 |
{ |
| 2791 |
case EXPR_ARRAY: |
| 2792 |
gfc_push_suppress_errors (); |
| 2793 |
|
| 2794 |
expand_save = current_expand; |
| 2795 |
|
| 2796 |
current_expand.count = result; |
| 2797 |
mpz_init_set_ui (*result, 0); |
| 2798 |
|
| 2799 |
current_expand.expand_work_function = count_elements; |
| 2800 |
iter_stack = NULL; |
| 2801 |
|
| 2802 |
t = expand_constructor (array->value.constructor); |
| 2803 |
|
| 2804 |
gfc_pop_suppress_errors (); |
| 2805 |
|
| 2806 |
if (!t) |
| 2807 |
mpz_clear (*result); |
| 2808 |
current_expand = expand_save; |
| 2809 |
return t; |
| 2810 |
|
| 2811 |
case EXPR_VARIABLE: |
| 2812 |
for (ref = array->ref; ref; ref = ref->next) |
| 2813 |
{ |
| 2814 |
if (ref->type != REF_ARRAY) |
| 2815 |
continue; |
| 2816 |
|
| 2817 |
if (ref->u.ar.type == AR_FULL) |
| 2818 |
return spec_size (ref->u.ar.as, result); |
| 2819 |
|
| 2820 |
if (ref->u.ar.type == AR_SECTION) |
| 2821 |
return ref_size (&ref->u.ar, result); |
| 2822 |
} |
| 2823 |
|
| 2824 |
return spec_size (array->symtree->n.sym->as, result); |
| 2825 |
|
| 2826 |
|
| 2827 |
default: |
| 2828 |
if (array->rank == 0 || array->shape == NULL) |
| 2829 |
return false; |
| 2830 |
|
| 2831 |
mpz_init_set_ui (*result, 1); |
| 2832 |
|
| 2833 |
for (i = 0; i < array->rank; i++) |
| 2834 |
mpz_mul (*result, *result, array->shape[i]); |
| 2835 |
|
| 2836 |
break; |
| 2837 |
} |
| 2838 |
|
| 2839 |
return true; |
| 2840 |
} |
| 2841 |
|
| 2842 |
|
| 2843 |
/* Given an array reference, return the shape of the reference in an |
| 2844 |
array of mpz_t integers. */ |
| 2845 |
|
| 2846 |
bool |
| 2847 |
gfc_array_ref_shape (gfc_array_ref *ar, mpz_t *shape) |
| 2848 |
{ |
| 2849 |
int d; |
| 2850 |
int i; |
| 2851 |
|
| 2852 |
d = 0; |
| 2853 |
|
| 2854 |
switch (ar->type) |
| 2855 |
{ |
| 2856 |
case AR_FULL: |
| 2857 |
for (; d < ar->as->rank; d++) |
| 2858 |
if (!spec_dimen_size (ar->as, d, &shape[d])) |
| 2859 |
goto cleanup; |
| 2860 |
|
| 2861 |
return true; |
| 2862 |
|
| 2863 |
case AR_SECTION: |
| 2864 |
for (i = 0; i < ar->dimen; i++) |
| 2865 |
{ |
| 2866 |
if (ar->dimen_type[i] != DIMEN_ELEMENT) |
| 2867 |
{ |
| 2868 |
if (!gfc_ref_dimen_size (ar, i, &shape[d], NULL)) |
| 2869 |
goto cleanup; |
| 2870 |
d++; |
| 2871 |
} |
| 2872 |
} |
| 2873 |
|
| 2874 |
return true; |
| 2875 |
|
| 2876 |
default: |
| 2877 |
break; |
| 2878 |
} |
| 2879 |
|
| 2880 |
cleanup: |
| 2881 |
gfc_clear_shape (shape, d); |
| 2882 |
return false; |
| 2883 |
} |
| 2884 |
|
| 2885 |
|
| 2886 |
/* Given an array expression, find the array reference structure that |
| 2887 |
characterizes the reference. */ |
| 2888 |
|
| 2889 |
gfc_array_ref * |
| 2890 |
gfc_find_array_ref (gfc_expr *e, bool allow_null) |
| 2891 |
{ |
| 2892 |
gfc_ref *ref; |
| 2893 |
|
| 2894 |
for (ref = e->ref; ref; ref = ref->next) |
| 2895 |
if (ref->type == REF_ARRAY |
| 2896 |
&& (ref->u.ar.type == AR_FULL || ref->u.ar.type == AR_SECTION)) |
| 2897 |
break; |
| 2898 |
|
| 2899 |
if (ref == NULL) |
| 2900 |
{ |
| 2901 |
if (allow_null) |
| 2902 |
return NULL; |
| 2903 |
else |
| 2904 |
gfc_internal_error ("gfc_find_array_ref(): No ref found"); |
| 2905 |
} |
| 2906 |
|
| 2907 |
return &ref->u.ar; |
| 2908 |
} |
| 2909 |
|
| 2910 |
|
| 2911 |
/* Find out if an array shape is known at compile time. */ |
| 2912 |
|
| 2913 |
bool |
| 2914 |
gfc_is_compile_time_shape (gfc_array_spec *as) |
| 2915 |
{ |
| 2916 |
if (as->type != AS_EXPLICIT) |
| 2917 |
return false; |
| 2918 |
|
| 2919 |
for (int i = 0; i < as->rank; i++) |
| 2920 |
if (!gfc_is_constant_expr (as->lower[i]) |
| 2921 |
|| !gfc_is_constant_expr (as->upper[i])) |
| 2922 |
return false; |
| 2923 |
|
| 2924 |
return true; |
| 2925 |
} |