OpenCores
URL https://opencores.org/ocsvn/scarts/scarts/trunk

Subversion Repositories scarts

[/] [scarts/] [trunk/] [toolchain/] [scarts-gcc/] [gcc-4.1.1/] [gcc/] [fortran/] [trans-decl.c] - Blame information for rev 20

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 12 jlechner
/* Backend function setup
2
   Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3
   Contributed by Paul Brook
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 2, 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 COPYING.  If not, write to the Free
19
Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20
02110-1301, USA.  */
21
 
22
/* trans-decl.c -- Handling of backend function and variable decls, etc */
23
 
24
#include "config.h"
25
#include "system.h"
26
#include "coretypes.h"
27
#include "tree.h"
28
#include "tree-dump.h"
29
#include "tree-gimple.h"
30
#include "ggc.h"
31
#include "toplev.h"
32
#include "tm.h"
33
#include "target.h"
34
#include "function.h"
35
#include "flags.h"
36
#include "cgraph.h"
37
#include "gfortran.h"
38
#include "trans.h"
39
#include "trans-types.h"
40
#include "trans-array.h"
41
#include "trans-const.h"
42
/* Only for gfc_trans_code.  Shouldn't need to include this.  */
43
#include "trans-stmt.h"
44
 
45
#define MAX_LABEL_VALUE 99999
46
 
47
 
48
/* Holds the result of the function if no result variable specified.  */
49
 
50
static GTY(()) tree current_fake_result_decl;
51
 
52
static GTY(()) tree current_function_return_label;
53
 
54
 
55
/* Holds the variable DECLs for the current function.  */
56
 
57
static GTY(()) tree saved_function_decls = NULL_TREE;
58
static GTY(()) tree saved_parent_function_decls = NULL_TREE;
59
 
60
 
61
/* The namespace of the module we're currently generating.  Only used while
62
   outputting decls for module variables.  Do not rely on this being set.  */
63
 
64
static gfc_namespace *module_namespace;
65
 
66
 
67
/* List of static constructor functions.  */
68
 
69
tree gfc_static_ctors;
70
 
71
 
72
/* Function declarations for builtin library functions.  */
73
 
74
tree gfor_fndecl_internal_malloc;
75
tree gfor_fndecl_internal_malloc64;
76
tree gfor_fndecl_internal_realloc;
77
tree gfor_fndecl_internal_realloc64;
78
tree gfor_fndecl_internal_free;
79
tree gfor_fndecl_allocate;
80
tree gfor_fndecl_allocate64;
81
tree gfor_fndecl_allocate_array;
82
tree gfor_fndecl_allocate64_array;
83
tree gfor_fndecl_deallocate;
84
tree gfor_fndecl_pause_numeric;
85
tree gfor_fndecl_pause_string;
86
tree gfor_fndecl_stop_numeric;
87
tree gfor_fndecl_stop_string;
88
tree gfor_fndecl_select_string;
89
tree gfor_fndecl_runtime_error;
90
tree gfor_fndecl_set_fpe;
91
tree gfor_fndecl_set_std;
92
tree gfor_fndecl_set_convert;
93
tree gfor_fndecl_set_record_marker;
94
tree gfor_fndecl_ctime;
95
tree gfor_fndecl_fdate;
96
tree gfor_fndecl_ttynam;
97
tree gfor_fndecl_in_pack;
98
tree gfor_fndecl_in_unpack;
99
tree gfor_fndecl_associated;
100
 
101
 
102
/* Math functions.  Many other math functions are handled in
103
   trans-intrinsic.c.  */
104
 
105
gfc_powdecl_list gfor_fndecl_math_powi[4][3];
106
tree gfor_fndecl_math_cpowf;
107
tree gfor_fndecl_math_cpow;
108
tree gfor_fndecl_math_cpowl10;
109
tree gfor_fndecl_math_cpowl16;
110
tree gfor_fndecl_math_ishftc4;
111
tree gfor_fndecl_math_ishftc8;
112
tree gfor_fndecl_math_ishftc16;
113
tree gfor_fndecl_math_exponent4;
114
tree gfor_fndecl_math_exponent8;
115
tree gfor_fndecl_math_exponent10;
116
tree gfor_fndecl_math_exponent16;
117
 
118
 
119
/* String functions.  */
120
 
121
tree gfor_fndecl_copy_string;
122
tree gfor_fndecl_compare_string;
123
tree gfor_fndecl_concat_string;
124
tree gfor_fndecl_string_len_trim;
125
tree gfor_fndecl_string_index;
126
tree gfor_fndecl_string_scan;
127
tree gfor_fndecl_string_verify;
128
tree gfor_fndecl_string_trim;
129
tree gfor_fndecl_string_repeat;
130
tree gfor_fndecl_adjustl;
131
tree gfor_fndecl_adjustr;
132
 
133
 
134
/* Other misc. runtime library functions.  */
135
 
136
tree gfor_fndecl_size0;
137
tree gfor_fndecl_size1;
138
tree gfor_fndecl_iargc;
139
 
140
/* Intrinsic functions implemented in FORTRAN.  */
141
tree gfor_fndecl_si_kind;
142
tree gfor_fndecl_sr_kind;
143
 
144
 
145
static void
146
gfc_add_decl_to_parent_function (tree decl)
147
{
148
  gcc_assert (decl);
149
  DECL_CONTEXT (decl) = DECL_CONTEXT (current_function_decl);
150
  DECL_NONLOCAL (decl) = 1;
151
  TREE_CHAIN (decl) = saved_parent_function_decls;
152
  saved_parent_function_decls = decl;
153
}
154
 
155
void
156
gfc_add_decl_to_function (tree decl)
157
{
158
  gcc_assert (decl);
159
  TREE_USED (decl) = 1;
160
  DECL_CONTEXT (decl) = current_function_decl;
161
  TREE_CHAIN (decl) = saved_function_decls;
162
  saved_function_decls = decl;
163
}
164
 
165
 
166
/* Build a  backend label declaration.  Set TREE_USED for named labels.
167
   The context of the label is always the current_function_decl.  All
168
   labels are marked artificial.  */
169
 
170
tree
171
gfc_build_label_decl (tree label_id)
172
{
173
  /* 2^32 temporaries should be enough.  */
174
  static unsigned int tmp_num = 1;
175
  tree label_decl;
176
  char *label_name;
177
 
178
  if (label_id == NULL_TREE)
179
    {
180
      /* Build an internal label name.  */
181
      ASM_FORMAT_PRIVATE_NAME (label_name, "L", tmp_num++);
182
      label_id = get_identifier (label_name);
183
    }
184
  else
185
    label_name = NULL;
186
 
187
  /* Build the LABEL_DECL node. Labels have no type.  */
188
  label_decl = build_decl (LABEL_DECL, label_id, void_type_node);
189
  DECL_CONTEXT (label_decl) = current_function_decl;
190
  DECL_MODE (label_decl) = VOIDmode;
191
 
192
  /* We always define the label as used, even if the original source
193
     file never references the label.  We don't want all kinds of
194
     spurious warnings for old-style Fortran code with too many
195
     labels.  */
196
  TREE_USED (label_decl) = 1;
197
 
198
  DECL_ARTIFICIAL (label_decl) = 1;
199
  return label_decl;
200
}
201
 
202
 
203
/* Returns the return label for the current function.  */
204
 
205
tree
206
gfc_get_return_label (void)
207
{
208
  char name[GFC_MAX_SYMBOL_LEN + 10];
209
 
210
  if (current_function_return_label)
211
    return current_function_return_label;
212
 
213
  sprintf (name, "__return_%s",
214
           IDENTIFIER_POINTER (DECL_NAME (current_function_decl)));
215
 
216
  current_function_return_label =
217
    gfc_build_label_decl (get_identifier (name));
218
 
219
  DECL_ARTIFICIAL (current_function_return_label) = 1;
220
 
221
  return current_function_return_label;
222
}
223
 
224
 
225
/* Set the backend source location of a decl.  */
226
 
227
void
228
gfc_set_decl_location (tree decl, locus * loc)
229
{
230
#ifdef USE_MAPPED_LOCATION
231
  DECL_SOURCE_LOCATION (decl) = loc->lb->location;
232
#else
233
  DECL_SOURCE_LINE (decl) = loc->lb->linenum;
234
  DECL_SOURCE_FILE (decl) = loc->lb->file->filename;
235
#endif
236
}
237
 
238
 
239
/* Return the backend label declaration for a given label structure,
240
   or create it if it doesn't exist yet.  */
241
 
242
tree
243
gfc_get_label_decl (gfc_st_label * lp)
244
{
245
  if (lp->backend_decl)
246
    return lp->backend_decl;
247
  else
248
    {
249
      char label_name[GFC_MAX_SYMBOL_LEN + 1];
250
      tree label_decl;
251
 
252
      /* Validate the label declaration from the front end.  */
253
      gcc_assert (lp != NULL && lp->value <= MAX_LABEL_VALUE);
254
 
255
      /* Build a mangled name for the label.  */
256
      sprintf (label_name, "__label_%.6d", lp->value);
257
 
258
      /* Build the LABEL_DECL node.  */
259
      label_decl = gfc_build_label_decl (get_identifier (label_name));
260
 
261
      /* Tell the debugger where the label came from.  */
262
      if (lp->value <= MAX_LABEL_VALUE) /* An internal label.  */
263
        gfc_set_decl_location (label_decl, &lp->where);
264
      else
265
        DECL_ARTIFICIAL (label_decl) = 1;
266
 
267
      /* Store the label in the label list and return the LABEL_DECL.  */
268
      lp->backend_decl = label_decl;
269
      return label_decl;
270
    }
271
}
272
 
273
 
274
/* Convert a gfc_symbol to an identifier of the same name.  */
275
 
276
static tree
277
gfc_sym_identifier (gfc_symbol * sym)
278
{
279
  return (get_identifier (sym->name));
280
}
281
 
282
 
283
/* Construct mangled name from symbol name.  */
284
 
285
static tree
286
gfc_sym_mangled_identifier (gfc_symbol * sym)
287
{
288
  char name[GFC_MAX_MANGLED_SYMBOL_LEN + 1];
289
 
290
  if (sym->module == NULL)
291
    return gfc_sym_identifier (sym);
292
  else
293
    {
294
      snprintf (name, sizeof name, "__%s__%s", sym->module, sym->name);
295
      return get_identifier (name);
296
    }
297
}
298
 
299
 
300
/* Construct mangled function name from symbol name.  */
301
 
302
static tree
303
gfc_sym_mangled_function_id (gfc_symbol * sym)
304
{
305
  int has_underscore;
306
  char name[GFC_MAX_MANGLED_SYMBOL_LEN + 1];
307
 
308
  if (sym->module == NULL || sym->attr.proc == PROC_EXTERNAL
309
      || (sym->module != NULL && sym->attr.if_source == IFSRC_IFBODY))
310
    {
311
      if (strcmp (sym->name, "MAIN__") == 0
312
          || sym->attr.proc == PROC_INTRINSIC)
313
        return get_identifier (sym->name);
314
 
315
      if (gfc_option.flag_underscoring)
316
        {
317
          has_underscore = strchr (sym->name, '_') != 0;
318
          if (gfc_option.flag_second_underscore && has_underscore)
319
            snprintf (name, sizeof name, "%s__", sym->name);
320
          else
321
            snprintf (name, sizeof name, "%s_", sym->name);
322
          return get_identifier (name);
323
        }
324
      else
325
        return get_identifier (sym->name);
326
    }
327
  else
328
    {
329
      snprintf (name, sizeof name, "__%s__%s", sym->module, sym->name);
330
      return get_identifier (name);
331
    }
332
}
333
 
334
 
335
/* Returns true if a variable of specified size should go on the stack.  */
336
 
337
int
338
gfc_can_put_var_on_stack (tree size)
339
{
340
  unsigned HOST_WIDE_INT low;
341
 
342
  if (!INTEGER_CST_P (size))
343
    return 0;
344
 
345
  if (gfc_option.flag_max_stack_var_size < 0)
346
    return 1;
347
 
348
  if (TREE_INT_CST_HIGH (size) != 0)
349
    return 0;
350
 
351
  low = TREE_INT_CST_LOW (size);
352
  if (low > (unsigned HOST_WIDE_INT) gfc_option.flag_max_stack_var_size)
353
    return 0;
354
 
355
/* TODO: Set a per-function stack size limit.  */
356
 
357
  return 1;
358
}
359
 
360
 
361
/* gfc_finish_cray_pointee sets DECL_VALUE_EXPR for a Cray pointee to
362
   an expression involving its corresponding pointer.  There are
363
   2 cases; one for variable size arrays, and one for everything else,
364
   because variable-sized arrays require one fewer level of
365
   indirection.  */
366
 
367
static void
368
gfc_finish_cray_pointee (tree decl, gfc_symbol *sym)
369
{
370
  tree ptr_decl = gfc_get_symbol_decl (sym->cp_pointer);
371
  tree value;
372
 
373
  /* Parameters need to be dereferenced.  */
374
  if (sym->cp_pointer->attr.dummy)
375
    ptr_decl = gfc_build_indirect_ref (ptr_decl);
376
 
377
  /* Check to see if we're dealing with a variable-sized array.  */
378
  if (sym->attr.dimension
379
      && TREE_CODE (TREE_TYPE (decl)) == POINTER_TYPE)
380
    {
381
      /* These decls will be dereferenced later, so we don't dereference
382
         them here.  */
383
      value = convert (TREE_TYPE (decl), ptr_decl);
384
    }
385
  else
386
    {
387
      ptr_decl = convert (build_pointer_type (TREE_TYPE (decl)),
388
                          ptr_decl);
389
      value = gfc_build_indirect_ref (ptr_decl);
390
    }
391
 
392
  SET_DECL_VALUE_EXPR (decl, value);
393
  DECL_HAS_VALUE_EXPR_P (decl) = 1;
394
  /* This is a fake variable just for debugging purposes.  */
395
  TREE_ASM_WRITTEN (decl) = 1;
396
}
397
 
398
 
399
/* Finish processing of a declaration and install its initial value.  */
400
 
401
static void
402
gfc_finish_decl (tree decl, tree init)
403
{
404
  if (TREE_CODE (decl) == PARM_DECL)
405
    gcc_assert (init == NULL_TREE);
406
  /* Remember that PARM_DECL doesn't have a DECL_INITIAL field per se
407
     -- it overlaps DECL_ARG_TYPE.  */
408
  else if (init == NULL_TREE)
409
    gcc_assert (DECL_INITIAL (decl) == NULL_TREE);
410
  else
411
    gcc_assert (DECL_INITIAL (decl) == error_mark_node);
412
 
413
  if (init != NULL_TREE)
414
    {
415
      if (TREE_CODE (decl) != TYPE_DECL)
416
        DECL_INITIAL (decl) = init;
417
      else
418
        {
419
          /* typedef foo = bar; store the type of bar as the type of foo.  */
420
          TREE_TYPE (decl) = TREE_TYPE (init);
421
          DECL_INITIAL (decl) = init = 0;
422
        }
423
    }
424
 
425
  if (TREE_CODE (decl) == VAR_DECL)
426
    {
427
      if (DECL_SIZE (decl) == NULL_TREE
428
          && TYPE_SIZE (TREE_TYPE (decl)) != NULL_TREE)
429
        layout_decl (decl, 0);
430
 
431
      /* A static variable with an incomplete type is an error if it is
432
         initialized. Also if it is not file scope. Otherwise, let it
433
         through, but if it is not `extern' then it may cause an error
434
         message later.  */
435
      /* An automatic variable with an incomplete type is an error.  */
436
      if (DECL_SIZE (decl) == NULL_TREE
437
          && (TREE_STATIC (decl) ? (DECL_INITIAL (decl) != 0
438
                                    || DECL_CONTEXT (decl) != 0)
439
                                 : !DECL_EXTERNAL (decl)))
440
        {
441
          gfc_fatal_error ("storage size not known");
442
        }
443
 
444
      if ((DECL_EXTERNAL (decl) || TREE_STATIC (decl))
445
          && (DECL_SIZE (decl) != 0)
446
          && (TREE_CODE (DECL_SIZE (decl)) != INTEGER_CST))
447
        {
448
          gfc_fatal_error ("storage size not constant");
449
        }
450
    }
451
 
452
}
453
 
454
 
455
/* Apply symbol attributes to a variable, and add it to the function scope.  */
456
 
457
static void
458
gfc_finish_var_decl (tree decl, gfc_symbol * sym)
459
{
460
  /* TREE_ADDRESSABLE means the address of this variable is actually needed.
461
     This is the equivalent of the TARGET variables.
462
     We also need to set this if the variable is passed by reference in a
463
     CALL statement.  */
464
 
465
  /* Set DECL_VALUE_EXPR for Cray Pointees.  */
466
  if (sym->attr.cray_pointee)
467
    gfc_finish_cray_pointee (decl, sym);
468
 
469
  if (sym->attr.target)
470
    TREE_ADDRESSABLE (decl) = 1;
471
  /* If it wasn't used we wouldn't be getting it.  */
472
  TREE_USED (decl) = 1;
473
 
474
  /* Chain this decl to the pending declarations.  Don't do pushdecl()
475
     because this would add them to the current scope rather than the
476
     function scope.  */
477
  if (current_function_decl != NULL_TREE)
478
    {
479
      if (sym->ns->proc_name->backend_decl == current_function_decl
480
          || sym->result == sym)
481
        gfc_add_decl_to_function (decl);
482
      else
483
        gfc_add_decl_to_parent_function (decl);
484
    }
485
 
486
  if (sym->attr.cray_pointee)
487
    return;
488
 
489
  /* If a variable is USE associated, it's always external.  */
490
  if (sym->attr.use_assoc)
491
    {
492
      DECL_EXTERNAL (decl) = 1;
493
      TREE_PUBLIC (decl) = 1;
494
    }
495
  else if (sym->module && !sym->attr.result && !sym->attr.dummy)
496
    {
497
      /* TODO: Don't set sym->module for result or dummy variables.  */
498
      gcc_assert (current_function_decl == NULL_TREE || sym->result == sym);
499
      /* This is the declaration of a module variable.  */
500
      TREE_PUBLIC (decl) = 1;
501
      TREE_STATIC (decl) = 1;
502
    }
503
 
504
  if ((sym->attr.save || sym->attr.data || sym->value)
505
      && !sym->attr.use_assoc)
506
    TREE_STATIC (decl) = 1;
507
 
508
  /* Keep variables larger than max-stack-var-size off stack.  */
509
  if (!sym->ns->proc_name->attr.recursive
510
      && INTEGER_CST_P (DECL_SIZE_UNIT (decl))
511
      && !gfc_can_put_var_on_stack (DECL_SIZE_UNIT (decl)))
512
    TREE_STATIC (decl) = 1;
513
}
514
 
515
 
516
/* Allocate the lang-specific part of a decl.  */
517
 
518
void
519
gfc_allocate_lang_decl (tree decl)
520
{
521
  DECL_LANG_SPECIFIC (decl) = (struct lang_decl *)
522
    ggc_alloc_cleared (sizeof (struct lang_decl));
523
}
524
 
525
/* Remember a symbol to generate initialization/cleanup code at function
526
   entry/exit.  */
527
 
528
static void
529
gfc_defer_symbol_init (gfc_symbol * sym)
530
{
531
  gfc_symbol *p;
532
  gfc_symbol *last;
533
  gfc_symbol *head;
534
 
535
  /* Don't add a symbol twice.  */
536
  if (sym->tlink)
537
    return;
538
 
539
  last = head = sym->ns->proc_name;
540
  p = last->tlink;
541
 
542
  /* Make sure that setup code for dummy variables which are used in the
543
     setup of other variables is generated first.  */
544
  if (sym->attr.dummy)
545
    {
546
      /* Find the first dummy arg seen after us, or the first non-dummy arg.
547
         This is a circular list, so don't go past the head.  */
548
      while (p != head
549
             && (!p->attr.dummy || p->dummy_order > sym->dummy_order))
550
        {
551
          last = p;
552
          p = p->tlink;
553
        }
554
    }
555
  /* Insert in between last and p.  */
556
  last->tlink = sym;
557
  sym->tlink = p;
558
}
559
 
560
 
561
/* Create an array index type variable with function scope.  */
562
 
563
static tree
564
create_index_var (const char * pfx, int nest)
565
{
566
  tree decl;
567
 
568
  decl = gfc_create_var_np (gfc_array_index_type, pfx);
569
  if (nest)
570
    gfc_add_decl_to_parent_function (decl);
571
  else
572
    gfc_add_decl_to_function (decl);
573
  return decl;
574
}
575
 
576
 
577
/* Create variables to hold all the non-constant bits of info for a
578
   descriptorless array.  Remember these in the lang-specific part of the
579
   type.  */
580
 
581
static void
582
gfc_build_qualified_array (tree decl, gfc_symbol * sym)
583
{
584
  tree type;
585
  int dim;
586
  int nest;
587
 
588
  type = TREE_TYPE (decl);
589
 
590
  /* We just use the descriptor, if there is one.  */
591
  if (GFC_DESCRIPTOR_TYPE_P (type))
592
    return;
593
 
594
  gcc_assert (GFC_ARRAY_TYPE_P (type));
595
  nest = (sym->ns->proc_name->backend_decl != current_function_decl)
596
         && !sym->attr.contained;
597
 
598
  for (dim = 0; dim < GFC_TYPE_ARRAY_RANK (type); dim++)
599
    {
600
      if (GFC_TYPE_ARRAY_LBOUND (type, dim) == NULL_TREE)
601
        GFC_TYPE_ARRAY_LBOUND (type, dim) = create_index_var ("lbound", nest);
602
      /* Don't try to use the unknown bound for assumed shape arrays.  */
603
      if (GFC_TYPE_ARRAY_UBOUND (type, dim) == NULL_TREE
604
          && (sym->as->type != AS_ASSUMED_SIZE
605
              || dim < GFC_TYPE_ARRAY_RANK (type) - 1))
606
        GFC_TYPE_ARRAY_UBOUND (type, dim) = create_index_var ("ubound", nest);
607
 
608
      if (GFC_TYPE_ARRAY_STRIDE (type, dim) == NULL_TREE)
609
        GFC_TYPE_ARRAY_STRIDE (type, dim) = create_index_var ("stride", nest);
610
    }
611
  if (GFC_TYPE_ARRAY_OFFSET (type) == NULL_TREE)
612
    {
613
      GFC_TYPE_ARRAY_OFFSET (type) = gfc_create_var_np (gfc_array_index_type,
614
                                                        "offset");
615
      if (nest)
616
        gfc_add_decl_to_parent_function (GFC_TYPE_ARRAY_OFFSET (type));
617
      else
618
        gfc_add_decl_to_function (GFC_TYPE_ARRAY_OFFSET (type));
619
    }
620
}
621
 
622
 
623
/* For some dummy arguments we don't use the actual argument directly.
624
   Instead we create a local decl and use that.  This allows us to perform
625
   initialization, and construct full type information.  */
626
 
627
static tree
628
gfc_build_dummy_array_decl (gfc_symbol * sym, tree dummy)
629
{
630
  tree decl;
631
  tree type;
632
  gfc_array_spec *as;
633
  char *name;
634
  int packed;
635
  int n;
636
  bool known_size;
637
 
638
  if (sym->attr.pointer || sym->attr.allocatable)
639
    return dummy;
640
 
641
  /* Add to list of variables if not a fake result variable.  */
642
  if (sym->attr.result || sym->attr.dummy)
643
    gfc_defer_symbol_init (sym);
644
 
645
  type = TREE_TYPE (dummy);
646
  gcc_assert (TREE_CODE (dummy) == PARM_DECL
647
          && POINTER_TYPE_P (type));
648
 
649
  /* Do we know the element size?  */
650
  known_size = sym->ts.type != BT_CHARACTER
651
          || INTEGER_CST_P (sym->ts.cl->backend_decl);
652
 
653
  if (known_size && !GFC_DESCRIPTOR_TYPE_P (TREE_TYPE (type)))
654
    {
655
      /* For descriptorless arrays with known element size the actual
656
         argument is sufficient.  */
657
      gcc_assert (GFC_ARRAY_TYPE_P (type));
658
      gfc_build_qualified_array (dummy, sym);
659
      return dummy;
660
    }
661
 
662
  type = TREE_TYPE (type);
663
  if (GFC_DESCRIPTOR_TYPE_P (type))
664
    {
665
      /* Create a decriptorless array pointer.  */
666
      as = sym->as;
667
      packed = 0;
668
      if (!gfc_option.flag_repack_arrays)
669
        {
670
          if (as->type == AS_ASSUMED_SIZE)
671
            packed = 2;
672
        }
673
      else
674
        {
675
          if (as->type == AS_EXPLICIT)
676
            {
677
              packed = 2;
678
              for (n = 0; n < as->rank; n++)
679
                {
680
                  if (!(as->upper[n]
681
                        && as->lower[n]
682
                        && as->upper[n]->expr_type == EXPR_CONSTANT
683
                        && as->lower[n]->expr_type == EXPR_CONSTANT))
684
                    packed = 1;
685
                }
686
            }
687
          else
688
            packed = 1;
689
        }
690
 
691
      type = gfc_typenode_for_spec (&sym->ts);
692
      type = gfc_get_nodesc_array_type (type, sym->as, packed);
693
    }
694
  else
695
    {
696
      /* We now have an expression for the element size, so create a fully
697
         qualified type.  Reset sym->backend decl or this will just return the
698
         old type.  */
699
      sym->backend_decl = NULL_TREE;
700
      type = gfc_sym_type (sym);
701
      packed = 2;
702
    }
703
 
704
  ASM_FORMAT_PRIVATE_NAME (name, IDENTIFIER_POINTER (DECL_NAME (dummy)), 0);
705
  decl = build_decl (VAR_DECL, get_identifier (name), type);
706
 
707
  DECL_ARTIFICIAL (decl) = 1;
708
  TREE_PUBLIC (decl) = 0;
709
  TREE_STATIC (decl) = 0;
710
  DECL_EXTERNAL (decl) = 0;
711
 
712
  /* We should never get deferred shape arrays here.  We used to because of
713
     frontend bugs.  */
714
  gcc_assert (sym->as->type != AS_DEFERRED);
715
 
716
  switch (packed)
717
    {
718
    case 1:
719
      GFC_DECL_PARTIAL_PACKED_ARRAY (decl) = 1;
720
      break;
721
 
722
    case 2:
723
      GFC_DECL_PACKED_ARRAY (decl) = 1;
724
      break;
725
    }
726
 
727
  gfc_build_qualified_array (decl, sym);
728
 
729
  if (DECL_LANG_SPECIFIC (dummy))
730
    DECL_LANG_SPECIFIC (decl) = DECL_LANG_SPECIFIC (dummy);
731
  else
732
    gfc_allocate_lang_decl (decl);
733
 
734
  GFC_DECL_SAVED_DESCRIPTOR (decl) = dummy;
735
 
736
  if (sym->ns->proc_name->backend_decl == current_function_decl
737
      || sym->attr.contained)
738
    gfc_add_decl_to_function (decl);
739
  else
740
    gfc_add_decl_to_parent_function (decl);
741
 
742
  return decl;
743
}
744
 
745
 
746
/* Return a constant or a variable to use as a string length.  Does not
747
   add the decl to the current scope.  */
748
 
749
static tree
750
gfc_create_string_length (gfc_symbol * sym)
751
{
752
  tree length;
753
 
754
  gcc_assert (sym->ts.cl);
755
  gfc_conv_const_charlen (sym->ts.cl);
756
 
757
  if (sym->ts.cl->backend_decl == NULL_TREE)
758
    {
759
      char name[GFC_MAX_MANGLED_SYMBOL_LEN + 2];
760
 
761
      /* Also prefix the mangled name.  */
762
      strcpy (&name[1], sym->name);
763
      name[0] = '.';
764
      length = build_decl (VAR_DECL, get_identifier (name),
765
                           gfc_charlen_type_node);
766
      DECL_ARTIFICIAL (length) = 1;
767
      TREE_USED (length) = 1;
768
      gfc_defer_symbol_init (sym);
769
      sym->ts.cl->backend_decl = length;
770
    }
771
 
772
  return sym->ts.cl->backend_decl;
773
}
774
 
775
/* If a variable is assigned a label, we add another two auxiliary
776
   variables.  */
777
 
778
static void
779
gfc_add_assign_aux_vars (gfc_symbol * sym)
780
{
781
  tree addr;
782
  tree length;
783
  tree decl;
784
 
785
  gcc_assert (sym->backend_decl);
786
 
787
  decl = sym->backend_decl;
788
  gfc_allocate_lang_decl (decl);
789
  GFC_DECL_ASSIGN (decl) = 1;
790
  length = build_decl (VAR_DECL, create_tmp_var_name (sym->name),
791
                       gfc_charlen_type_node);
792
  addr = build_decl (VAR_DECL, create_tmp_var_name (sym->name),
793
                     pvoid_type_node);
794
  gfc_finish_var_decl (length, sym);
795
  gfc_finish_var_decl (addr, sym);
796
  /*  STRING_LENGTH is also used as flag. Less than -1 means that
797
      ASSIGN_ADDR can not be used. Equal -1 means that ASSIGN_ADDR is the
798
      target label's address. Otherwise, value is the length of a format string
799
      and ASSIGN_ADDR is its address.  */
800
  if (TREE_STATIC (length))
801
    DECL_INITIAL (length) = build_int_cst (NULL_TREE, -2);
802
  else
803
    gfc_defer_symbol_init (sym);
804
 
805
  GFC_DECL_STRING_LEN (decl) = length;
806
  GFC_DECL_ASSIGN_ADDR (decl) = addr;
807
}
808
 
809
/* Return the decl for a gfc_symbol, create it if it doesn't already
810
   exist.  */
811
 
812
tree
813
gfc_get_symbol_decl (gfc_symbol * sym)
814
{
815
  tree decl;
816
  tree etype = NULL_TREE;
817
  tree length = NULL_TREE;
818
  tree tmp = NULL_TREE;
819
  int byref;
820
 
821
  gcc_assert (sym->attr.referenced
822
               || sym->ns->proc_name->attr.if_source == IFSRC_IFBODY);
823
 
824
  if (sym->ns && sym->ns->proc_name->attr.function)
825
    byref = gfc_return_by_reference (sym->ns->proc_name);
826
  else
827
    byref = 0;
828
 
829
  if ((sym->attr.dummy && ! sym->attr.function) || (sym->attr.result && byref))
830
    {
831
      /* Return via extra parameter.  */
832
      if (sym->attr.result && byref
833
          && !sym->backend_decl)
834
        {
835
          sym->backend_decl =
836
            DECL_ARGUMENTS (sym->ns->proc_name->backend_decl);
837
          /* For entry master function skip over the __entry
838
             argument.  */
839
          if (sym->ns->proc_name->attr.entry_master)
840
            sym->backend_decl = TREE_CHAIN (sym->backend_decl);
841
        }
842
 
843
      /* Dummy variables should already have been created.  */
844
      gcc_assert (sym->backend_decl);
845
 
846
      /* Create a character length variable.  */
847
      if (sym->ts.type == BT_CHARACTER)
848
        {
849
          if (sym->ts.cl->backend_decl == NULL_TREE)
850
            {
851
              length = gfc_create_string_length (sym);
852
              if (TREE_CODE (length) != INTEGER_CST)
853
                {
854
                  gfc_finish_var_decl (length, sym);
855
                  gfc_defer_symbol_init (sym);
856
                }
857
            }
858
 
859
          /* Set the element size of automatic and assumed character length
860
             length, dummy, pointer arrays.  */
861
          if (sym->attr.pointer && sym->attr.dummy
862
                && sym->attr.dimension)
863
            {
864
              tmp = gfc_build_indirect_ref (sym->backend_decl);
865
              etype = gfc_get_element_type (TREE_TYPE (tmp));
866
              if (TYPE_SIZE_UNIT (etype) == NULL_TREE)
867
                {
868
                  tmp = TYPE_SIZE_UNIT (gfc_character1_type_node);
869
                  tmp = fold_convert (TREE_TYPE (tmp), sym->ts.cl->backend_decl);
870
                  TYPE_SIZE_UNIT (etype) = tmp;
871
                }
872
            }
873
        }
874
 
875
      /* Use a copy of the descriptor for dummy arrays.  */
876
      if (sym->attr.dimension && !TREE_USED (sym->backend_decl))
877
        {
878
          sym->backend_decl =
879
            gfc_build_dummy_array_decl (sym, sym->backend_decl);
880
        }
881
 
882
      TREE_USED (sym->backend_decl) = 1;
883
      if (sym->attr.assign && GFC_DECL_ASSIGN (sym->backend_decl) == 0)
884
        {
885
          gfc_add_assign_aux_vars (sym);
886
        }
887
      return sym->backend_decl;
888
    }
889
 
890
  if (sym->backend_decl)
891
    return sym->backend_decl;
892
 
893
  /* Catch function declarations.  Only used for actual parameters.  */
894
  if (sym->attr.flavor == FL_PROCEDURE)
895
    {
896
      decl = gfc_get_extern_function_decl (sym);
897
      return decl;
898
    }
899
 
900
  if (sym->attr.intrinsic)
901
    internal_error ("intrinsic variable which isn't a procedure");
902
 
903
  /* Create string length decl first so that they can be used in the
904
     type declaration.  */
905
  if (sym->ts.type == BT_CHARACTER)
906
    length = gfc_create_string_length (sym);
907
 
908
  /* Create the decl for the variable.  */
909
  decl = build_decl (VAR_DECL, gfc_sym_identifier (sym), gfc_sym_type (sym));
910
 
911
  gfc_set_decl_location (decl, &sym->declared_at);
912
 
913
  /* Symbols from modules should have their assembler names mangled.
914
     This is done here rather than in gfc_finish_var_decl because it
915
     is different for string length variables.  */
916
  if (sym->module)
917
    SET_DECL_ASSEMBLER_NAME (decl, gfc_sym_mangled_identifier (sym));
918
 
919
  if (sym->attr.dimension)
920
    {
921
      /* Create variables to hold the non-constant bits of array info.  */
922
      gfc_build_qualified_array (decl, sym);
923
 
924
      /* Remember this variable for allocation/cleanup.  */
925
      gfc_defer_symbol_init (sym);
926
 
927
      if ((sym->attr.allocatable || !sym->attr.dummy) && !sym->attr.pointer)
928
        GFC_DECL_PACKED_ARRAY (decl) = 1;
929
    }
930
 
931
  gfc_finish_var_decl (decl, sym);
932
 
933
  if (sym->ts.type == BT_CHARACTER)
934
    {
935
      /* Character variables need special handling.  */
936
      gfc_allocate_lang_decl (decl);
937
 
938
      if (TREE_CODE (length) != INTEGER_CST)
939
        {
940
          char name[GFC_MAX_MANGLED_SYMBOL_LEN + 2];
941
 
942
          if (sym->module)
943
            {
944
              /* Also prefix the mangled name for symbols from modules.  */
945
              strcpy (&name[1], sym->name);
946
              name[0] = '.';
947
              strcpy (&name[1],
948
                      IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (length)));
949
              SET_DECL_ASSEMBLER_NAME (decl, get_identifier (name));
950
            }
951
          gfc_finish_var_decl (length, sym);
952
          gcc_assert (!sym->value);
953
        }
954
    }
955
  sym->backend_decl = decl;
956
 
957
  if (sym->attr.assign)
958
    {
959
      gfc_add_assign_aux_vars (sym);
960
    }
961
 
962
  if (TREE_STATIC (decl) && !sym->attr.use_assoc)
963
    {
964
      /* Add static initializer.  */
965
      DECL_INITIAL (decl) = gfc_conv_initializer (sym->value, &sym->ts,
966
          TREE_TYPE (decl), sym->attr.dimension,
967
          sym->attr.pointer || sym->attr.allocatable);
968
    }
969
 
970
  return decl;
971
}
972
 
973
 
974
/* Substitute a temporary variable in place of the real one.  */
975
 
976
void
977
gfc_shadow_sym (gfc_symbol * sym, tree decl, gfc_saved_var * save)
978
{
979
  save->attr = sym->attr;
980
  save->decl = sym->backend_decl;
981
 
982
  gfc_clear_attr (&sym->attr);
983
  sym->attr.referenced = 1;
984
  sym->attr.flavor = FL_VARIABLE;
985
 
986
  sym->backend_decl = decl;
987
}
988
 
989
 
990
/* Restore the original variable.  */
991
 
992
void
993
gfc_restore_sym (gfc_symbol * sym, gfc_saved_var * save)
994
{
995
  sym->attr = save->attr;
996
  sym->backend_decl = save->decl;
997
}
998
 
999
 
1000
/* Get a basic decl for an external function.  */
1001
 
1002
tree
1003
gfc_get_extern_function_decl (gfc_symbol * sym)
1004
{
1005
  tree type;
1006
  tree fndecl;
1007
  gfc_expr e;
1008
  gfc_intrinsic_sym *isym;
1009
  gfc_expr argexpr;
1010
  char s[GFC_MAX_SYMBOL_LEN + 13]; /* "f2c_specific" and '\0'.  */
1011
  tree name;
1012
  tree mangled_name;
1013
 
1014
  if (sym->backend_decl)
1015
    return sym->backend_decl;
1016
 
1017
  /* We should never be creating external decls for alternate entry points.
1018
     The procedure may be an alternate entry point, but we don't want/need
1019
     to know that.  */
1020
  gcc_assert (!(sym->attr.entry || sym->attr.entry_master));
1021
 
1022
  if (sym->attr.intrinsic)
1023
    {
1024
      /* Call the resolution function to get the actual name.  This is
1025
         a nasty hack which relies on the resolution functions only looking
1026
         at the first argument.  We pass NULL for the second argument
1027
         otherwise things like AINT get confused.  */
1028
      isym = gfc_find_function (sym->name);
1029
      gcc_assert (isym->resolve.f0 != NULL);
1030
 
1031
      memset (&e, 0, sizeof (e));
1032
      e.expr_type = EXPR_FUNCTION;
1033
 
1034
      memset (&argexpr, 0, sizeof (argexpr));
1035
      gcc_assert (isym->formal);
1036
      argexpr.ts = isym->formal->ts;
1037
 
1038
      if (isym->formal->next == NULL)
1039
        isym->resolve.f1 (&e, &argexpr);
1040
      else
1041
        {
1042
          /* All specific intrinsics take one or two arguments.  */
1043
          gcc_assert (isym->formal->next->next == NULL);
1044
          isym->resolve.f2 (&e, &argexpr, NULL);
1045
        }
1046
 
1047
      if (gfc_option.flag_f2c
1048
          && ((e.ts.type == BT_REAL && e.ts.kind == gfc_default_real_kind)
1049
              || e.ts.type == BT_COMPLEX))
1050
        {
1051
          /* Specific which needs a different implementation if f2c
1052
             calling conventions are used.  */
1053
          sprintf (s, "f2c_specific%s", e.value.function.name);
1054
        }
1055
      else
1056
        sprintf (s, "specific%s", e.value.function.name);
1057
 
1058
      name = get_identifier (s);
1059
      mangled_name = name;
1060
    }
1061
  else
1062
    {
1063
      name = gfc_sym_identifier (sym);
1064
      mangled_name = gfc_sym_mangled_function_id (sym);
1065
    }
1066
 
1067
  type = gfc_get_function_type (sym);
1068
  fndecl = build_decl (FUNCTION_DECL, name, type);
1069
 
1070
  SET_DECL_ASSEMBLER_NAME (fndecl, mangled_name);
1071
  /* If the return type is a pointer, avoid alias issues by setting
1072
     DECL_IS_MALLOC to nonzero. This means that the function should be
1073
     treated as if it were a malloc, meaning it returns a pointer that
1074
     is not an alias.  */
1075
  if (POINTER_TYPE_P (type))
1076
    DECL_IS_MALLOC (fndecl) = 1;
1077
 
1078
  /* Set the context of this decl.  */
1079
  if (0 && sym->ns && sym->ns->proc_name)
1080
    {
1081
      /* TODO: Add external decls to the appropriate scope.  */
1082
      DECL_CONTEXT (fndecl) = sym->ns->proc_name->backend_decl;
1083
    }
1084
  else
1085
    {
1086
      /* Global declaration, e.g. intrinsic subroutine.  */
1087
      DECL_CONTEXT (fndecl) = NULL_TREE;
1088
    }
1089
 
1090
  DECL_EXTERNAL (fndecl) = 1;
1091
 
1092
  /* This specifies if a function is globally addressable, i.e. it is
1093
     the opposite of declaring static in C.  */
1094
  TREE_PUBLIC (fndecl) = 1;
1095
 
1096
  /* Set attributes for PURE functions. A call to PURE function in the
1097
     Fortran 95 sense is both pure and without side effects in the C
1098
     sense.  */
1099
  if (sym->attr.pure || sym->attr.elemental)
1100
    {
1101
      if (sym->attr.function && !gfc_return_by_reference (sym))
1102
        DECL_IS_PURE (fndecl) = 1;
1103
      /* TODO: check if pure SUBROUTINEs don't have INTENT(OUT)
1104
         parameters and don't use alternate returns (is this
1105
         allowed?). In that case, calls to them are meaningless, and
1106
         can be optimized away. See also in build_function_decl().  */
1107
      TREE_SIDE_EFFECTS (fndecl) = 0;
1108
    }
1109
 
1110
  /* Mark non-returning functions.  */
1111
  if (sym->attr.noreturn)
1112
      TREE_THIS_VOLATILE(fndecl) = 1;
1113
 
1114
  sym->backend_decl = fndecl;
1115
 
1116
  if (DECL_CONTEXT (fndecl) == NULL_TREE)
1117
    pushdecl_top_level (fndecl);
1118
 
1119
  return fndecl;
1120
}
1121
 
1122
 
1123
/* Create a declaration for a procedure.  For external functions (in the C
1124
   sense) use gfc_get_extern_function_decl.  HAS_ENTRIES is true if this is
1125
   a master function with alternate entry points.  */
1126
 
1127
static void
1128
build_function_decl (gfc_symbol * sym)
1129
{
1130
  tree fndecl, type;
1131
  symbol_attribute attr;
1132
  tree result_decl;
1133
  gfc_formal_arglist *f;
1134
 
1135
  gcc_assert (!sym->backend_decl);
1136
  gcc_assert (!sym->attr.external);
1137
 
1138
  /* Set the line and filename.  sym->declared_at seems to point to the
1139
     last statement for subroutines, but it'll do for now.  */
1140
  gfc_set_backend_locus (&sym->declared_at);
1141
 
1142
  /* Allow only one nesting level.  Allow public declarations.  */
1143
  gcc_assert (current_function_decl == NULL_TREE
1144
          || DECL_CONTEXT (current_function_decl) == NULL_TREE);
1145
 
1146
  type = gfc_get_function_type (sym);
1147
  fndecl = build_decl (FUNCTION_DECL, gfc_sym_identifier (sym), type);
1148
 
1149
  /* Perform name mangling if this is a top level or module procedure.  */
1150
  if (current_function_decl == NULL_TREE)
1151
    SET_DECL_ASSEMBLER_NAME (fndecl, gfc_sym_mangled_function_id (sym));
1152
 
1153
  /* Figure out the return type of the declared function, and build a
1154
     RESULT_DECL for it.  If this is a subroutine with alternate
1155
     returns, build a RESULT_DECL for it.  */
1156
  attr = sym->attr;
1157
 
1158
  result_decl = NULL_TREE;
1159
  /* TODO: Shouldn't this just be TREE_TYPE (TREE_TYPE (fndecl)).  */
1160
  if (attr.function)
1161
    {
1162
      if (gfc_return_by_reference (sym))
1163
        type = void_type_node;
1164
      else
1165
        {
1166
          if (sym->result != sym)
1167
            result_decl = gfc_sym_identifier (sym->result);
1168
 
1169
          type = TREE_TYPE (TREE_TYPE (fndecl));
1170
        }
1171
    }
1172
  else
1173
    {
1174
      /* Look for alternate return placeholders.  */
1175
      int has_alternate_returns = 0;
1176
      for (f = sym->formal; f; f = f->next)
1177
        {
1178
          if (f->sym == NULL)
1179
            {
1180
              has_alternate_returns = 1;
1181
              break;
1182
            }
1183
        }
1184
 
1185
      if (has_alternate_returns)
1186
        type = integer_type_node;
1187
      else
1188
        type = void_type_node;
1189
    }
1190
 
1191
  result_decl = build_decl (RESULT_DECL, result_decl, type);
1192
  DECL_ARTIFICIAL (result_decl) = 1;
1193
  DECL_IGNORED_P (result_decl) = 1;
1194
  DECL_CONTEXT (result_decl) = fndecl;
1195
  DECL_RESULT (fndecl) = result_decl;
1196
 
1197
  /* Don't call layout_decl for a RESULT_DECL.
1198
     layout_decl (result_decl, 0);  */
1199
 
1200
  /* If the return type is a pointer, avoid alias issues by setting
1201
     DECL_IS_MALLOC to nonzero. This means that the function should be
1202
     treated as if it were a malloc, meaning it returns a pointer that
1203
     is not an alias.  */
1204
  if (POINTER_TYPE_P (type))
1205
    DECL_IS_MALLOC (fndecl) = 1;
1206
 
1207
  /* Set up all attributes for the function.  */
1208
  DECL_CONTEXT (fndecl) = current_function_decl;
1209
  DECL_EXTERNAL (fndecl) = 0;
1210
 
1211
  /* This specifies if a function is globally visible, i.e. it is
1212
     the opposite of declaring static in C.  */
1213
  if (DECL_CONTEXT (fndecl) == NULL_TREE
1214
      && !sym->attr.entry_master)
1215
    TREE_PUBLIC (fndecl) = 1;
1216
 
1217
  /* TREE_STATIC means the function body is defined here.  */
1218
  TREE_STATIC (fndecl) = 1;
1219
 
1220
  /* Set attributes for PURE functions. A call to a PURE function in the
1221
     Fortran 95 sense is both pure and without side effects in the C
1222
     sense.  */
1223
  if (attr.pure || attr.elemental)
1224
    {
1225
      /* TODO: check if a pure SUBROUTINE has no INTENT(OUT) arguments
1226
         including a alternate return. In that case it can also be
1227
         marked as PURE. See also in gfc_get_extern_function_decl().  */
1228
      if (attr.function && !gfc_return_by_reference (sym))
1229
        DECL_IS_PURE (fndecl) = 1;
1230
      TREE_SIDE_EFFECTS (fndecl) = 0;
1231
    }
1232
 
1233
  /* Layout the function declaration and put it in the binding level
1234
     of the current function.  */
1235
  pushdecl (fndecl);
1236
 
1237
  sym->backend_decl = fndecl;
1238
}
1239
 
1240
 
1241
/* Create the DECL_ARGUMENTS for a procedure.  */
1242
 
1243
static void
1244
create_function_arglist (gfc_symbol * sym)
1245
{
1246
  tree fndecl;
1247
  gfc_formal_arglist *f;
1248
  tree typelist;
1249
  tree arglist;
1250
  tree length;
1251
  tree type;
1252
  tree parm;
1253
 
1254
  fndecl = sym->backend_decl;
1255
 
1256
  /* Build formal argument list. Make sure that their TREE_CONTEXT is
1257
     the new FUNCTION_DECL node.  */
1258
  arglist = NULL_TREE;
1259
  typelist = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
1260
 
1261
  if (sym->attr.entry_master)
1262
    {
1263
      type = TREE_VALUE (typelist);
1264
      parm = build_decl (PARM_DECL, get_identifier ("__entry"), type);
1265
 
1266
      DECL_CONTEXT (parm) = fndecl;
1267
      DECL_ARG_TYPE (parm) = type;
1268
      TREE_READONLY (parm) = 1;
1269
      gfc_finish_decl (parm, NULL_TREE);
1270
 
1271
      arglist = chainon (arglist, parm);
1272
      typelist = TREE_CHAIN (typelist);
1273
    }
1274
 
1275
  if (gfc_return_by_reference (sym))
1276
    {
1277
      type = TREE_VALUE (typelist);
1278
      parm = build_decl (PARM_DECL, get_identifier ("__result"), type);
1279
 
1280
      DECL_CONTEXT (parm) = fndecl;
1281
      DECL_ARG_TYPE (parm) = type;
1282
      TREE_READONLY (parm) = 1;
1283
      DECL_ARTIFICIAL (parm) = 1;
1284
      gfc_finish_decl (parm, NULL_TREE);
1285
 
1286
      arglist = chainon (arglist, parm);
1287
      typelist = TREE_CHAIN (typelist);
1288
 
1289
      if (sym->ts.type == BT_CHARACTER)
1290
        {
1291
          gfc_allocate_lang_decl (parm);
1292
 
1293
          /* Length of character result.  */
1294
          type = TREE_VALUE (typelist);
1295
          gcc_assert (type == gfc_charlen_type_node);
1296
 
1297
          length = build_decl (PARM_DECL,
1298
                               get_identifier (".__result"),
1299
                               type);
1300
          if (!sym->ts.cl->length)
1301
            {
1302
              sym->ts.cl->backend_decl = length;
1303
              TREE_USED (length) = 1;
1304
            }
1305
          gcc_assert (TREE_CODE (length) == PARM_DECL);
1306
          arglist = chainon (arglist, length);
1307
          typelist = TREE_CHAIN (typelist);
1308
          DECL_CONTEXT (length) = fndecl;
1309
          DECL_ARG_TYPE (length) = type;
1310
          TREE_READONLY (length) = 1;
1311
          DECL_ARTIFICIAL (length) = 1;
1312
          gfc_finish_decl (length, NULL_TREE);
1313
        }
1314
    }
1315
 
1316
  for (f = sym->formal; f; f = f->next)
1317
    {
1318
      if (f->sym != NULL)       /* ignore alternate returns.  */
1319
        {
1320
          length = NULL_TREE;
1321
 
1322
          type = TREE_VALUE (typelist);
1323
 
1324
          /* Build a the argument declaration.  */
1325
          parm = build_decl (PARM_DECL,
1326
                             gfc_sym_identifier (f->sym), type);
1327
 
1328
          /* Fill in arg stuff.  */
1329
          DECL_CONTEXT (parm) = fndecl;
1330
          DECL_ARG_TYPE (parm) = type;
1331
          /* All implementation args are read-only.  */
1332
          TREE_READONLY (parm) = 1;
1333
 
1334
          gfc_finish_decl (parm, NULL_TREE);
1335
 
1336
          f->sym->backend_decl = parm;
1337
 
1338
          arglist = chainon (arglist, parm);
1339
          typelist = TREE_CHAIN (typelist);
1340
        }
1341
    }
1342
 
1343
  /* Add the hidden string length parameters.  */
1344
  parm = arglist;
1345
  for (f = sym->formal; f; f = f->next)
1346
    {
1347
      char name[GFC_MAX_SYMBOL_LEN + 2];
1348
      /* Ignore alternate returns.  */
1349
      if (f->sym == NULL)
1350
        continue;
1351
 
1352
      if (f->sym->ts.type != BT_CHARACTER)
1353
        continue;
1354
 
1355
      parm = f->sym->backend_decl;
1356
      type = TREE_VALUE (typelist);
1357
      gcc_assert (type == gfc_charlen_type_node);
1358
 
1359
      strcpy (&name[1], f->sym->name);
1360
      name[0] = '_';
1361
      length = build_decl (PARM_DECL, get_identifier (name), type);
1362
 
1363
      arglist = chainon (arglist, length);
1364
      DECL_CONTEXT (length) = fndecl;
1365
      DECL_ARTIFICIAL (length) = 1;
1366
      DECL_ARG_TYPE (length) = type;
1367
      TREE_READONLY (length) = 1;
1368
      gfc_finish_decl (length, NULL_TREE);
1369
 
1370
      /* TODO: Check string lengths when -fbounds-check.  */
1371
 
1372
      /* Use the passed value for assumed length variables.  */
1373
      if (!f->sym->ts.cl->length)
1374
        {
1375
          TREE_USED (length) = 1;
1376
          if (!f->sym->ts.cl->backend_decl)
1377
            f->sym->ts.cl->backend_decl = length;
1378
          else
1379
            {
1380
              /* there is already another variable using this
1381
                 gfc_charlen node, build a new one for this variable
1382
                 and chain it into the list of gfc_charlens.
1383
                 This happens for e.g. in the case
1384
                 CHARACTER(*)::c1,c2
1385
                 since CHARACTER declarations on the same line share
1386
                 the same gfc_charlen node.  */
1387
              gfc_charlen *cl;
1388
 
1389
              cl = gfc_get_charlen ();
1390
              cl->backend_decl = length;
1391
              cl->next = f->sym->ts.cl->next;
1392
              f->sym->ts.cl->next = cl;
1393
              f->sym->ts.cl = cl;
1394
            }
1395
        }
1396
 
1397
      parm = TREE_CHAIN (parm);
1398
      typelist = TREE_CHAIN (typelist);
1399
    }
1400
 
1401
  gcc_assert (TREE_VALUE (typelist) == void_type_node);
1402
  DECL_ARGUMENTS (fndecl) = arglist;
1403
}
1404
 
1405
/* Convert FNDECL's code to GIMPLE and handle any nested functions.  */
1406
 
1407
static void
1408
gfc_gimplify_function (tree fndecl)
1409
{
1410
  struct cgraph_node *cgn;
1411
 
1412
  gimplify_function_tree (fndecl);
1413
  dump_function (TDI_generic, fndecl);
1414
 
1415
  /* Convert all nested functions to GIMPLE now.  We do things in this order
1416
     so that items like VLA sizes are expanded properly in the context of the
1417
     correct function.  */
1418
  cgn = cgraph_node (fndecl);
1419
  for (cgn = cgn->nested; cgn; cgn = cgn->next_nested)
1420
    gfc_gimplify_function (cgn->decl);
1421
}
1422
 
1423
 
1424
/* Do the setup necessary before generating the body of a function.  */
1425
 
1426
static void
1427
trans_function_start (gfc_symbol * sym)
1428
{
1429
  tree fndecl;
1430
 
1431
  fndecl = sym->backend_decl;
1432
 
1433
  /* Let GCC know the current scope is this function.  */
1434
  current_function_decl = fndecl;
1435
 
1436
  /* Let the world know what we're about to do.  */
1437
  announce_function (fndecl);
1438
 
1439
  if (DECL_CONTEXT (fndecl) == NULL_TREE)
1440
    {
1441
      /* Create RTL for function declaration.  */
1442
      rest_of_decl_compilation (fndecl, 1, 0);
1443
    }
1444
 
1445
  /* Create RTL for function definition.  */
1446
  make_decl_rtl (fndecl);
1447
 
1448
  init_function_start (fndecl);
1449
 
1450
  /* Even though we're inside a function body, we still don't want to
1451
     call expand_expr to calculate the size of a variable-sized array.
1452
     We haven't necessarily assigned RTL to all variables yet, so it's
1453
     not safe to try to expand expressions involving them.  */
1454
  cfun->x_dont_save_pending_sizes_p = 1;
1455
 
1456
  /* function.c requires a push at the start of the function.  */
1457
  pushlevel (0);
1458
}
1459
 
1460
/* Create thunks for alternate entry points.  */
1461
 
1462
static void
1463
build_entry_thunks (gfc_namespace * ns)
1464
{
1465
  gfc_formal_arglist *formal;
1466
  gfc_formal_arglist *thunk_formal;
1467
  gfc_entry_list *el;
1468
  gfc_symbol *thunk_sym;
1469
  stmtblock_t body;
1470
  tree thunk_fndecl;
1471
  tree args;
1472
  tree string_args;
1473
  tree tmp;
1474
  locus old_loc;
1475
 
1476
  /* This should always be a toplevel function.  */
1477
  gcc_assert (current_function_decl == NULL_TREE);
1478
 
1479
  gfc_get_backend_locus (&old_loc);
1480
  for (el = ns->entries; el; el = el->next)
1481
    {
1482
      thunk_sym = el->sym;
1483
 
1484
      build_function_decl (thunk_sym);
1485
      create_function_arglist (thunk_sym);
1486
 
1487
      trans_function_start (thunk_sym);
1488
 
1489
      thunk_fndecl = thunk_sym->backend_decl;
1490
 
1491
      gfc_start_block (&body);
1492
 
1493
      /* Pass extra parameter identifying this entry point.  */
1494
      tmp = build_int_cst (gfc_array_index_type, el->id);
1495
      args = tree_cons (NULL_TREE, tmp, NULL_TREE);
1496
      string_args = NULL_TREE;
1497
 
1498
      if (thunk_sym->attr.function)
1499
        {
1500
          if (gfc_return_by_reference (ns->proc_name))
1501
            {
1502
              tree ref = DECL_ARGUMENTS (current_function_decl);
1503
              args = tree_cons (NULL_TREE, ref, args);
1504
              if (ns->proc_name->ts.type == BT_CHARACTER)
1505
                args = tree_cons (NULL_TREE, TREE_CHAIN (ref),
1506
                                  args);
1507
            }
1508
        }
1509
 
1510
      for (formal = ns->proc_name->formal; formal; formal = formal->next)
1511
        {
1512
          /* Ignore alternate returns.  */
1513
          if (formal->sym == NULL)
1514
            continue;
1515
 
1516
          /* We don't have a clever way of identifying arguments, so resort to
1517
             a brute-force search.  */
1518
          for (thunk_formal = thunk_sym->formal;
1519
               thunk_formal;
1520
               thunk_formal = thunk_formal->next)
1521
            {
1522
              if (thunk_formal->sym == formal->sym)
1523
                break;
1524
            }
1525
 
1526
          if (thunk_formal)
1527
            {
1528
              /* Pass the argument.  */
1529
              args = tree_cons (NULL_TREE, thunk_formal->sym->backend_decl,
1530
                                args);
1531
              if (formal->sym->ts.type == BT_CHARACTER)
1532
                {
1533
                  tmp = thunk_formal->sym->ts.cl->backend_decl;
1534
                  string_args = tree_cons (NULL_TREE, tmp, string_args);
1535
                }
1536
            }
1537
          else
1538
            {
1539
              /* Pass NULL for a missing argument.  */
1540
              args = tree_cons (NULL_TREE, null_pointer_node, args);
1541
              if (formal->sym->ts.type == BT_CHARACTER)
1542
                {
1543
                  tmp = convert (gfc_charlen_type_node, integer_zero_node);
1544
                  string_args = tree_cons (NULL_TREE, tmp, string_args);
1545
                }
1546
            }
1547
        }
1548
 
1549
      /* Call the master function.  */
1550
      args = nreverse (args);
1551
      args = chainon (args, nreverse (string_args));
1552
      tmp = ns->proc_name->backend_decl;
1553
      tmp = gfc_build_function_call (tmp, args);
1554
      if (ns->proc_name->attr.mixed_entry_master)
1555
        {
1556
          tree union_decl, field;
1557
          tree master_type = TREE_TYPE (ns->proc_name->backend_decl);
1558
 
1559
          union_decl = build_decl (VAR_DECL, get_identifier ("__result"),
1560
                                   TREE_TYPE (master_type));
1561
          DECL_ARTIFICIAL (union_decl) = 1;
1562
          DECL_EXTERNAL (union_decl) = 0;
1563
          TREE_PUBLIC (union_decl) = 0;
1564
          TREE_USED (union_decl) = 1;
1565
          layout_decl (union_decl, 0);
1566
          pushdecl (union_decl);
1567
 
1568
          DECL_CONTEXT (union_decl) = current_function_decl;
1569
          tmp = build2 (MODIFY_EXPR,
1570
                        TREE_TYPE (union_decl),
1571
                        union_decl, tmp);
1572
          gfc_add_expr_to_block (&body, tmp);
1573
 
1574
          for (field = TYPE_FIELDS (TREE_TYPE (union_decl));
1575
               field; field = TREE_CHAIN (field))
1576
            if (strcmp (IDENTIFIER_POINTER (DECL_NAME (field)),
1577
                thunk_sym->result->name) == 0)
1578
              break;
1579
          gcc_assert (field != NULL_TREE);
1580
          tmp = build3 (COMPONENT_REF, TREE_TYPE (field), union_decl, field,
1581
                        NULL_TREE);
1582
          tmp = build2 (MODIFY_EXPR,
1583
                        TREE_TYPE (DECL_RESULT (current_function_decl)),
1584
                        DECL_RESULT (current_function_decl), tmp);
1585
          tmp = build1_v (RETURN_EXPR, tmp);
1586
        }
1587
      else if (TREE_TYPE (DECL_RESULT (current_function_decl))
1588
               != void_type_node)
1589
        {
1590
          tmp = build2 (MODIFY_EXPR,
1591
                        TREE_TYPE (DECL_RESULT (current_function_decl)),
1592
                        DECL_RESULT (current_function_decl), tmp);
1593
          tmp = build1_v (RETURN_EXPR, tmp);
1594
        }
1595
      gfc_add_expr_to_block (&body, tmp);
1596
 
1597
      /* Finish off this function and send it for code generation.  */
1598
      DECL_SAVED_TREE (thunk_fndecl) = gfc_finish_block (&body);
1599
      poplevel (1, 0, 1);
1600
      BLOCK_SUPERCONTEXT (DECL_INITIAL (thunk_fndecl)) = thunk_fndecl;
1601
 
1602
      /* Output the GENERIC tree.  */
1603
      dump_function (TDI_original, thunk_fndecl);
1604
 
1605
      /* Store the end of the function, so that we get good line number
1606
         info for the epilogue.  */
1607
      cfun->function_end_locus = input_location;
1608
 
1609
      /* We're leaving the context of this function, so zap cfun.
1610
         It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
1611
         tree_rest_of_compilation.  */
1612
      cfun = NULL;
1613
 
1614
      current_function_decl = NULL_TREE;
1615
 
1616
      gfc_gimplify_function (thunk_fndecl);
1617
      cgraph_finalize_function (thunk_fndecl, false);
1618
 
1619
      /* We share the symbols in the formal argument list with other entry
1620
         points and the master function.  Clear them so that they are
1621
         recreated for each function.  */
1622
      for (formal = thunk_sym->formal; formal; formal = formal->next)
1623
        if (formal->sym != NULL)  /* Ignore alternate returns.  */
1624
          {
1625
            formal->sym->backend_decl = NULL_TREE;
1626
            if (formal->sym->ts.type == BT_CHARACTER)
1627
              formal->sym->ts.cl->backend_decl = NULL_TREE;
1628
          }
1629
 
1630
      if (thunk_sym->attr.function)
1631
        {
1632
          if (thunk_sym->ts.type == BT_CHARACTER)
1633
            thunk_sym->ts.cl->backend_decl = NULL_TREE;
1634
          if (thunk_sym->result->ts.type == BT_CHARACTER)
1635
            thunk_sym->result->ts.cl->backend_decl = NULL_TREE;
1636
        }
1637
    }
1638
 
1639
  gfc_set_backend_locus (&old_loc);
1640
}
1641
 
1642
 
1643
/* Create a decl for a function, and create any thunks for alternate entry
1644
   points.  */
1645
 
1646
void
1647
gfc_create_function_decl (gfc_namespace * ns)
1648
{
1649
  /* Create a declaration for the master function.  */
1650
  build_function_decl (ns->proc_name);
1651
 
1652
  /* Compile the entry thunks.  */
1653
  if (ns->entries)
1654
    build_entry_thunks (ns);
1655
 
1656
  /* Now create the read argument list.  */
1657
  create_function_arglist (ns->proc_name);
1658
}
1659
 
1660
/* Return the decl used to hold the function return value.  */
1661
 
1662
tree
1663
gfc_get_fake_result_decl (gfc_symbol * sym)
1664
{
1665
  tree decl;
1666
  tree length;
1667
 
1668
  char name[GFC_MAX_SYMBOL_LEN + 10];
1669
 
1670
  if (sym
1671
      && sym->ns->proc_name->backend_decl == current_function_decl
1672
      && sym->ns->proc_name->attr.mixed_entry_master
1673
      && sym != sym->ns->proc_name)
1674
    {
1675
      decl = gfc_get_fake_result_decl (sym->ns->proc_name);
1676
      if (decl)
1677
        {
1678
          tree field;
1679
 
1680
          for (field = TYPE_FIELDS (TREE_TYPE (decl));
1681
               field; field = TREE_CHAIN (field))
1682
            if (strcmp (IDENTIFIER_POINTER (DECL_NAME (field)),
1683
                sym->name) == 0)
1684
              break;
1685
 
1686
          gcc_assert (field != NULL_TREE);
1687
          decl = build3 (COMPONENT_REF, TREE_TYPE (field), decl, field,
1688
                         NULL_TREE);
1689
        }
1690
      return decl;
1691
    }
1692
 
1693
  if (current_fake_result_decl != NULL_TREE)
1694
    return current_fake_result_decl;
1695
 
1696
  /* Only when gfc_get_fake_result_decl is called by gfc_trans_return,
1697
     sym is NULL.  */
1698
  if (!sym)
1699
    return NULL_TREE;
1700
 
1701
  if (sym->ts.type == BT_CHARACTER
1702
      && !sym->ts.cl->backend_decl)
1703
    {
1704
      length = gfc_create_string_length (sym);
1705
      gfc_finish_var_decl (length, sym);
1706
    }
1707
 
1708
  if (gfc_return_by_reference (sym))
1709
    {
1710
      decl = DECL_ARGUMENTS (current_function_decl);
1711
 
1712
      if (sym->ns->proc_name->backend_decl == current_function_decl
1713
          && sym->ns->proc_name->attr.entry_master)
1714
        decl = TREE_CHAIN (decl);
1715
 
1716
      TREE_USED (decl) = 1;
1717
      if (sym->as)
1718
        decl = gfc_build_dummy_array_decl (sym, decl);
1719
    }
1720
  else
1721
    {
1722
      sprintf (name, "__result_%.20s",
1723
               IDENTIFIER_POINTER (DECL_NAME (current_function_decl)));
1724
 
1725
      decl = build_decl (VAR_DECL, get_identifier (name),
1726
                         TREE_TYPE (TREE_TYPE (current_function_decl)));
1727
 
1728
      DECL_ARTIFICIAL (decl) = 1;
1729
      DECL_EXTERNAL (decl) = 0;
1730
      TREE_PUBLIC (decl) = 0;
1731
      TREE_USED (decl) = 1;
1732
 
1733
      layout_decl (decl, 0);
1734
 
1735
      gfc_add_decl_to_function (decl);
1736
    }
1737
 
1738
  current_fake_result_decl = decl;
1739
 
1740
  return decl;
1741
}
1742
 
1743
 
1744
/* Builds a function decl.  The remaining parameters are the types of the
1745
   function arguments.  Negative nargs indicates a varargs function.  */
1746
 
1747
tree
1748
gfc_build_library_function_decl (tree name, tree rettype, int nargs, ...)
1749
{
1750
  tree arglist;
1751
  tree argtype;
1752
  tree fntype;
1753
  tree fndecl;
1754
  va_list p;
1755
  int n;
1756
 
1757
  /* Library functions must be declared with global scope.  */
1758
  gcc_assert (current_function_decl == NULL_TREE);
1759
 
1760
  va_start (p, nargs);
1761
 
1762
 
1763
  /* Create a list of the argument types.  */
1764
  for (arglist = NULL_TREE, n = abs (nargs); n > 0; n--)
1765
    {
1766
      argtype = va_arg (p, tree);
1767
      arglist = gfc_chainon_list (arglist, argtype);
1768
    }
1769
 
1770
  if (nargs >= 0)
1771
    {
1772
      /* Terminate the list.  */
1773
      arglist = gfc_chainon_list (arglist, void_type_node);
1774
    }
1775
 
1776
  /* Build the function type and decl.  */
1777
  fntype = build_function_type (rettype, arglist);
1778
  fndecl = build_decl (FUNCTION_DECL, name, fntype);
1779
 
1780
  /* Mark this decl as external.  */
1781
  DECL_EXTERNAL (fndecl) = 1;
1782
  TREE_PUBLIC (fndecl) = 1;
1783
 
1784
  va_end (p);
1785
 
1786
  pushdecl (fndecl);
1787
 
1788
  rest_of_decl_compilation (fndecl, 1, 0);
1789
 
1790
  return fndecl;
1791
}
1792
 
1793
static void
1794
gfc_build_intrinsic_function_decls (void)
1795
{
1796
  tree gfc_int4_type_node = gfc_get_int_type (4);
1797
  tree gfc_int8_type_node = gfc_get_int_type (8);
1798
  tree gfc_int16_type_node = gfc_get_int_type (16);
1799
  tree gfc_logical4_type_node = gfc_get_logical_type (4);
1800
  tree gfc_real4_type_node = gfc_get_real_type (4);
1801
  tree gfc_real8_type_node = gfc_get_real_type (8);
1802
  tree gfc_real10_type_node = gfc_get_real_type (10);
1803
  tree gfc_real16_type_node = gfc_get_real_type (16);
1804
  tree gfc_complex4_type_node = gfc_get_complex_type (4);
1805
  tree gfc_complex8_type_node = gfc_get_complex_type (8);
1806
  tree gfc_complex10_type_node = gfc_get_complex_type (10);
1807
  tree gfc_complex16_type_node = gfc_get_complex_type (16);
1808
  tree gfc_c_int_type_node = gfc_get_int_type (gfc_c_int_kind);
1809
 
1810
  /* String functions.  */
1811
  gfor_fndecl_copy_string =
1812
    gfc_build_library_function_decl (get_identifier (PREFIX("copy_string")),
1813
                                     void_type_node,
1814
                                     4,
1815
                                     gfc_charlen_type_node, pchar_type_node,
1816
                                     gfc_charlen_type_node, pchar_type_node);
1817
 
1818
  gfor_fndecl_compare_string =
1819
    gfc_build_library_function_decl (get_identifier (PREFIX("compare_string")),
1820
                                     gfc_int4_type_node,
1821
                                     4,
1822
                                     gfc_charlen_type_node, pchar_type_node,
1823
                                     gfc_charlen_type_node, pchar_type_node);
1824
 
1825
  gfor_fndecl_concat_string =
1826
    gfc_build_library_function_decl (get_identifier (PREFIX("concat_string")),
1827
                                     void_type_node,
1828
                                     6,
1829
                                     gfc_charlen_type_node, pchar_type_node,
1830
                                     gfc_charlen_type_node, pchar_type_node,
1831
                                     gfc_charlen_type_node, pchar_type_node);
1832
 
1833
  gfor_fndecl_string_len_trim =
1834
    gfc_build_library_function_decl (get_identifier (PREFIX("string_len_trim")),
1835
                                     gfc_int4_type_node,
1836
                                     2, gfc_charlen_type_node,
1837
                                     pchar_type_node);
1838
 
1839
  gfor_fndecl_string_index =
1840
    gfc_build_library_function_decl (get_identifier (PREFIX("string_index")),
1841
                                     gfc_int4_type_node,
1842
                                     5, gfc_charlen_type_node, pchar_type_node,
1843
                                     gfc_charlen_type_node, pchar_type_node,
1844
                                     gfc_logical4_type_node);
1845
 
1846
  gfor_fndecl_string_scan =
1847
    gfc_build_library_function_decl (get_identifier (PREFIX("string_scan")),
1848
                                     gfc_int4_type_node,
1849
                                     5, gfc_charlen_type_node, pchar_type_node,
1850
                                     gfc_charlen_type_node, pchar_type_node,
1851
                                     gfc_logical4_type_node);
1852
 
1853
  gfor_fndecl_string_verify =
1854
    gfc_build_library_function_decl (get_identifier (PREFIX("string_verify")),
1855
                                     gfc_int4_type_node,
1856
                                     5, gfc_charlen_type_node, pchar_type_node,
1857
                                     gfc_charlen_type_node, pchar_type_node,
1858
                                     gfc_logical4_type_node);
1859
 
1860
  gfor_fndecl_string_trim =
1861
    gfc_build_library_function_decl (get_identifier (PREFIX("string_trim")),
1862
                                     void_type_node,
1863
                                     4,
1864
                                     build_pointer_type (gfc_charlen_type_node),
1865
                                     ppvoid_type_node,
1866
                                     gfc_charlen_type_node,
1867
                                     pchar_type_node);
1868
 
1869
  gfor_fndecl_string_repeat =
1870
    gfc_build_library_function_decl (get_identifier (PREFIX("string_repeat")),
1871
                                     void_type_node,
1872
                                     4,
1873
                                     pchar_type_node,
1874
                                     gfc_charlen_type_node,
1875
                                     pchar_type_node,
1876
                                     gfc_int4_type_node);
1877
 
1878
  gfor_fndecl_ttynam =
1879
    gfc_build_library_function_decl (get_identifier (PREFIX("ttynam")),
1880
                                     void_type_node,
1881
                                     3,
1882
                                     pchar_type_node,
1883
                                     gfc_charlen_type_node,
1884
                                     gfc_c_int_type_node);
1885
 
1886
  gfor_fndecl_fdate =
1887
    gfc_build_library_function_decl (get_identifier (PREFIX("fdate")),
1888
                                     void_type_node,
1889
                                     2,
1890
                                     pchar_type_node,
1891
                                     gfc_charlen_type_node);
1892
 
1893
  gfor_fndecl_ctime =
1894
    gfc_build_library_function_decl (get_identifier (PREFIX("ctime")),
1895
                                     void_type_node,
1896
                                     3,
1897
                                     pchar_type_node,
1898
                                     gfc_charlen_type_node,
1899
                                     gfc_int8_type_node);
1900
 
1901
  gfor_fndecl_adjustl =
1902
    gfc_build_library_function_decl (get_identifier (PREFIX("adjustl")),
1903
                                     void_type_node,
1904
                                     3,
1905
                                     pchar_type_node,
1906
                                     gfc_charlen_type_node, pchar_type_node);
1907
 
1908
  gfor_fndecl_adjustr =
1909
    gfc_build_library_function_decl (get_identifier (PREFIX("adjustr")),
1910
                                     void_type_node,
1911
                                     3,
1912
                                     pchar_type_node,
1913
                                     gfc_charlen_type_node, pchar_type_node);
1914
 
1915
  gfor_fndecl_si_kind =
1916
    gfc_build_library_function_decl (get_identifier ("selected_int_kind"),
1917
                                     gfc_int4_type_node,
1918
                                     1,
1919
                                     pvoid_type_node);
1920
 
1921
  gfor_fndecl_sr_kind =
1922
    gfc_build_library_function_decl (get_identifier ("selected_real_kind"),
1923
                                     gfc_int4_type_node,
1924
                                     2, pvoid_type_node,
1925
                                     pvoid_type_node);
1926
 
1927
  /* Power functions.  */
1928
  {
1929
    tree ctype, rtype, itype, jtype;
1930
    int rkind, ikind, jkind;
1931
#define NIKINDS 3
1932
#define NRKINDS 4
1933
    static int ikinds[NIKINDS] = {4, 8, 16};
1934
    static int rkinds[NRKINDS] = {4, 8, 10, 16};
1935
    char name[PREFIX_LEN + 12]; /* _gfortran_pow_?n_?n */
1936
 
1937
    for (ikind=0; ikind < NIKINDS; ikind++)
1938
      {
1939
        itype = gfc_get_int_type (ikinds[ikind]);
1940
 
1941
        for (jkind=0; jkind < NIKINDS; jkind++)
1942
          {
1943
            jtype = gfc_get_int_type (ikinds[jkind]);
1944
            if (itype && jtype)
1945
              {
1946
                sprintf(name, PREFIX("pow_i%d_i%d"), ikinds[ikind],
1947
                        ikinds[jkind]);
1948
                gfor_fndecl_math_powi[jkind][ikind].integer =
1949
                  gfc_build_library_function_decl (get_identifier (name),
1950
                    jtype, 2, jtype, itype);
1951
              }
1952
          }
1953
 
1954
        for (rkind = 0; rkind < NRKINDS; rkind ++)
1955
          {
1956
            rtype = gfc_get_real_type (rkinds[rkind]);
1957
            if (rtype && itype)
1958
              {
1959
                sprintf(name, PREFIX("pow_r%d_i%d"), rkinds[rkind],
1960
                        ikinds[ikind]);
1961
                gfor_fndecl_math_powi[rkind][ikind].real =
1962
                  gfc_build_library_function_decl (get_identifier (name),
1963
                    rtype, 2, rtype, itype);
1964
              }
1965
 
1966
            ctype = gfc_get_complex_type (rkinds[rkind]);
1967
            if (ctype && itype)
1968
              {
1969
                sprintf(name, PREFIX("pow_c%d_i%d"), rkinds[rkind],
1970
                        ikinds[ikind]);
1971
                gfor_fndecl_math_powi[rkind][ikind].cmplx =
1972
                  gfc_build_library_function_decl (get_identifier (name),
1973
                    ctype, 2,ctype, itype);
1974
              }
1975
          }
1976
      }
1977
#undef NIKINDS
1978
#undef NRKINDS
1979
  }
1980
 
1981
  gfor_fndecl_math_cpowf =
1982
    gfc_build_library_function_decl (get_identifier ("cpowf"),
1983
                                     gfc_complex4_type_node,
1984
                                     1, gfc_complex4_type_node);
1985
  gfor_fndecl_math_cpow =
1986
    gfc_build_library_function_decl (get_identifier ("cpow"),
1987
                                     gfc_complex8_type_node,
1988
                                     1, gfc_complex8_type_node);
1989
  if (gfc_complex10_type_node)
1990
    gfor_fndecl_math_cpowl10 =
1991
      gfc_build_library_function_decl (get_identifier ("cpowl"),
1992
                                       gfc_complex10_type_node, 1,
1993
                                       gfc_complex10_type_node);
1994
  if (gfc_complex16_type_node)
1995
    gfor_fndecl_math_cpowl16 =
1996
      gfc_build_library_function_decl (get_identifier ("cpowl"),
1997
                                       gfc_complex16_type_node, 1,
1998
                                       gfc_complex16_type_node);
1999
 
2000
  gfor_fndecl_math_ishftc4 =
2001
    gfc_build_library_function_decl (get_identifier (PREFIX("ishftc4")),
2002
                                     gfc_int4_type_node,
2003
                                     3, gfc_int4_type_node,
2004
                                     gfc_int4_type_node, gfc_int4_type_node);
2005
  gfor_fndecl_math_ishftc8 =
2006
    gfc_build_library_function_decl (get_identifier (PREFIX("ishftc8")),
2007
                                     gfc_int8_type_node,
2008
                                     3, gfc_int8_type_node,
2009
                                     gfc_int4_type_node, gfc_int4_type_node);
2010
  if (gfc_int16_type_node)
2011
    gfor_fndecl_math_ishftc16 =
2012
      gfc_build_library_function_decl (get_identifier (PREFIX("ishftc16")),
2013
                                       gfc_int16_type_node, 3,
2014
                                       gfc_int16_type_node,
2015
                                       gfc_int4_type_node,
2016
                                       gfc_int4_type_node);
2017
 
2018
  gfor_fndecl_math_exponent4 =
2019
    gfc_build_library_function_decl (get_identifier (PREFIX("exponent_r4")),
2020
                                     gfc_int4_type_node,
2021
                                     1, gfc_real4_type_node);
2022
  gfor_fndecl_math_exponent8 =
2023
    gfc_build_library_function_decl (get_identifier (PREFIX("exponent_r8")),
2024
                                     gfc_int4_type_node,
2025
                                     1, gfc_real8_type_node);
2026
  if (gfc_real10_type_node)
2027
    gfor_fndecl_math_exponent10 =
2028
      gfc_build_library_function_decl (get_identifier (PREFIX("exponent_r10")),
2029
                                       gfc_int4_type_node, 1,
2030
                                       gfc_real10_type_node);
2031
  if (gfc_real16_type_node)
2032
    gfor_fndecl_math_exponent16 =
2033
      gfc_build_library_function_decl (get_identifier (PREFIX("exponent_r16")),
2034
                                       gfc_int4_type_node, 1,
2035
                                       gfc_real16_type_node);
2036
 
2037
  /* Other functions.  */
2038
  gfor_fndecl_size0 =
2039
    gfc_build_library_function_decl (get_identifier (PREFIX("size0")),
2040
                                     gfc_array_index_type,
2041
                                     1, pvoid_type_node);
2042
  gfor_fndecl_size1 =
2043
    gfc_build_library_function_decl (get_identifier (PREFIX("size1")),
2044
                                     gfc_array_index_type,
2045
                                     2, pvoid_type_node,
2046
                                     gfc_array_index_type);
2047
 
2048
  gfor_fndecl_iargc =
2049
    gfc_build_library_function_decl (get_identifier (PREFIX ("iargc")),
2050
                                     gfc_int4_type_node,
2051
                                     0);
2052
}
2053
 
2054
 
2055
/* Make prototypes for runtime library functions.  */
2056
 
2057
void
2058
gfc_build_builtin_function_decls (void)
2059
{
2060
  tree gfc_c_int_type_node = gfc_get_int_type (gfc_c_int_kind);
2061
  tree gfc_int4_type_node = gfc_get_int_type (4);
2062
  tree gfc_int8_type_node = gfc_get_int_type (8);
2063
  tree gfc_logical4_type_node = gfc_get_logical_type (4);
2064
  tree gfc_pint4_type_node = build_pointer_type (gfc_int4_type_node);
2065
 
2066
  /* Treat these two internal malloc wrappers as malloc.  */
2067
  gfor_fndecl_internal_malloc =
2068
    gfc_build_library_function_decl (get_identifier (PREFIX("internal_malloc")),
2069
                                     pvoid_type_node, 1, gfc_int4_type_node);
2070
  DECL_IS_MALLOC (gfor_fndecl_internal_malloc) = 1;
2071
 
2072
  gfor_fndecl_internal_malloc64 =
2073
    gfc_build_library_function_decl (get_identifier
2074
                                     (PREFIX("internal_malloc64")),
2075
                                     pvoid_type_node, 1, gfc_int8_type_node);
2076
  DECL_IS_MALLOC (gfor_fndecl_internal_malloc64) = 1;
2077
 
2078
  gfor_fndecl_internal_realloc =
2079
    gfc_build_library_function_decl (get_identifier
2080
                                     (PREFIX("internal_realloc")),
2081
                                     pvoid_type_node, 2, pvoid_type_node,
2082
                                     gfc_int4_type_node);
2083
 
2084
  gfor_fndecl_internal_realloc64 =
2085
    gfc_build_library_function_decl (get_identifier
2086
                                     (PREFIX("internal_realloc64")),
2087
                                     pvoid_type_node, 2, pvoid_type_node,
2088
                                     gfc_int8_type_node);
2089
 
2090
  gfor_fndecl_internal_free =
2091
    gfc_build_library_function_decl (get_identifier (PREFIX("internal_free")),
2092
                                     void_type_node, 1, pvoid_type_node);
2093
 
2094
  gfor_fndecl_allocate =
2095
    gfc_build_library_function_decl (get_identifier (PREFIX("allocate")),
2096
                                     void_type_node, 2, ppvoid_type_node,
2097
                                     gfc_int4_type_node);
2098
 
2099
  gfor_fndecl_allocate64 =
2100
    gfc_build_library_function_decl (get_identifier (PREFIX("allocate64")),
2101
                                     void_type_node, 2, ppvoid_type_node,
2102
                                     gfc_int8_type_node);
2103
 
2104
  gfor_fndecl_allocate_array =
2105
    gfc_build_library_function_decl (get_identifier (PREFIX("allocate_array")),
2106
                                     void_type_node, 2, ppvoid_type_node,
2107
                                     gfc_int4_type_node);
2108
 
2109
  gfor_fndecl_allocate64_array =
2110
    gfc_build_library_function_decl (get_identifier (PREFIX("allocate64_array")),
2111
                                     void_type_node, 2, ppvoid_type_node,
2112
                                     gfc_int8_type_node);
2113
 
2114
  gfor_fndecl_deallocate =
2115
    gfc_build_library_function_decl (get_identifier (PREFIX("deallocate")),
2116
                                     void_type_node, 2, ppvoid_type_node,
2117
                                     gfc_pint4_type_node);
2118
 
2119
  gfor_fndecl_stop_numeric =
2120
    gfc_build_library_function_decl (get_identifier (PREFIX("stop_numeric")),
2121
                                     void_type_node, 1, gfc_int4_type_node);
2122
 
2123
  /* Stop doesn't return.  */
2124
  TREE_THIS_VOLATILE (gfor_fndecl_stop_numeric) = 1;
2125
 
2126
  gfor_fndecl_stop_string =
2127
    gfc_build_library_function_decl (get_identifier (PREFIX("stop_string")),
2128
                                     void_type_node, 2, pchar_type_node,
2129
                                     gfc_int4_type_node);
2130
  /* Stop doesn't return.  */
2131
  TREE_THIS_VOLATILE (gfor_fndecl_stop_string) = 1;
2132
 
2133
  gfor_fndecl_pause_numeric =
2134
    gfc_build_library_function_decl (get_identifier (PREFIX("pause_numeric")),
2135
                                     void_type_node, 1, gfc_int4_type_node);
2136
 
2137
  gfor_fndecl_pause_string =
2138
    gfc_build_library_function_decl (get_identifier (PREFIX("pause_string")),
2139
                                     void_type_node, 2, pchar_type_node,
2140
                                     gfc_int4_type_node);
2141
 
2142
  gfor_fndecl_select_string =
2143
    gfc_build_library_function_decl (get_identifier (PREFIX("select_string")),
2144
                                     pvoid_type_node, 0);
2145
 
2146
  gfor_fndecl_runtime_error =
2147
    gfc_build_library_function_decl (get_identifier (PREFIX("runtime_error")),
2148
                                     void_type_node,
2149
                                     3,
2150
                                     pchar_type_node, pchar_type_node,
2151
                                     gfc_int4_type_node);
2152
  /* The runtime_error function does not return.  */
2153
  TREE_THIS_VOLATILE (gfor_fndecl_runtime_error) = 1;
2154
 
2155
  gfor_fndecl_set_fpe =
2156
    gfc_build_library_function_decl (get_identifier (PREFIX("set_fpe")),
2157
                                    void_type_node, 1, gfc_c_int_type_node);
2158
 
2159
  gfor_fndecl_set_std =
2160
    gfc_build_library_function_decl (get_identifier (PREFIX("set_std")),
2161
                                    void_type_node,
2162
                                    3,
2163
                                    gfc_int4_type_node,
2164
                                    gfc_int4_type_node,
2165
                                    gfc_int4_type_node);
2166
 
2167
  gfor_fndecl_set_convert =
2168
    gfc_build_library_function_decl (get_identifier (PREFIX("set_convert")),
2169
                                     void_type_node, 1, gfc_c_int_type_node);
2170
 
2171
  gfor_fndecl_set_record_marker =
2172
    gfc_build_library_function_decl (get_identifier (PREFIX("set_record_marker")),
2173
                                     void_type_node, 1, gfc_c_int_type_node);
2174
 
2175
  gfor_fndecl_in_pack = gfc_build_library_function_decl (
2176
        get_identifier (PREFIX("internal_pack")),
2177
        pvoid_type_node, 1, pvoid_type_node);
2178
 
2179
  gfor_fndecl_in_unpack = gfc_build_library_function_decl (
2180
        get_identifier (PREFIX("internal_unpack")),
2181
        pvoid_type_node, 1, pvoid_type_node);
2182
 
2183
  gfor_fndecl_associated =
2184
    gfc_build_library_function_decl (
2185
                                     get_identifier (PREFIX("associated")),
2186
                                     gfc_logical4_type_node,
2187
                                     2,
2188
                                     ppvoid_type_node,
2189
                                     ppvoid_type_node);
2190
 
2191
  gfc_build_intrinsic_function_decls ();
2192
  gfc_build_intrinsic_lib_fndecls ();
2193
  gfc_build_io_library_fndecls ();
2194
}
2195
 
2196
 
2197
/* Evaluate the length of dummy character variables.  */
2198
 
2199
static tree
2200
gfc_trans_dummy_character (gfc_charlen * cl, tree fnbody)
2201
{
2202
  stmtblock_t body;
2203
 
2204
  gfc_finish_decl (cl->backend_decl, NULL_TREE);
2205
 
2206
  gfc_start_block (&body);
2207
 
2208
  /* Evaluate the string length expression.  */
2209
  gfc_trans_init_string_length (cl, &body);
2210
 
2211
  gfc_add_expr_to_block (&body, fnbody);
2212
  return gfc_finish_block (&body);
2213
}
2214
 
2215
 
2216
/* Allocate and cleanup an automatic character variable.  */
2217
 
2218
static tree
2219
gfc_trans_auto_character_variable (gfc_symbol * sym, tree fnbody)
2220
{
2221
  stmtblock_t body;
2222
  tree decl;
2223
  tree tmp;
2224
 
2225
  gcc_assert (sym->backend_decl);
2226
  gcc_assert (sym->ts.cl && sym->ts.cl->length);
2227
 
2228
  gfc_start_block (&body);
2229
 
2230
  /* Evaluate the string length expression.  */
2231
  gfc_trans_init_string_length (sym->ts.cl, &body);
2232
 
2233
  decl = sym->backend_decl;
2234
 
2235
  /* Emit a DECL_EXPR for this variable, which will cause the
2236
     gimplifier to allocate storage, and all that good stuff.  */
2237
  tmp = build1 (DECL_EXPR, TREE_TYPE (decl), decl);
2238
  gfc_add_expr_to_block (&body, tmp);
2239
 
2240
  gfc_add_expr_to_block (&body, fnbody);
2241
  return gfc_finish_block (&body);
2242
}
2243
 
2244
/* Set the initial value of ASSIGN statement auxiliary variable explicitly.  */
2245
 
2246
static tree
2247
gfc_trans_assign_aux_var (gfc_symbol * sym, tree fnbody)
2248
{
2249
  stmtblock_t body;
2250
 
2251
  gcc_assert (sym->backend_decl);
2252
  gfc_start_block (&body);
2253
 
2254
  /* Set the initial value to length. See the comments in
2255
     function gfc_add_assign_aux_vars in this file.  */
2256
  gfc_add_modify_expr (&body, GFC_DECL_STRING_LEN (sym->backend_decl),
2257
                       build_int_cst (NULL_TREE, -2));
2258
 
2259
  gfc_add_expr_to_block (&body, fnbody);
2260
  return gfc_finish_block (&body);
2261
}
2262
 
2263
 
2264
/* Generate function entry and exit code, and add it to the function body.
2265
   This includes:
2266
    Allocation and initialization of array variables.
2267
    Allocation of character string variables.
2268
    Initialization and possibly repacking of dummy arrays.
2269
    Initialization of ASSIGN statement auxiliary variable.  */
2270
 
2271
static tree
2272
gfc_trans_deferred_vars (gfc_symbol * proc_sym, tree fnbody)
2273
{
2274
  locus loc;
2275
  gfc_symbol *sym;
2276
 
2277
  /* Deal with implicit return variables.  Explicit return variables will
2278
     already have been added.  */
2279
  if (gfc_return_by_reference (proc_sym) && proc_sym->result == proc_sym)
2280
    {
2281
      if (!current_fake_result_decl)
2282
        {
2283
          gfc_entry_list *el = NULL;
2284
          if (proc_sym->attr.entry_master)
2285
            {
2286
              for (el = proc_sym->ns->entries; el; el = el->next)
2287
                if (el->sym != el->sym->result)
2288
                  break;
2289
            }
2290
          if (el == NULL)
2291
            warning (0, "Function does not return a value");
2292
        }
2293
      else if (proc_sym->as)
2294
        {
2295
          fnbody = gfc_trans_dummy_array_bias (proc_sym,
2296
                                               current_fake_result_decl,
2297
                                               fnbody);
2298
 
2299
          /* An automatic character length, pointer array result.  */
2300
          if (proc_sym->ts.type == BT_CHARACTER
2301
                && TREE_CODE (proc_sym->ts.cl->backend_decl) == VAR_DECL)
2302
            fnbody = gfc_trans_dummy_character (proc_sym->ts.cl, fnbody);
2303
        }
2304
      else if (proc_sym->ts.type == BT_CHARACTER)
2305
        {
2306
          if (TREE_CODE (proc_sym->ts.cl->backend_decl) == VAR_DECL)
2307
            fnbody = gfc_trans_dummy_character (proc_sym->ts.cl, fnbody);
2308
        }
2309
      else
2310
        gcc_assert (gfc_option.flag_f2c
2311
                    && proc_sym->ts.type == BT_COMPLEX);
2312
    }
2313
 
2314
  for (sym = proc_sym->tlink; sym != proc_sym; sym = sym->tlink)
2315
    {
2316
      if (sym->attr.dimension)
2317
        {
2318
          switch (sym->as->type)
2319
            {
2320
            case AS_EXPLICIT:
2321
              if (sym->attr.dummy || sym->attr.result)
2322
                fnbody =
2323
                  gfc_trans_dummy_array_bias (sym, sym->backend_decl, fnbody);
2324
              else if (sym->attr.pointer || sym->attr.allocatable)
2325
                {
2326
                  if (TREE_STATIC (sym->backend_decl))
2327
                    gfc_trans_static_array_pointer (sym);
2328
                  else
2329
                    fnbody = gfc_trans_deferred_array (sym, fnbody);
2330
                }
2331
              else
2332
                {
2333
                  gfc_get_backend_locus (&loc);
2334
                  gfc_set_backend_locus (&sym->declared_at);
2335
                  fnbody = gfc_trans_auto_array_allocation (sym->backend_decl,
2336
                      sym, fnbody);
2337
                  gfc_set_backend_locus (&loc);
2338
                }
2339
              break;
2340
 
2341
            case AS_ASSUMED_SIZE:
2342
              /* Must be a dummy parameter.  */
2343
              gcc_assert (sym->attr.dummy);
2344
 
2345
              /* We should always pass assumed size arrays the g77 way.  */
2346
              fnbody = gfc_trans_g77_array (sym, fnbody);
2347
              break;
2348
 
2349
            case AS_ASSUMED_SHAPE:
2350
              /* Must be a dummy parameter.  */
2351
              gcc_assert (sym->attr.dummy);
2352
 
2353
              fnbody = gfc_trans_dummy_array_bias (sym, sym->backend_decl,
2354
                                                   fnbody);
2355
              break;
2356
 
2357
            case AS_DEFERRED:
2358
              fnbody = gfc_trans_deferred_array (sym, fnbody);
2359
              break;
2360
 
2361
            default:
2362
              gcc_unreachable ();
2363
            }
2364
        }
2365
      else if (sym->ts.type == BT_CHARACTER)
2366
        {
2367
          gfc_get_backend_locus (&loc);
2368
          gfc_set_backend_locus (&sym->declared_at);
2369
          if (sym->attr.dummy || sym->attr.result)
2370
            fnbody = gfc_trans_dummy_character (sym->ts.cl, fnbody);
2371
          else
2372
            fnbody = gfc_trans_auto_character_variable (sym, fnbody);
2373
          gfc_set_backend_locus (&loc);
2374
        }
2375
      else if (sym->attr.assign)
2376
        {
2377
          gfc_get_backend_locus (&loc);
2378
          gfc_set_backend_locus (&sym->declared_at);
2379
          fnbody = gfc_trans_assign_aux_var (sym, fnbody);
2380
          gfc_set_backend_locus (&loc);
2381
        }
2382
      else
2383
        gcc_unreachable ();
2384
    }
2385
 
2386
  return fnbody;
2387
}
2388
 
2389
 
2390
/* Output an initialized decl for a module variable.  */
2391
 
2392
static void
2393
gfc_create_module_variable (gfc_symbol * sym)
2394
{
2395
  tree decl;
2396
 
2397
  /* Only output symbols from this module.  */
2398
  if (sym->ns != module_namespace)
2399
    {
2400
      /* I don't think this should ever happen.  */
2401
      internal_error ("module symbol %s in wrong namespace", sym->name);
2402
    }
2403
 
2404
  /* Only output variables and array valued parameters.  */
2405
  if (sym->attr.flavor != FL_VARIABLE
2406
      && (sym->attr.flavor != FL_PARAMETER || sym->attr.dimension == 0))
2407
    return;
2408
 
2409
  /* Don't generate variables from other modules. Variables from
2410
     COMMONs will already have been generated.  */
2411
  if (sym->attr.use_assoc || sym->attr.in_common)
2412
    return;
2413
 
2414
  /* Equivalenced variables arrive here after creation.  */
2415
  if (sym->backend_decl
2416
        && (sym->equiv_built || sym->attr.in_equivalence))
2417
      return;
2418
 
2419
  if (sym->backend_decl)
2420
    internal_error ("backend decl for module variable %s already exists",
2421
                    sym->name);
2422
 
2423
  /* We always want module variables to be created.  */
2424
  sym->attr.referenced = 1;
2425
  /* Create the decl.  */
2426
  decl = gfc_get_symbol_decl (sym);
2427
 
2428
  /* Create the variable.  */
2429
  pushdecl (decl);
2430
  rest_of_decl_compilation (decl, 1, 0);
2431
 
2432
  /* Also add length of strings.  */
2433
  if (sym->ts.type == BT_CHARACTER)
2434
    {
2435
      tree length;
2436
 
2437
      length = sym->ts.cl->backend_decl;
2438
      if (!INTEGER_CST_P (length))
2439
        {
2440
          pushdecl (length);
2441
          rest_of_decl_compilation (length, 1, 0);
2442
        }
2443
    }
2444
}
2445
 
2446
 
2447
/* Generate all the required code for module variables.  */
2448
 
2449
void
2450
gfc_generate_module_vars (gfc_namespace * ns)
2451
{
2452
  module_namespace = ns;
2453
 
2454
  /* Check if the frontend left the namespace in a reasonable state.  */
2455
  gcc_assert (ns->proc_name && !ns->proc_name->tlink);
2456
 
2457
  /* Generate COMMON blocks.  */
2458
  gfc_trans_common (ns);
2459
 
2460
  /* Create decls for all the module variables.  */
2461
  gfc_traverse_ns (ns, gfc_create_module_variable);
2462
}
2463
 
2464
static void
2465
gfc_generate_contained_functions (gfc_namespace * parent)
2466
{
2467
  gfc_namespace *ns;
2468
 
2469
  /* We create all the prototypes before generating any code.  */
2470
  for (ns = parent->contained; ns; ns = ns->sibling)
2471
    {
2472
      /* Skip namespaces from used modules.  */
2473
      if (ns->parent != parent)
2474
        continue;
2475
 
2476
      gfc_create_function_decl (ns);
2477
    }
2478
 
2479
  for (ns = parent->contained; ns; ns = ns->sibling)
2480
    {
2481
      /* Skip namespaces from used modules.  */
2482
      if (ns->parent != parent)
2483
        continue;
2484
 
2485
      gfc_generate_function_code (ns);
2486
    }
2487
}
2488
 
2489
 
2490
/* Generate decls for all local variables.  We do this to ensure correct
2491
   handling of expressions which only appear in the specification of
2492
   other functions.  */
2493
 
2494
static void
2495
generate_local_decl (gfc_symbol * sym)
2496
{
2497
  if (sym->attr.flavor == FL_VARIABLE)
2498
    {
2499
      if (sym->attr.referenced)
2500
        gfc_get_symbol_decl (sym);
2501
      else if (sym->attr.dummy && warn_unused_parameter)
2502
            warning (0, "unused parameter %qs", sym->name);
2503
      /* Warn for unused variables, but not if they're inside a common
2504
         block or are use-associated.  */
2505
      else if (warn_unused_variable
2506
               && !(sym->attr.in_common || sym->attr.use_assoc))
2507
        warning (0, "unused variable %qs", sym->name);
2508
    }
2509
}
2510
 
2511
static void
2512
generate_local_vars (gfc_namespace * ns)
2513
{
2514
  gfc_traverse_ns (ns, generate_local_decl);
2515
}
2516
 
2517
 
2518
/* Generate a switch statement to jump to the correct entry point.  Also
2519
   creates the label decls for the entry points.  */
2520
 
2521
static tree
2522
gfc_trans_entry_master_switch (gfc_entry_list * el)
2523
{
2524
  stmtblock_t block;
2525
  tree label;
2526
  tree tmp;
2527
  tree val;
2528
 
2529
  gfc_init_block (&block);
2530
  for (; el; el = el->next)
2531
    {
2532
      /* Add the case label.  */
2533
      label = gfc_build_label_decl (NULL_TREE);
2534
      val = build_int_cst (gfc_array_index_type, el->id);
2535
      tmp = build3_v (CASE_LABEL_EXPR, val, NULL_TREE, label);
2536
      gfc_add_expr_to_block (&block, tmp);
2537
 
2538
      /* And jump to the actual entry point.  */
2539
      label = gfc_build_label_decl (NULL_TREE);
2540
      tmp = build1_v (GOTO_EXPR, label);
2541
      gfc_add_expr_to_block (&block, tmp);
2542
 
2543
      /* Save the label decl.  */
2544
      el->label = label;
2545
    }
2546
  tmp = gfc_finish_block (&block);
2547
  /* The first argument selects the entry point.  */
2548
  val = DECL_ARGUMENTS (current_function_decl);
2549
  tmp = build3_v (SWITCH_EXPR, val, tmp, NULL_TREE);
2550
  return tmp;
2551
}
2552
 
2553
 
2554
/* Generate code for a function.  */
2555
 
2556
void
2557
gfc_generate_function_code (gfc_namespace * ns)
2558
{
2559
  tree fndecl;
2560
  tree old_context;
2561
  tree decl;
2562
  tree tmp;
2563
  stmtblock_t block;
2564
  stmtblock_t body;
2565
  tree result;
2566
  gfc_symbol *sym;
2567
 
2568
  sym = ns->proc_name;
2569
 
2570
  /* Check that the frontend isn't still using this.  */
2571
  gcc_assert (sym->tlink == NULL);
2572
  sym->tlink = sym;
2573
 
2574
  /* Create the declaration for functions with global scope.  */
2575
  if (!sym->backend_decl)
2576
    gfc_create_function_decl (ns);
2577
 
2578
  fndecl = sym->backend_decl;
2579
  old_context = current_function_decl;
2580
 
2581
  if (old_context)
2582
    {
2583
      push_function_context ();
2584
      saved_parent_function_decls = saved_function_decls;
2585
      saved_function_decls = NULL_TREE;
2586
    }
2587
 
2588
  trans_function_start (sym);
2589
 
2590
  gfc_start_block (&block);
2591
 
2592
  if (ns->entries && ns->proc_name->ts.type == BT_CHARACTER)
2593
    {
2594
      /* Copy length backend_decls to all entry point result
2595
         symbols.  */
2596
      gfc_entry_list *el;
2597
      tree backend_decl;
2598
 
2599
      gfc_conv_const_charlen (ns->proc_name->ts.cl);
2600
      backend_decl = ns->proc_name->result->ts.cl->backend_decl;
2601
      for (el = ns->entries; el; el = el->next)
2602
        el->sym->result->ts.cl->backend_decl = backend_decl;
2603
    }
2604
 
2605
  /* Translate COMMON blocks.  */
2606
  gfc_trans_common (ns);
2607
 
2608
  gfc_generate_contained_functions (ns);
2609
 
2610
  generate_local_vars (ns);
2611
 
2612
  /* Will be created as needed.  */
2613
  current_fake_result_decl = NULL_TREE;
2614
  current_function_return_label = NULL;
2615
 
2616
  /* Now generate the code for the body of this function.  */
2617
  gfc_init_block (&body);
2618
 
2619
  /* If this is the main program, add a call to set_std to set up the
2620
     runtime library Fortran language standard parameters.  */
2621
 
2622
  if (sym->attr.is_main_program)
2623
    {
2624
      tree arglist, gfc_int4_type_node;
2625
 
2626
      gfc_int4_type_node = gfc_get_int_type (4);
2627
      arglist = gfc_chainon_list (NULL_TREE,
2628
                                  build_int_cst (gfc_int4_type_node,
2629
                                                 gfc_option.warn_std));
2630
      arglist = gfc_chainon_list (arglist,
2631
                                  build_int_cst (gfc_int4_type_node,
2632
                                                 gfc_option.allow_std));
2633
      arglist = gfc_chainon_list (arglist,
2634
                                  build_int_cst (gfc_int4_type_node,
2635
                                                 pedantic));
2636
      tmp = build_function_call_expr (gfor_fndecl_set_std, arglist);
2637
      gfc_add_expr_to_block (&body, tmp);
2638
    }
2639
 
2640
  /* If this is the main program and a -ffpe-trap option was provided,
2641
     add a call to set_fpe so that the library will raise a FPE when
2642
     needed.  */
2643
  if (sym->attr.is_main_program && gfc_option.fpe != 0)
2644
    {
2645
      tree arglist, gfc_c_int_type_node;
2646
 
2647
      gfc_c_int_type_node = gfc_get_int_type (gfc_c_int_kind);
2648
      arglist = gfc_chainon_list (NULL_TREE,
2649
                                  build_int_cst (gfc_c_int_type_node,
2650
                                                 gfc_option.fpe));
2651
      tmp = gfc_build_function_call (gfor_fndecl_set_fpe, arglist);
2652
      gfc_add_expr_to_block (&body, tmp);
2653
    }
2654
 
2655
  /* If this is the main program and an -fconvert option was provided,
2656
     add a call to set_convert.  */
2657
 
2658
  if (sym->attr.is_main_program && gfc_option.convert != CONVERT_NATIVE)
2659
    {
2660
      tree arglist, gfc_c_int_type_node;
2661
 
2662
      gfc_c_int_type_node = gfc_get_int_type (gfc_c_int_kind);
2663
      arglist = gfc_chainon_list (NULL_TREE,
2664
                                  build_int_cst (gfc_c_int_type_node,
2665
                                                 gfc_option.convert));
2666
      tmp = build_function_call_expr (gfor_fndecl_set_convert, arglist);
2667
      gfc_add_expr_to_block (&body, tmp);
2668
    }
2669
 
2670
  /* If this is the main program and an -frecord-marker option was provided,
2671
     add a call to set_record_marker.  */
2672
 
2673
  if (sym->attr.is_main_program && gfc_option.record_marker != 0)
2674
    {
2675
      tree arglist, gfc_c_int_type_node;
2676
 
2677
      gfc_c_int_type_node = gfc_get_int_type (gfc_c_int_kind);
2678
      arglist = gfc_chainon_list (NULL_TREE,
2679
                                  build_int_cst (gfc_c_int_type_node,
2680
                                                 gfc_option.record_marker));
2681
      tmp = build_function_call_expr (gfor_fndecl_set_record_marker, arglist);
2682
      gfc_add_expr_to_block (&body, tmp);
2683
 
2684
    }
2685
 
2686
  if (TREE_TYPE (DECL_RESULT (fndecl)) != void_type_node
2687
      && sym->attr.subroutine)
2688
    {
2689
      tree alternate_return;
2690
      alternate_return = gfc_get_fake_result_decl (sym);
2691
      gfc_add_modify_expr (&body, alternate_return, integer_zero_node);
2692
    }
2693
 
2694
  if (ns->entries)
2695
    {
2696
      /* Jump to the correct entry point.  */
2697
      tmp = gfc_trans_entry_master_switch (ns->entries);
2698
      gfc_add_expr_to_block (&body, tmp);
2699
    }
2700
 
2701
  tmp = gfc_trans_code (ns->code);
2702
  gfc_add_expr_to_block (&body, tmp);
2703
 
2704
  /* Add a return label if needed.  */
2705
  if (current_function_return_label)
2706
    {
2707
      tmp = build1_v (LABEL_EXPR, current_function_return_label);
2708
      gfc_add_expr_to_block (&body, tmp);
2709
    }
2710
 
2711
  tmp = gfc_finish_block (&body);
2712
  /* Add code to create and cleanup arrays.  */
2713
  tmp = gfc_trans_deferred_vars (sym, tmp);
2714
  gfc_add_expr_to_block (&block, tmp);
2715
 
2716
  if (TREE_TYPE (DECL_RESULT (fndecl)) != void_type_node)
2717
    {
2718
      if (sym->attr.subroutine || sym == sym->result)
2719
        {
2720
          result = current_fake_result_decl;
2721
          current_fake_result_decl = NULL_TREE;
2722
        }
2723
      else
2724
        result = sym->result->backend_decl;
2725
 
2726
      if (result == NULL_TREE)
2727
        warning (0, "Function return value not set");
2728
      else
2729
        {
2730
          /* Set the return value to the dummy result variable.  */
2731
          tmp = build2 (MODIFY_EXPR, TREE_TYPE (result),
2732
                        DECL_RESULT (fndecl), result);
2733
          tmp = build1_v (RETURN_EXPR, tmp);
2734
          gfc_add_expr_to_block (&block, tmp);
2735
        }
2736
    }
2737
 
2738
  /* Add all the decls we created during processing.  */
2739
  decl = saved_function_decls;
2740
  while (decl)
2741
    {
2742
      tree next;
2743
 
2744
      next = TREE_CHAIN (decl);
2745
      TREE_CHAIN (decl) = NULL_TREE;
2746
      pushdecl (decl);
2747
      decl = next;
2748
    }
2749
  saved_function_decls = NULL_TREE;
2750
 
2751
  DECL_SAVED_TREE (fndecl) = gfc_finish_block (&block);
2752
 
2753
  /* Finish off this function and send it for code generation.  */
2754
  poplevel (1, 0, 1);
2755
  BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
2756
 
2757
  /* Output the GENERIC tree.  */
2758
  dump_function (TDI_original, fndecl);
2759
 
2760
  /* Store the end of the function, so that we get good line number
2761
     info for the epilogue.  */
2762
  cfun->function_end_locus = input_location;
2763
 
2764
  /* We're leaving the context of this function, so zap cfun.
2765
     It's still in DECL_STRUCT_FUNCTION, and we'll restore it in
2766
     tree_rest_of_compilation.  */
2767
  cfun = NULL;
2768
 
2769
  if (old_context)
2770
    {
2771
      pop_function_context ();
2772
      saved_function_decls = saved_parent_function_decls;
2773
    }
2774
  current_function_decl = old_context;
2775
 
2776
  if (decl_function_context (fndecl))
2777
    /* Register this function with cgraph just far enough to get it
2778
       added to our parent's nested function list.  */
2779
    (void) cgraph_node (fndecl);
2780
  else
2781
    {
2782
      gfc_gimplify_function (fndecl);
2783
      cgraph_finalize_function (fndecl, false);
2784
    }
2785
}
2786
 
2787
void
2788
gfc_generate_constructors (void)
2789
{
2790
  gcc_assert (gfc_static_ctors == NULL_TREE);
2791
#if 0
2792
  tree fnname;
2793
  tree type;
2794
  tree fndecl;
2795
  tree decl;
2796
  tree tmp;
2797
 
2798
  if (gfc_static_ctors == NULL_TREE)
2799
    return;
2800
 
2801
  fnname = get_file_function_name ('I');
2802
  type = build_function_type (void_type_node,
2803
                              gfc_chainon_list (NULL_TREE, void_type_node));
2804
 
2805
  fndecl = build_decl (FUNCTION_DECL, fnname, type);
2806
  TREE_PUBLIC (fndecl) = 1;
2807
 
2808
  decl = build_decl (RESULT_DECL, NULL_TREE, void_type_node);
2809
  DECL_ARTIFICIAL (decl) = 1;
2810
  DECL_IGNORED_P (decl) = 1;
2811
  DECL_CONTEXT (decl) = fndecl;
2812
  DECL_RESULT (fndecl) = decl;
2813
 
2814
  pushdecl (fndecl);
2815
 
2816
  current_function_decl = fndecl;
2817
 
2818
  rest_of_decl_compilation (fndecl, 1, 0);
2819
 
2820
  make_decl_rtl (fndecl);
2821
 
2822
  init_function_start (fndecl);
2823
 
2824
  pushlevel (0);
2825
 
2826
  for (; gfc_static_ctors; gfc_static_ctors = TREE_CHAIN (gfc_static_ctors))
2827
    {
2828
      tmp =
2829
        gfc_build_function_call (TREE_VALUE (gfc_static_ctors), NULL_TREE);
2830
      DECL_SAVED_TREE (fndecl) = build_stmt (EXPR_STMT, tmp);
2831
    }
2832
 
2833
  poplevel (1, 0, 1);
2834
 
2835
  BLOCK_SUPERCONTEXT (DECL_INITIAL (fndecl)) = fndecl;
2836
 
2837
  free_after_parsing (cfun);
2838
  free_after_compilation (cfun);
2839
 
2840
  tree_rest_of_compilation (fndecl);
2841
 
2842
  current_function_decl = NULL_TREE;
2843
#endif
2844
}
2845
 
2846
/* Translates a BLOCK DATA program unit. This means emitting the
2847
   commons contained therein plus their initializations. We also emit
2848
   a globally visible symbol to make sure that each BLOCK DATA program
2849
   unit remains unique.  */
2850
 
2851
void
2852
gfc_generate_block_data (gfc_namespace * ns)
2853
{
2854
  tree decl;
2855
  tree id;
2856
 
2857
  /* Tell the backend the source location of the block data.  */
2858
  if (ns->proc_name)
2859
    gfc_set_backend_locus (&ns->proc_name->declared_at);
2860
  else
2861
    gfc_set_backend_locus (&gfc_current_locus);
2862
 
2863
  /* Process the DATA statements.  */
2864
  gfc_trans_common (ns);
2865
 
2866
  /* Create a global symbol with the mane of the block data.  This is to
2867
     generate linker errors if the same name is used twice.  It is never
2868
     really used.  */
2869
  if (ns->proc_name)
2870
    id = gfc_sym_mangled_function_id (ns->proc_name);
2871
  else
2872
    id = get_identifier ("__BLOCK_DATA__");
2873
 
2874
  decl = build_decl (VAR_DECL, id, gfc_array_index_type);
2875
  TREE_PUBLIC (decl) = 1;
2876
  TREE_STATIC (decl) = 1;
2877
 
2878
  pushdecl (decl);
2879
  rest_of_decl_compilation (decl, 1, 0);
2880
}
2881
 
2882
 
2883
#include "gt-fortran-trans-decl.h"

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.