home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / f2csrc.zip / f2csrc / src / proc.c < prev    next >
C/C++ Source or Header  |  1994-07-05  |  39KB  |  1,765 lines

  1. /****************************************************************
  2. Copyright 1990, 1994 by AT&T Bell Laboratories and Bellcore.
  3.  
  4. Permission to use, copy, modify, and distribute this software
  5. and its documentation for any purpose and without fee is hereby
  6. granted, provided that the above copyright notice appear in all
  7. copies and that both that the copyright notice and this
  8. permission notice and warranty disclaimer appear in supporting
  9. documentation, and that the names of AT&T Bell Laboratories or
  10. Bellcore or any of their entities not be used in advertising or
  11. publicity pertaining to distribution of the software without
  12. specific, written prior permission.
  13.  
  14. AT&T and Bellcore disclaim all warranties with regard to this
  15. software, including all implied warranties of merchantability
  16. and fitness.  In no event shall AT&T or Bellcore be liable for
  17. any special, indirect or consequential damages or any damages
  18. whatsoever resulting from loss of use, data or profits, whether
  19. in an action of contract, negligence or other tortious action,
  20. arising out of or in connection with the use or performance of
  21. this software.
  22. ****************************************************************/
  23.  
  24. #include "defs.h"
  25. #include "names.h"
  26. #include "output.h"
  27. #include "p1defs.h"
  28.  
  29. #define EXNULL (union Expression *)0
  30.  
  31. static void dobss Argdcl((void));
  32. static void docomleng Argdcl((void));
  33. static void docommon Argdcl((void));
  34. static void doentry Argdcl((struct Entrypoint*));
  35. static void epicode Argdcl((void));
  36. static int nextarg Argdcl((int));
  37. static void retval Argdcl((int));
  38.  
  39. static char Blank[] = BLANKCOMMON;
  40.  
  41.  static char *postfix[] = { "g", "h", "i",
  42. #ifdef TYQUAD
  43.                     "j",
  44. #endif
  45.                     "r", "d", "c", "z", "g", "h", "i" };
  46.  
  47.  chainp new_procs;
  48.  int prev_proc, proc_argchanges, proc_protochanges;
  49.  
  50.  void
  51. #ifdef KR_headers
  52. changedtype(q)
  53.     Namep q;
  54. #else
  55. changedtype(Namep q)
  56. #endif
  57. {
  58.     char buf[200];
  59.     int qtype, type1;
  60.     register Extsym *e;
  61.     Argtypes *at;
  62.  
  63.     if (q->vtypewarned)
  64.         return;
  65.     q->vtypewarned = 1;
  66.     qtype = q->vtype;
  67.     e = &extsymtab[q->vardesc.varno];
  68.     if (!(at = e->arginfo)) {
  69.         if (!e->exused)
  70.             return;
  71.         }
  72.     else if (at->changes & 2 && qtype != TYUNKNOWN && !at->defined)
  73.         proc_protochanges++;
  74.     type1 = e->extype;
  75.     if (type1 == TYUNKNOWN)
  76.         return;
  77.     if (qtype == TYUNKNOWN)
  78.         /* e.g.,
  79.             subroutine foo
  80.             end
  81.             external foo
  82.             call goo(foo)
  83.             end
  84.         */
  85.         return;
  86.     sprintf(buf, "%.90s: inconsistent declarations:\n\
  87.     here %s%s, previously %s%s.", q->fvarname, ftn_types[qtype],
  88.         qtype == TYSUBR ? "" : " function",
  89.         ftn_types[type1], type1 == TYSUBR ? "" : " function");
  90.     warn(buf);
  91.     }
  92.  
  93.  void
  94. #ifdef KR_headers
  95. unamstring(q, s)
  96.     register Addrp q;
  97.     register char *s;
  98. #else
  99. unamstring(register Addrp q, register char *s)
  100. #endif
  101. {
  102.     register int k;
  103.     register char *t;
  104.  
  105.     k = strlen(s);
  106.     if (k < IDENT_LEN) {
  107.         q->uname_tag = UNAM_IDENT;
  108.         t = q->user.ident;
  109.         }
  110.     else {
  111.         q->uname_tag = UNAM_CHARP;
  112.         q->user.Charp = t = mem(k+1, 0);
  113.         }
  114.     strcpy(t, s);
  115.     }
  116.  
  117.  static void
  118. fix_entry_returns(Void)    /* for multiple entry points */
  119. {
  120.     Addrp a;
  121.     int i;
  122.     struct Entrypoint *e;
  123.     Namep np;
  124.  
  125.     e = entries = (struct Entrypoint *)revchain((chainp)entries);
  126.     allargs = revchain(allargs);
  127.     if (!multitype)
  128.         return;
  129.  
  130.     /* TYLOGICAL should have been turned into TYLONG or TYSHORT by now */
  131.  
  132.     for(i = TYINT1; i <= TYLOGICAL; i++)
  133.         if (a = xretslot[i])
  134.             sprintf(a->user.ident, "(*ret_val).%s",
  135.                 postfix[i-TYINT1]);
  136.  
  137.     do {
  138.         np = e->enamep;
  139.         switch(np->vtype) {
  140.             case TYINT1:
  141.             case TYSHORT:
  142.             case TYLONG:
  143. #ifdef TYQUAD
  144.             case TYQUAD:
  145. #endif
  146.             case TYREAL:
  147.             case TYDREAL:
  148.             case TYCOMPLEX:
  149.             case TYDCOMPLEX:
  150.             case TYLOGICAL1:
  151.             case TYLOGICAL2:
  152.             case TYLOGICAL:
  153.                 np->vstg = STGARG;
  154.             }
  155.         }
  156.         while(e = e->entnextp);
  157.     }
  158.  
  159.  static void
  160. #ifdef KR_headers
  161. putentries(outfile)
  162.     FILE *outfile;
  163. #else
  164. putentries(FILE *outfile)
  165. #endif
  166.     /* put out wrappers for multiple entries */
  167. {
  168.     char base[IDENT_LEN];
  169.     struct Entrypoint *e;
  170.     Namep *A, *Ae, *Ae1, **Alp, *a, **a1, np;
  171.     chainp args, lengths;
  172.     int i, k, mt, nL, t, type;
  173.     extern char *dfltarg[], **dfltproc;
  174.  
  175.     e = entries;
  176.     if (!e->enamep) /* only possible with erroneous input */
  177.         return;
  178.     nL = (nallargs + nallchargs) * sizeof(Namep *);
  179.     A = (Namep *)ckalloc(nL + nallargs*sizeof(Namep **));
  180.     Ae = A + nallargs;
  181.     Alp = (Namep **)(Ae1 = Ae + nallchargs);
  182.     i = k = 0;
  183.     for(a1 = Alp, args = allargs; args; a1++, args = args->nextp) {
  184.         np = (Namep)args->datap;
  185.         if (np->vtype == TYCHAR && np->vclass != CLPROC)
  186.             *a1 = &Ae[i++];
  187.         }
  188.  
  189.     mt = multitype;
  190.     multitype = 0;
  191.     sprintf(base, "%s0_", e->enamep->cvarname);
  192.     do {
  193.         np = e->enamep;
  194.         lengths = length_comp(e, 0);
  195.         proctype = type = np->vtype;
  196.         if (protofile)
  197.             protowrite(protofile, type, np->cvarname, e, lengths);
  198.         nice_printf(outfile, "\n%s ", c_type_decl(type, 1));
  199.         nice_printf(outfile, "%s", np->cvarname);
  200.         if (!Ansi) {
  201.             listargs(outfile, e, 0, lengths);
  202.             nice_printf(outfile, "\n");
  203.             }
  204.             list_arg_types(outfile, e, lengths, 0, "\n");
  205.         nice_printf(outfile, "{\n");
  206.         frchain(&lengths);
  207.         next_tab(outfile);
  208.         if (mt)
  209.             nice_printf(outfile,
  210.                 "Multitype ret_val;\n%s(%d, &ret_val",
  211.                 base, k); /*)*/
  212.         else if (ISCOMPLEX(type))
  213.             nice_printf(outfile, "%s(%d,%s", base, k,
  214.                 xretslot[type]->user.ident); /*)*/
  215.         else if (type == TYCHAR)
  216.             nice_printf(outfile,
  217.                 "%s(%d, ret_val, ret_val_len", base, k); /*)*/
  218.         else
  219.             nice_printf(outfile, "return %s(%d", base, k); /*)*/
  220.         k++;
  221.         memset((char *)A, 0, nL);
  222.         for(args = e->arglist; args; args = args->nextp) {
  223.             np = (Namep)args->datap;
  224.             A[np->argno] = np;
  225.             if (np->vtype == TYCHAR && np->vclass != CLPROC)
  226.                 *Alp[np->argno] = np;
  227.             }
  228.         args = allargs;
  229.         for(a = A; a < Ae; a++, args = args->nextp) {
  230.             t = ((Namep)args->datap)->vtype;
  231.             nice_printf(outfile, ", %s", (np = *a)
  232.                 ? np->cvarname
  233.                 : ((Namep)args->datap)->vclass == CLPROC
  234.                 ? dfltproc[((Namep)args->datap)->vimpltype
  235.                     ? (Castargs ? TYUNKNOWN : TYSUBR)
  236.                     : t == TYREAL && forcedouble && !Castargs
  237.                     ? TYDREAL : t]
  238.                 : dfltarg[((Namep)args->datap)->vtype]);
  239.             }
  240.         for(; a < Ae1; a++)
  241.             if (np = *a)
  242.                 nice_printf(outfile, ", %s_len", np->fvarname);
  243.             else
  244.                 nice_printf(outfile, ", (ftnint)0");
  245.         nice_printf(outfile, /*(*/ ");\n");
  246.         if (mt) {
  247.             if (type == TYCOMPLEX)
  248.                 nice_printf(outfile,
  249.             "r_v->r = ret_val.c.r; r_v->i = ret_val.c.i;\n");
  250.             else if (type == TYDCOMPLEX)
  251.                 nice_printf(outfile,
  252.             "r_v->r = ret_val.z.r; r_v->i = ret_val.z.i;\n");
  253.             else if (type <= TYLOGICAL)
  254.                 nice_printf(outfile, "return ret_val.%s;\n",
  255.                     postfix[type-TYINT1]);
  256.             }
  257.         nice_printf(outfile, "}\n");
  258.         prev_tab(outfile);
  259.         }
  260.         while(e = e->entnextp);
  261.     free((char *)A);
  262.     }
  263.  
  264.  static void
  265. #ifdef KR_headers
  266. entry_goto(outfile)
  267.     FILE *outfile;
  268. #else
  269. entry_goto(FILE *outfile)
  270. #endif
  271. {
  272.     struct Entrypoint *e = entries;
  273.     int k = 0;
  274.  
  275.     nice_printf(outfile, "switch(n__) {\n");
  276.     next_tab(outfile);
  277.     while(e = e->entnextp)
  278.         nice_printf(outfile, "case %d: goto %s;\n", ++k,
  279.             user_label((long)(extsymtab - e->entryname - 1)));
  280.     nice_printf(outfile, "}\n\n");
  281.     prev_tab(outfile);
  282.     }
  283.  
  284. /* start a new procedure */
  285.  
  286.  void
  287. newproc(Void)
  288. {
  289.     if(parstate != OUTSIDE)
  290.     {
  291.         execerr("missing end statement", CNULL);
  292.         endproc();
  293.     }
  294.  
  295.     parstate = INSIDE;
  296.     procclass = CLMAIN;    /* default */
  297. }
  298.  
  299.  static void
  300. zap_changes(Void)
  301. {
  302.     register chainp cp;
  303.     register Argtypes *at;
  304.  
  305.     /* arrange to get correct count of prototypes that would
  306.        change by running f2c again */
  307.  
  308.     if (prev_proc && proc_argchanges)
  309.         proc_protochanges++;
  310.     prev_proc = proc_argchanges = 0;
  311.     for(cp = new_procs; cp; cp = cp->nextp)
  312.         if (at = ((Namep)cp->datap)->arginfo)
  313.             at->changes &= ~1;
  314.     frchain(&new_procs);
  315.     }
  316.  
  317. /* end of procedure. generate variables, epilogs, and prologs */
  318.  
  319.  void
  320. endproc(Void)
  321. {
  322.     struct Labelblock *lp;
  323.     Extsym *ext;
  324.  
  325.     if(parstate < INDATA)
  326.         enddcl();
  327.     if(ctlstack >= ctls)
  328.         err("DO loop or BLOCK IF not closed");
  329.     for(lp = labeltab ; lp < labtabend ; ++lp)
  330.         if(lp->stateno!=0 && lp->labdefined==NO)
  331.             errstr("missing statement label %s",
  332.                 convic(lp->stateno) );
  333.  
  334. /* Save copies of the common variables in extptr -> allextp */
  335.  
  336.     for (ext = extsymtab; ext < nextext; ext++)
  337.         if (ext -> extstg == STGCOMMON && ext -> extp) {
  338.             extern int usedefsforcommon;
  339.  
  340. /* Write out the abbreviations for common block reference */
  341.  
  342.             copy_data (ext -> extp);
  343.             if (usedefsforcommon) {
  344.                 wr_abbrevs (c_file, 1, ext -> extp);
  345.                 ext -> used_here = 1;
  346.                 }
  347.             else
  348.                 ext -> extp = CHNULL;
  349.  
  350.             }
  351.  
  352.     if (nentry > 1)
  353.         fix_entry_returns();
  354.     epicode();
  355.     donmlist();
  356.     dobss();
  357.     start_formatting ();
  358.     if (nentry > 1)
  359.         putentries(c_file);
  360.  
  361.     zap_changes();
  362.     procinit();    /* clean up for next procedure */
  363. }
  364.  
  365.  
  366.  
  367. /* End of declaration section of procedure.  Allocate storage. */
  368.  
  369.  void
  370. enddcl(Void)
  371. {
  372.     register struct Entrypoint *ep;
  373.     struct Entrypoint *ep0;
  374.     chainp cp;
  375.     extern char *err_proc;
  376.     static char comblks[] = "common blocks";
  377.  
  378.     err_proc = comblks;
  379.     docommon();
  380.  
  381. /* Now the hash table entries for fields of common blocks have STGCOMMON,
  382.    vdcldone, voffset, and varno.  And the common blocks themselves have
  383.    their full sizes in extleng. */
  384.  
  385.     err_proc = "equivalences";
  386.     doequiv();
  387.  
  388.     err_proc = comblks;
  389.     docomleng();
  390.  
  391. /* This implies that entry points in the declarations are buffered in
  392.    entries   but not written out */
  393.  
  394.     err_proc = "entries";
  395.     if (ep = ep0 = (struct Entrypoint *)revchain((chainp)entries)) {
  396.         /* entries could be 0 in case of an error */
  397.         do doentry(ep);
  398.             while(ep = ep->entnextp);
  399.         entries = (struct Entrypoint *)revchain((chainp)ep0);
  400.         }
  401.  
  402.     err_proc = 0;
  403.     parstate = INEXEC;
  404.     p1put(P1_PROCODE);
  405.     freetemps();
  406.     if (earlylabs) {
  407.         for(cp = earlylabs = revchain(earlylabs); cp; cp = cp->nextp)
  408.             p1_label((long)cp->datap);
  409.         frchain(&earlylabs);
  410.         }
  411.     p1_line_number(lineno); /* for files that start with a MAIN program */
  412.                 /* that starts with an executable statement */
  413. }
  414.  
  415. /* ROUTINES CALLED WHEN ENCOUNTERING ENTRY POINTS */
  416.  
  417. /* Main program or Block data */
  418.  
  419.  void
  420. #ifdef KR_headers
  421. startproc(progname, class)
  422.     Extsym *progname;
  423.     int class;
  424. #else
  425. startproc(Extsym *progname, int class)
  426. #endif
  427. {
  428.     register struct Entrypoint *p;
  429.  
  430.     p = ALLOC(Entrypoint);
  431.     if(class == CLMAIN) {
  432.         puthead(CNULL, CLMAIN);
  433.         if (progname)
  434.             strcpy (main_alias, progname->cextname);
  435.     } else
  436.         puthead(CNULL, CLBLOCK);
  437.     if(class == CLMAIN)
  438.         newentry( mkname(" MAIN"), 0 )->extinit = 1;
  439.     p->entryname = progname;
  440.     entries = p;
  441.  
  442.     procclass = class;
  443.     fprintf(diagfile, "   %s", (class==CLMAIN ? "MAIN" : "BLOCK DATA") );
  444.     if(progname) {
  445.         fprintf(diagfile, " %s", progname->fextname);
  446.         procname = progname->cextname;
  447.         }
  448.     fprintf(diagfile, ":\n");
  449.     fflush(diagfile);
  450. }
  451.  
  452. /* subroutine or function statement */
  453.  
  454.  Extsym *
  455. #ifdef KR_headers
  456. newentry(v, substmsg)
  457.     register Namep v;
  458.     int substmsg;
  459. #else
  460. newentry(register Namep v, int substmsg)
  461. #endif
  462. {
  463.     register Extsym *p;
  464.     char buf[128], badname[64];
  465.     static int nbad = 0;
  466.     static char already[] = "external name already used";
  467.  
  468.     p = mkext(v->fvarname, addunder(v->cvarname));
  469.  
  470.     if(p->extinit || ! ONEOF(p->extstg, M(STGUNKNOWN)|M(STGEXT)) )
  471.     {
  472.         sprintf(badname, "%s_bad%d", v->fvarname, ++nbad);
  473.         if (substmsg) {
  474.             sprintf(buf,"%s\n\tsubstituting \"%s\"",
  475.                 already, badname);
  476.             dclerr(buf, v);
  477.             }
  478.         else
  479.             dclerr(already, v);
  480.         p = mkext(v->fvarname, badname);
  481.     }
  482.     v->vstg = STGAUTO;
  483.     v->vprocclass = PTHISPROC;
  484.     v->vclass = CLPROC;
  485.     if (p->extstg == STGEXT)
  486.         prev_proc = 1;
  487.     else
  488.         p->extstg = STGEXT;
  489.     p->extinit = YES;
  490.     v->vardesc.varno = p - extsymtab;
  491.     return(p);
  492. }
  493.  
  494.  void
  495. #ifdef KR_headers
  496. entrypt(class, type, length, entry, args)
  497.     int class;
  498.     int type;
  499.     ftnint length;
  500.     Extsym *entry;
  501.     chainp args;
  502. #else
  503. entrypt(int class, int type, ftnint length, Extsym *entry, chainp args)
  504. #endif
  505. {
  506.     register Namep q;
  507.     register struct Entrypoint *p;
  508.  
  509.     if(class != CLENTRY)
  510.         puthead( procname = entry->cextname, class);
  511.     else
  512.         fprintf(diagfile, "       entry ");
  513.     fprintf(diagfile, "   %s:\n", entry->fextname);
  514.     fflush(diagfile);
  515.     q = mkname(entry->fextname);
  516.     if (type == TYSUBR)
  517.         q->vstg = STGEXT;
  518.  
  519.     type = lengtype(type, length);
  520.     if(class == CLPROC)
  521.     {
  522.         procclass = CLPROC;
  523.         proctype = type;
  524.         procleng = type == TYCHAR ? length : 0;
  525.     }
  526.  
  527.     p = ALLOC(Entrypoint);
  528.  
  529.     p->entnextp = entries;
  530.     entries = p;
  531.  
  532.     p->entryname = entry;
  533.     p->arglist = revchain(args);
  534.     p->enamep = q;
  535.  
  536.     if(class == CLENTRY)
  537.     {
  538.         class = CLPROC;
  539.         if(proctype == TYSUBR)
  540.             type = TYSUBR;
  541.     }
  542.  
  543.     q->vclass = class;
  544.     q->vprocclass = 0;
  545.     settype(q, type, length);
  546.     q->vprocclass = PTHISPROC;
  547.     /* hold all initial entry points till end of declarations */
  548.     if(parstate >= INDATA)
  549.         doentry(p);
  550. }
  551.  
  552. /* generate epilogs */
  553.  
  554. /* epicode -- write out the proper function return mechanism at the end of
  555.    the procedure declaration.  Handles multiple return value types, as
  556.    well as cooercion into the proper value */
  557.  
  558.  LOCAL void
  559. epicode(Void)
  560. {
  561.     extern int lastwasbranch;
  562.  
  563.     if(procclass==CLPROC)
  564.     {
  565.         if(proctype==TYSUBR)
  566.         {
  567.  
  568. /* Return a zero only when the alternate return mechanism has been
  569.    specified in the function header */
  570.  
  571.             if ((substars || Ansi) && lastwasbranch != YES)
  572.                 p1_subr_ret (ICON(0));
  573.         }
  574.         else if (!multitype && lastwasbranch != YES)
  575.             retval(proctype);
  576.     }
  577.     else if (procclass == CLMAIN && Ansi && lastwasbranch != YES)
  578.         p1_subr_ret (ICON(0));
  579.     lastwasbranch = NO;
  580. }
  581.  
  582.  
  583. /* generate code to return value of type  t */
  584.  
  585.  LOCAL void
  586. #ifdef KR_headers
  587. retval(t)
  588.     register int t;
  589. #else
  590. retval(register int t)
  591. #endif
  592. {
  593.     register Addrp p;
  594.  
  595.     switch(t)
  596.     {
  597.     case TYCHAR:
  598.     case TYCOMPLEX:
  599.     case TYDCOMPLEX:
  600.         break;
  601.  
  602.     case TYLOGICAL:
  603.         t = tylogical;
  604.     case TYINT1:
  605.     case TYADDR:
  606.     case TYSHORT:
  607.     case TYLONG:
  608. #ifdef TYQUAD
  609.     case TYQUAD:
  610. #endif
  611.     case TYREAL:
  612.     case TYDREAL:
  613.     case TYLOGICAL1:
  614.     case TYLOGICAL2:
  615.         p = (Addrp) cpexpr((expptr)retslot);
  616.         p->vtype = t;
  617.         p1_subr_ret (mkconv (t, fixtype((expptr)p)));
  618.         break;
  619.  
  620.     default:
  621.         badtype("retval", t);
  622.     }
  623. }
  624.  
  625.  
  626. /* Do parameter adjustments */
  627.  
  628.  void
  629. #ifdef KR_headers
  630. procode(outfile)
  631.     FILE *outfile;
  632. #else
  633. procode(FILE *outfile)
  634. #endif
  635. {
  636.     prolog(outfile, allargs);
  637.  
  638.     if (nentry > 1)
  639.         entry_goto(outfile);
  640.     }
  641.  
  642. /* Finish bound computations now that all variables are declared.
  643.  * This used to be in setbound(), but under -u the following incurred
  644.  * an erroneous error message:
  645.  *    subroutine foo(x,n)
  646.  *    real x(n)
  647.  *    integer n
  648.  */
  649.  
  650.  static void
  651. #ifdef KR_headers
  652. dim_finish(v)
  653.     Namep v;
  654. #else
  655. dim_finish(Namep v)
  656. #endif
  657. {
  658.     register struct Dimblock *p;
  659.     register expptr q;
  660.     register int i, nd;
  661.  
  662.     p = v->vdim;
  663.     v->vdimfinish = 0;
  664.     nd = p->ndim;
  665.     doin_setbound = 1;
  666.     for(i = 0; i < nd; i++)
  667.         if (q = p->dims[i].dimexpr) {
  668.             q = p->dims[i].dimexpr = make_int_expr(putx(fixtype(q)));
  669.             if (!ONEOF(q->headblock.vtype, MSKINT|MSKREAL))
  670.                 errstr("bad dimension type for %.70s",
  671.                     v->fvarname);
  672.             }
  673.     if (q = p->basexpr)
  674.         p->basexpr = make_int_expr(putx(fixtype(q)));
  675.     doin_setbound = 0;
  676.     }
  677.  
  678.  static void
  679. #ifdef KR_headers
  680. duparg(q)
  681.     Namep q;
  682. #else
  683. duparg(Namep q)
  684. #endif
  685. { errstr("duplicate argument %.80s", q->fvarname); }
  686.  
  687. /*
  688.    manipulate argument lists (allocate argument slot positions)
  689.  * keep track of return types and labels
  690.  */
  691.  
  692.  LOCAL void
  693. #ifdef KR_headers
  694. doentry(ep)
  695.     struct Entrypoint *ep;
  696. #else
  697. doentry(struct Entrypoint *ep)
  698. #endif
  699. {
  700.     register int type;
  701.     register Namep np;
  702.     chainp p, p1;
  703.     register Namep q;
  704.     Addrp rs;
  705.     int it, k;
  706.     extern char dflttype[26];
  707.     Extsym *entryname = ep->entryname;
  708.  
  709.     if (++nentry > 1)
  710.         p1_label((long)(extsymtab - entryname - 1));
  711.  
  712. /* The main program isn't allowed to have parameters, so any given
  713.    parameters are ignored */
  714.  
  715.     if(procclass == CLMAIN || procclass == CLBLOCK)
  716.         return;
  717.  
  718. /* So now we're working with something other than CLMAIN or CLBLOCK.
  719.    Determine the type of its return value. */
  720.  
  721.     impldcl( np = mkname(entryname->fextname) );
  722.     type = np->vtype;
  723.     proc_argchanges = prev_proc && type != entryname->extype;
  724.     entryname->extseen = 1;
  725.     if(proctype == TYUNKNOWN)
  726.         if( (proctype = type) == TYCHAR)
  727.             procleng = np->vleng ? np->vleng->constblock.Const.ci
  728.                          : (ftnint) (-1);
  729.  
  730.     if(proctype == TYCHAR)
  731.     {
  732.         if(type != TYCHAR)
  733.             err("noncharacter entry of character function");
  734.  
  735. /* Functions returning type   char   can only have multiple entries if all
  736.    entries return the same length */
  737.  
  738.         else if( (np->vleng ? np->vleng->constblock.Const.ci :
  739.             (ftnint) (-1)) != procleng)
  740.             err("mismatched character entry lengths");
  741.     }
  742.     else if(type == TYCHAR)
  743.         err("character entry of noncharacter function");
  744.     else if(type != proctype)
  745.         multitype = YES;
  746.     if(rtvlabel[type] == 0)
  747.         rtvlabel[type] = newlabel();
  748.     ep->typelabel = rtvlabel[type];
  749.  
  750.     if(type == TYCHAR)
  751.     {
  752.         if(chslot < 0)
  753.         {
  754.             chslot = nextarg(TYADDR);
  755.             chlgslot = nextarg(TYLENG);
  756.         }
  757.         np->vstg = STGARG;
  758.  
  759. /* Put a new argument in the function, one which will hold the result of
  760.    a character function.  This will have to be named sometime, probably in
  761.    mkarg(). */
  762.  
  763.         if(procleng < 0) {
  764.             np->vleng = (expptr) mkarg(TYLENG, chlgslot);
  765.             np->vleng->addrblock.uname_tag = UNAM_IDENT;
  766.             strcpy (np -> vleng -> addrblock.user.ident,
  767.                 new_func_length());
  768.             }
  769.         if (!xretslot[TYCHAR]) {
  770.             xretslot[TYCHAR] = rs =
  771.                 autovar(0, type, ISCONST(np->vleng)
  772.                     ? np->vleng : ICON(0), "");
  773.             strcpy(rs->user.ident, "ret_val");
  774.             }
  775.     }
  776.  
  777. /* Handle a   complex   return type -- declare a new parameter (pointer to
  778.    a complex value) */
  779.  
  780.     else if( ISCOMPLEX(type) ) {
  781.         if (!xretslot[type])
  782.             xretslot[type] =
  783.                 autovar(0, type, EXNULL, " ret_val");
  784.                 /* the blank is for use in out_addr */
  785.         np->vstg = STGARG;
  786.         if(cxslot < 0)
  787.             cxslot = nextarg(TYADDR);
  788.         }
  789.     else if (type != TYSUBR) {
  790.         if (type == TYUNKNOWN) {
  791.             dclerr("untyped function", np);
  792.             proctype = type = np->vtype =
  793.                 dflttype[letter(np->fvarname[0])];
  794.             }
  795.         if (!xretslot[type])
  796.             xretslot[type] = retslot =
  797.                 autovar(1, type, EXNULL, " ret_val");
  798.                 /* the blank is for use in out_addr */
  799.         np->vstg = STGAUTO;
  800.         }
  801.  
  802.     for(p = ep->arglist ; p ; p = p->nextp)
  803.         if(! (( q = (Namep) (p->datap) )->vknownarg) ) {
  804.             q->vknownarg = 1;
  805.             q->vardesc.varno = nextarg(TYADDR);
  806.             allargs = mkchain((char *)q, allargs);
  807.             q->argno = nallargs++;
  808.             }
  809.         else if (nentry == 1)
  810.             duparg(q);
  811.         else for(p1 = ep->arglist ; p1 != p; p1 = p1->nextp)
  812.             if ((Namep)p1->datap == q)
  813.                 duparg(q);
  814.  
  815.     k = 0;
  816.     for(p = ep->arglist ; p ; p = p->nextp) {
  817.         if(! (( q = (Namep) (p->datap) )->vdcldone) )
  818.             {
  819.             impldcl(q);
  820.             q->vdcldone = YES;
  821.             if(q->vtype == TYCHAR)
  822.                 {
  823.  
  824. /* If we don't know the length of a char*(*) (i.e. a string), we must add
  825.    in this additional length argument. */
  826.  
  827.                 ++nallchargs;
  828.                 if (q->vclass == CLPROC)
  829.                     nallchargs--;
  830.                 else if (q->vleng == NULL) {
  831.                     /* character*(*) */
  832.                     q->vleng = (expptr)
  833.                         mkarg(TYLENG, nextarg(TYLENG) );
  834.                     unamstring((Addrp)q->vleng,
  835.                         new_arg_length(q));
  836.                     }
  837.                 }
  838.             }
  839.         if (q->vdimfinish)
  840.             dim_finish(q);
  841.         if (q->vtype == TYCHAR && q->vclass != CLPROC)
  842.             k++;
  843.         }
  844.  
  845.     if (entryname->extype != type)
  846.         changedtype(np);
  847.  
  848.     /* save information for checking consistency of arg lists */
  849.  
  850.     it = infertypes;
  851.     if (entryname->exproto)
  852.         infertypes = 1;
  853.     save_argtypes(ep->arglist, &entryname->arginfo, &np->arginfo,
  854.             0, np->fvarname, STGEXT, k, np->vtype, 2);
  855.     infertypes = it;
  856. }
  857.  
  858.  
  859.  
  860.  LOCAL int
  861. #ifdef KR_headers
  862. nextarg(type)
  863.     int type;
  864. #else
  865. nextarg(int type)
  866. #endif
  867. {
  868.     type = type;    /* shut up warning */
  869.     return(lastargslot++);
  870.     }
  871.  
  872.  LOCAL void
  873. #ifdef KR_headers
  874. dim_check(q)
  875.     Namep q;
  876. #else
  877. dim_check(Namep q)
  878. #endif
  879. {
  880.     register struct Dimblock *vdim = q->vdim;
  881.  
  882.     if(!vdim->nelt || !ISICON(vdim->nelt))
  883.         dclerr("adjustable dimension on non-argument", q);
  884.     else if (vdim->nelt->constblock.Const.ci <= 0)
  885.         dclerr("nonpositive dimension", q);
  886.     }
  887.  
  888.  LOCAL void
  889. dobss(Void)
  890. {
  891.     register struct Hashentry *p;
  892.     register Namep q;
  893.     int qstg, qclass, qtype;
  894.     Extsym *e;
  895.  
  896.     for(p = hashtab ; p<lasthash ; ++p)
  897.         if(q = p->varp)
  898.         {
  899.             qstg = q->vstg;
  900.             qtype = q->vtype;
  901.             qclass = q->vclass;
  902.  
  903.             if( (qclass==CLUNKNOWN && qstg!=STGARG) ||
  904.                 (qclass==CLVAR && qstg==STGUNKNOWN) ) {
  905.                 if (!(q->vis_assigned | q->vimpldovar))
  906.                     warn1("local variable %s never used",
  907.                         q->fvarname);
  908.                 }
  909.             else if(qclass==CLVAR && qstg==STGBSS)
  910.             { ; }
  911.  
  912. /* Give external procedures the proper storage class */
  913.  
  914.             else if(qclass==CLPROC && q->vprocclass==PEXTERNAL
  915.                     && qstg!=STGARG) {
  916.                 e = mkext(q->fvarname,addunder(q->cvarname));
  917.                 e->extstg = STGEXT;
  918.                 q->vardesc.varno = e - extsymtab;
  919.                 if (e->extype != qtype)
  920.                     changedtype(q);
  921.                 }
  922.             if(qclass==CLVAR) {
  923.                 if (qstg != STGARG && q->vdim)
  924.                 dim_check(q);
  925.             } /* if qclass == CLVAR */
  926.         }
  927.  
  928. }
  929.  
  930.  
  931.  void
  932. donmlist(Void)
  933. {
  934.     register struct Hashentry *p;
  935.     register Namep q;
  936.  
  937.     for(p=hashtab; p<lasthash; ++p)
  938.         if( (q = p->varp) && q->vclass==CLNAMELIST)
  939.             namelist(q);
  940. }
  941.  
  942.  
  943. /* iarrlen -- Returns the size of the array in bytes, or -1 */
  944.  
  945.  ftnint
  946. #ifdef KR_headers
  947. iarrlen(q)
  948.     register Namep q;
  949. #else
  950. iarrlen(register Namep q)
  951. #endif
  952. {
  953.     ftnint leng;
  954.  
  955.     leng = typesize[q->vtype];
  956.     if(leng <= 0)
  957.         return(-1);
  958.     if(q->vdim)
  959.         if( ISICON(q->vdim->nelt) )
  960.             leng *= q->vdim->nelt->constblock.Const.ci;
  961.         else    return(-1);
  962.     if(q->vleng)
  963.         if( ISICON(q->vleng) )
  964.             leng *= q->vleng->constblock.Const.ci;
  965.         else return(-1);
  966.     return(leng);
  967. }
  968.  
  969.  void
  970. #ifdef KR_headers
  971. namelist(np)
  972.     Namep np;
  973. #else
  974. namelist(Namep np)
  975. #endif
  976. {
  977.     register chainp q;
  978.     register Namep v;
  979.     int y;
  980.  
  981.     if (!np->visused)
  982.         return;
  983.     y = 0;
  984.  
  985.     for(q = np->varxptr.namelist ; q ; q = q->nextp)
  986.     {
  987.         vardcl( v = (Namep) (q->datap) );
  988.         if( !ONEOF(v->vstg, MSKSTATIC) )
  989.             dclerr("may not appear in namelist", v);
  990.         else {
  991.             v->vnamelist = 1;
  992.             v->visused = 1;
  993.             v->vsave = 1;
  994.             y = 1;
  995.             }
  996.     np->visused = y;
  997.     }
  998. }
  999.  
  1000. /* docommon -- called at the end of procedure declarations, before
  1001.    equivalences and the procedure body */
  1002.  
  1003.  LOCAL void
  1004. docommon(Void)
  1005. {
  1006.     register Extsym *extptr;
  1007.     register chainp q, q1;
  1008.     struct Dimblock *t;
  1009.     expptr neltp;
  1010.     register Namep comvar;
  1011.     ftnint size;
  1012.     int i, k, pref, type;
  1013.     extern int type_pref[];
  1014.  
  1015.     for(extptr = extsymtab ; extptr<nextext ; ++extptr)
  1016.     if (extptr->extstg == STGCOMMON && (q = extptr->extp)) {
  1017.  
  1018. /* If a common declaration also had a list of variables ... */
  1019.  
  1020.         q = extptr->extp = revchain(q);
  1021.         pref = 1;
  1022.         for(k = TYCHAR; q ; q = q->nextp)
  1023.         {
  1024.         comvar = (Namep) (q->datap);
  1025.  
  1026.         if(comvar->vdcldone == NO)
  1027.             vardcl(comvar);
  1028.         type = comvar->vtype;
  1029.         if (pref < type_pref[type])
  1030.             pref = type_pref[k = type];
  1031.         if(extptr->extleng % typealign[type] != 0) {
  1032.             dclerr("common alignment", comvar);
  1033.             --nerr; /* don't give bad return code for this */
  1034. #if 0
  1035.             extptr->extleng = roundup(extptr->extleng, typealign[type]);
  1036. #endif
  1037.         } /* if extptr -> extleng % */
  1038.  
  1039. /* Set the offset into the common block */
  1040.  
  1041.         comvar->voffset = extptr->extleng;
  1042.         comvar->vardesc.varno = extptr - extsymtab;
  1043.         if(type == TYCHAR)
  1044.             size = comvar->vleng->constblock.Const.ci;
  1045.         else
  1046.             size = typesize[type];
  1047.         if(t = comvar->vdim)
  1048.             if( (neltp = t->nelt) && ISCONST(neltp) )
  1049.             size *= neltp->constblock.Const.ci;
  1050.             else
  1051.             dclerr("adjustable array in common", comvar);
  1052.  
  1053. /* Adjust the length of the common block so far */
  1054.  
  1055.         extptr->extleng += size;
  1056.         } /* for */
  1057.  
  1058.         extptr->extype = k;
  1059.  
  1060. /* Determine curno and, if new, save this identifier chain */
  1061.  
  1062.         q1 = extptr->extp;
  1063.         for (q = extptr->allextp, i = 0; q; i++, q = q->nextp)
  1064.         if (struct_eq((chainp)q->datap, q1))
  1065.             break;
  1066.         if (q)
  1067.         extptr->curno = extptr->maxno - i;
  1068.         else {
  1069.         extptr->curno = ++extptr->maxno;
  1070.         extptr->allextp = mkchain((char *)extptr->extp,
  1071.                         extptr->allextp);
  1072.         }
  1073.     } /* if extptr -> extstg == STGCOMMON */
  1074.  
  1075. /* Now the hash table entries have STGCOMMON, vdcldone, voffset, and
  1076.    varno.  And the common block itself has its full size in extleng. */
  1077.  
  1078. } /* docommon */
  1079.  
  1080.  
  1081. /* copy_data -- copy the Namep entries so they are available even after
  1082.    the hash table is empty */
  1083.  
  1084.  void
  1085. #ifdef KR_headers
  1086. copy_data(list)
  1087.     chainp list;
  1088. #else
  1089. copy_data(chainp list)
  1090. #endif
  1091. {
  1092.     for (; list; list = list -> nextp) {
  1093.     Namep namep = ALLOC (Nameblock);
  1094.     int size, nd, i;
  1095.     struct Dimblock *dp;
  1096.  
  1097.     cpn(sizeof(struct Nameblock), list->datap, (char *)namep);
  1098.     namep->fvarname = strcpy(gmem(strlen(namep->fvarname)+1,0),
  1099.         namep->fvarname);
  1100.     namep->cvarname = strcmp(namep->fvarname, namep->cvarname)
  1101.         ? strcpy(gmem(strlen(namep->cvarname)+1,0), namep->cvarname)
  1102.         : namep->fvarname;
  1103.     if (namep -> vleng)
  1104.         namep -> vleng = (expptr) cpexpr (namep -> vleng);
  1105.     if (namep -> vdim) {
  1106.         nd = namep -> vdim -> ndim;
  1107.         size = sizeof(int) + (3 + 2 * nd) * sizeof (expptr);
  1108.         dp = (struct Dimblock *) ckalloc (size);
  1109.         cpn(size, (char *)namep->vdim, (char *)dp);
  1110.         namep -> vdim = dp;
  1111.         dp->nelt = (expptr)cpexpr(dp->nelt);
  1112.         for (i = 0; i < nd; i++) {
  1113.         dp -> dims[i].dimsize = (expptr) cpexpr (dp -> dims[i].dimsize);
  1114.         } /* for */
  1115.     } /* if */
  1116.     list -> datap = (char *) namep;
  1117.     } /* for */
  1118. } /* copy_data */
  1119.  
  1120.  
  1121.  
  1122.  LOCAL void
  1123. docomleng(Void)
  1124. {
  1125.     register Extsym *p;
  1126.  
  1127.     for(p = extsymtab ; p < nextext ; ++p)
  1128.         if(p->extstg == STGCOMMON)
  1129.         {
  1130.             if(p->maxleng!=0 && p->extleng!=0 && p->maxleng!=p->extleng
  1131.                 && strcmp(Blank, p->cextname) )
  1132.                 warn1("incompatible lengths for common block %.60s",
  1133.                     p->fextname);
  1134.             if(p->maxleng < p->extleng)
  1135.                 p->maxleng = p->extleng;
  1136.             p->extleng = 0;
  1137.         }
  1138. }
  1139.  
  1140.  
  1141. /* ROUTINES DEALING WITH AUTOMATIC AND TEMPORARY STORAGE */
  1142.  
  1143.  void
  1144. #ifdef KR_headers
  1145. frtemp(p)
  1146.     Addrp p;
  1147. #else
  1148. frtemp(Addrp p)
  1149. #endif
  1150. {
  1151.     /* put block on chain of temps to be reclaimed */
  1152.     holdtemps = mkchain((char *)p, holdtemps);
  1153. }
  1154.  
  1155.  void
  1156. freetemps(Void)
  1157. {
  1158.     register chainp p, p1;
  1159.     register Addrp q;
  1160.     register int t;
  1161.  
  1162.     p1 = holdtemps;
  1163.     while(p = p1) {
  1164.         q = (Addrp)p->datap;
  1165.         t = q->vtype;
  1166.         if (t == TYCHAR && q->varleng != 0) {
  1167.             /* restore clobbered character string lengths */
  1168.             frexpr(q->vleng);
  1169.             q->vleng = ICON(q->varleng);
  1170.             }
  1171.         p1 = p->nextp;
  1172.         p->nextp = templist[t];
  1173.         templist[t] = p;
  1174.         }
  1175.     holdtemps = 0;
  1176.     }
  1177.  
  1178. /* allocate an automatic variable slot for each of   nelt   variables */
  1179.  
  1180.  Addrp
  1181. #ifdef KR_headers
  1182. autovar(nelt0, t, lengp, name)
  1183.     register int nelt0;
  1184.     register int t;
  1185.     expptr lengp;
  1186.     char *name;
  1187. #else
  1188. autovar(register int nelt0, register int t, expptr lengp, char *name)
  1189. #endif
  1190. {
  1191.     ftnint leng;
  1192.     register Addrp q;
  1193.     register int nelt = nelt0 > 0 ? nelt0 : 1;
  1194.     extern char *av_pfix[];
  1195.  
  1196.     if(t == TYCHAR)
  1197.         if( ISICON(lengp) )
  1198.             leng = lengp->constblock.Const.ci;
  1199.         else    {
  1200.             Fatal("automatic variable of nonconstant length");
  1201.         }
  1202.     else
  1203.         leng = typesize[t];
  1204.  
  1205.     q = ALLOC(Addrblock);
  1206.     q->tag = TADDR;
  1207.     q->vtype = t;
  1208.     if(t == TYCHAR)
  1209.     {
  1210.         q->vleng = ICON(leng);
  1211.         q->varleng = leng;
  1212.     }
  1213.     q->vstg = STGAUTO;
  1214.     q->ntempelt = nelt;
  1215.     q->isarray = (nelt > 1);
  1216.     q->memoffset = ICON(0);
  1217.  
  1218.     /* kludge for nls so we can have ret_val rather than ret_val_4 */
  1219.     if (*name == ' ')
  1220.         unamstring(q, name);
  1221.     else {
  1222.         q->uname_tag = UNAM_IDENT;
  1223.         temp_name(av_pfix[t], ++autonum[t], q->user.ident);
  1224.         }
  1225.     if (nelt0 > 0)
  1226.         declare_new_addr (q);
  1227.     return(q);
  1228. }
  1229.  
  1230.  
  1231. /* Returns a temporary of the appropriate type.  Will reuse existing
  1232.    temporaries when possible */
  1233.  
  1234.  Addrp
  1235. #ifdef KR_headers
  1236. mktmpn(nelt, type, lengp)
  1237.     int nelt;
  1238.     register int type;
  1239.     expptr lengp;
  1240. #else
  1241. mktmpn(int nelt, register int type, expptr lengp)
  1242. #endif
  1243. {
  1244.     ftnint leng;
  1245.     chainp p, oldp;
  1246.     register Addrp q;
  1247.  
  1248.     if(type==TYUNKNOWN || type==TYERROR)
  1249.         badtype("mktmpn", type);
  1250.  
  1251.     if(type==TYCHAR)
  1252.         if(lengp && ISICON(lengp) )
  1253.             leng = lengp->constblock.Const.ci;
  1254.         else    {
  1255.             err("adjustable length");
  1256.             return( (Addrp) errnode() );
  1257.         }
  1258.     else if (type > TYCHAR || type < TYADDR) {
  1259.         erri("mktmpn: unexpected type %d", type);
  1260.         exit(1);
  1261.         }
  1262. /*
  1263.  * if a temporary of appropriate shape is on the templist,
  1264.  * remove it from the list and return it
  1265.  */
  1266.     for(oldp=CHNULL, p=templist[type];  p  ;  oldp=p, p=p->nextp)
  1267.     {
  1268.         q = (Addrp) (p->datap);
  1269.         if(q->ntempelt==nelt &&
  1270.             (type!=TYCHAR || q->vleng->constblock.Const.ci==leng) )
  1271.         {
  1272.             if(oldp)
  1273.                 oldp->nextp = p->nextp;
  1274.             else
  1275.                 templist[type] = p->nextp;
  1276.             free( (charptr) p);
  1277.             return(q);
  1278.         }
  1279.     }
  1280.     q = autovar(nelt, type, lengp, "");
  1281.     return(q);
  1282. }
  1283.  
  1284.  
  1285.  
  1286.  
  1287. /* mktmp -- create new local variable; call it something like   name
  1288.    lengp   is taken directly, not copied */
  1289.  
  1290.  Addrp
  1291. #ifdef KR_headers
  1292. mktmp(type, lengp)
  1293.     int type;
  1294.     expptr lengp;
  1295. #else
  1296. mktmp(int type, expptr lengp)
  1297. #endif
  1298. {
  1299.     Addrp rv;
  1300.     /* arrange for temporaries to be recycled */
  1301.     /* at the end of this statement... */
  1302.     rv = mktmpn(1,type,lengp);
  1303.     frtemp((Addrp)cpexpr((expptr)rv));
  1304.     return rv;
  1305. }
  1306.  
  1307. /* mktmp0 omits frtemp() */
  1308.  Addrp
  1309. #ifdef KR_headers
  1310. mktmp0(type, lengp)
  1311.     int type;
  1312.     expptr lengp;
  1313. #else
  1314. mktmp0(int type, expptr lengp)
  1315. #endif
  1316. {
  1317.     Addrp rv;
  1318.     /* arrange for temporaries to be recycled */
  1319.     /* when this Addrp is freed */
  1320.     rv = mktmpn(1,type,lengp);
  1321.     rv->istemp = YES;
  1322.     return rv;
  1323. }
  1324.  
  1325. /* VARIOUS ROUTINES FOR PROCESSING DECLARATIONS */
  1326.  
  1327. /* comblock -- Declare a new common block.  Input parameters name the block;
  1328.    s   will be NULL if the block is unnamed */
  1329.  
  1330.  Extsym *
  1331. #ifdef KR_headers
  1332. comblock(s)
  1333.     register char *s;
  1334. #else
  1335. comblock(register char *s)
  1336. #endif
  1337. {
  1338.     Extsym *p;
  1339.     register char *t;
  1340.     register int c, i;
  1341.     char cbuf[256], *s0;
  1342.  
  1343. /* Give the unnamed common block a unique name */
  1344.  
  1345.     if(*s == 0)
  1346.         p = mkext1(s0 = Blank, Blank);
  1347.     else {
  1348.         s0 = s;
  1349.         t = cbuf;
  1350.         for(i = 0; c = *t = *s++; t++)
  1351.             if (c == '_')
  1352.                 i = 1;
  1353.         if (i)
  1354.             *t++ = '_';
  1355.         t[0] = '_';
  1356.         t[1] = 0;
  1357.         p = mkext1(s0,cbuf);
  1358.         }
  1359.     if(p->extstg == STGUNKNOWN)
  1360.         p->extstg = STGCOMMON;
  1361.     else if(p->extstg != STGCOMMON)
  1362.     {
  1363.         errstr("%.52s cannot be a common block: it is a subprogram.",
  1364.             s0);
  1365.         return(0);
  1366.     }
  1367.  
  1368.     return( p );
  1369. }
  1370.  
  1371.  
  1372. /* incomm -- add a new variable to a common declaration */
  1373.  
  1374.  void
  1375. #ifdef KR_headers
  1376. incomm(c, v)
  1377.     Extsym *c;
  1378.     Namep v;
  1379. #else
  1380. incomm(Extsym *c, Namep v)
  1381. #endif
  1382. {
  1383.     if (!c)
  1384.         return;
  1385.     if(v->vstg != STGUNKNOWN && !v->vimplstg)
  1386.         dclerr(v->vstg == STGARG
  1387.             ? "dummy arguments cannot be in common"
  1388.             : "incompatible common declaration", v);
  1389.     else
  1390.     {
  1391.         v->vstg = STGCOMMON;
  1392.         c->extp = mkchain((char *)v, c->extp);
  1393.     }
  1394. }
  1395.  
  1396.  
  1397.  
  1398.  
  1399. /* settype -- set the type or storage class of a Namep object.  If
  1400.    v -> vstg == STGUNKNOWN && type < 0,   attempt to reset vstg to be
  1401.    -type.  This function will not change any earlier definitions in   v,
  1402.    in will only attempt to fill out more information give the other params */
  1403.  
  1404.  void
  1405. #ifdef KR_headers
  1406. settype(v, type, length)
  1407.     register Namep v;
  1408.     register int type;
  1409.     register ftnint length;
  1410. #else
  1411. settype(register Namep v, register int type, register ftnint length)
  1412. #endif
  1413. {
  1414.     int type1;
  1415.  
  1416.     if(type == TYUNKNOWN)
  1417.         return;
  1418.  
  1419.     if(type==TYSUBR && v->vtype!=TYUNKNOWN && v->vstg==STGARG)
  1420.     {
  1421.         v->vtype = TYSUBR;
  1422.         frexpr(v->vleng);
  1423.         v->vleng = 0;
  1424.         v->vimpltype = 0;
  1425.     }
  1426.     else if(type < 0)    /* storage class set */
  1427.     {
  1428.         if(v->vstg == STGUNKNOWN)
  1429.             v->vstg = - type;
  1430.         else if(v->vstg != -type)
  1431.             dclerr("incompatible storage declarations", v);
  1432.     }
  1433.     else if(v->vtype == TYUNKNOWN || v->vimpltype && v->vtype != type)
  1434.     {
  1435.         if( (v->vtype = lengtype(type, length))==TYCHAR )
  1436.             if (length>=0)
  1437.                 v->vleng = ICON(length);
  1438.             else if (parstate >= INDATA)
  1439.                 v->vleng = ICON(1);    /* avoid a memory fault */
  1440.         v->vimpltype = 0;
  1441.  
  1442.         if (v->vclass == CLPROC) {
  1443.             if (v->vstg == STGEXT
  1444.              && (type1 = extsymtab[v->vardesc.varno].extype)
  1445.              &&  type1 != v->vtype)
  1446.                 changedtype(v);
  1447.             else if (v->vprocclass == PTHISPROC
  1448.                     && (parstate >= INDATA
  1449.                         || procclass == CLMAIN)
  1450.                     && !xretslot[type]) {
  1451.                 xretslot[type] = autovar(ONEOF(type,
  1452.                     MSKCOMPLEX|MSKCHAR) ? 0 : 1, type,
  1453.                     v->vleng, " ret_val");
  1454.                 if (procclass == CLMAIN)
  1455.                     errstr(
  1456.                 "illegal use of %.60s (main program name)",
  1457.                     v->fvarname);
  1458.                 /* not completely right, but enough to */
  1459.                 /* avoid memory faults; we won't */
  1460.                 /* emit any C as we have illegal Fortran */
  1461.                 }
  1462.             }
  1463.     }
  1464.     else if(v->vtype!=type) {
  1465.  incompat:
  1466.         dclerr("incompatible type declarations", v);
  1467.         }
  1468.     else if (type==TYCHAR)
  1469.         if (v->vleng && v->vleng->constblock.Const.ci != length)
  1470.             goto incompat;
  1471.         else if (parstate >= INDATA)
  1472.             v->vleng = ICON(1);    /* avoid a memory fault */
  1473. }
  1474.  
  1475.  
  1476.  
  1477.  
  1478.  
  1479. /* lengtype -- returns the proper compiler type, given input of Fortran
  1480.    type and length specifier */
  1481.  
  1482.  int
  1483. #ifdef KR_headers
  1484. lengtype(type, len)
  1485.     register int type;
  1486.     ftnint len;
  1487. #else
  1488. lengtype(register int type, ftnint len)
  1489. #endif
  1490. {
  1491.     register int length = (int)len;
  1492.     switch(type)
  1493.     {
  1494.     case TYREAL:
  1495.         if(length == typesize[TYDREAL])
  1496.             return(TYDREAL);
  1497.         if(length == typesize[TYREAL])
  1498.             goto ret;
  1499.         break;
  1500.  
  1501.     case TYCOMPLEX:
  1502.         if(length == typesize[TYDCOMPLEX])
  1503.             return(TYDCOMPLEX);
  1504.         if(length == typesize[TYCOMPLEX])
  1505.             goto ret;
  1506.         break;
  1507.  
  1508.     case TYINT1:
  1509.     case TYSHORT:
  1510.     case TYDREAL:
  1511.     case TYDCOMPLEX:
  1512.     case TYCHAR:
  1513.     case TYLOGICAL1:
  1514.     case TYLOGICAL2:
  1515.     case TYUNKNOWN:
  1516.     case TYSUBR:
  1517.     case TYERROR:
  1518. #ifdef TYQUAD
  1519.     case TYQUAD:
  1520. #endif
  1521.         goto ret;
  1522.  
  1523.     case TYLOGICAL:
  1524.         switch(length) {
  1525.             case 0: return tylog;
  1526.             case 1:    return TYLOGICAL1;
  1527.             case 2: return TYLOGICAL2;
  1528.             case 4: goto ret;
  1529.             }
  1530. #if 0 /*!!??!!*/
  1531.         if(length == typesize[TYLOGICAL])
  1532.             goto ret;
  1533. #endif
  1534.         break;
  1535.  
  1536.     case TYLONG:
  1537.         if(length == 0)
  1538.             return(tyint);
  1539.         if (length == 1)
  1540.             return TYINT1;
  1541.         if(length == typesize[TYSHORT])
  1542.             return(TYSHORT);
  1543. #ifdef TYQUAD
  1544.         if(length == typesize[TYQUAD] && use_tyquad)
  1545.             return(TYQUAD);
  1546. #endif
  1547.         if(length == typesize[TYLONG])
  1548.             goto ret;
  1549.         break;
  1550.     default:
  1551.         badtype("lengtype", type);
  1552.     }
  1553.  
  1554.     if(len != 0)
  1555.         err("incompatible type-length combination");
  1556.  
  1557. ret:
  1558.     return(type);
  1559. }
  1560.  
  1561.  
  1562.  
  1563.  
  1564.  
  1565. /* setintr -- Set Intrinsic function */
  1566.  
  1567.  void
  1568. #ifdef KR_headers
  1569. setintr(v)
  1570.     register Namep v;
  1571. #else
  1572. setintr(register Namep v)
  1573. #endif
  1574. {
  1575.     int k;
  1576.  
  1577.     if(k = intrfunct(v->fvarname)) {
  1578.         if ((*(struct Intrpacked *)&k).f4)
  1579.             if (noextflag)
  1580.                 goto unknown;
  1581.             else
  1582.                 dcomplex_seen++;
  1583.         v->vardesc.varno = k;
  1584.         }
  1585.     else {
  1586.  unknown:
  1587.         dclerr("unknown intrinsic function", v);
  1588.         return;
  1589.         }
  1590.     if(v->vstg == STGUNKNOWN)
  1591.         v->vstg = STGINTR;
  1592.     else if(v->vstg!=STGINTR)
  1593.         dclerr("incompatible use of intrinsic function", v);
  1594.     if(v->vclass==CLUNKNOWN)
  1595.         v->vclass = CLPROC;
  1596.     if(v->vprocclass == PUNKNOWN)
  1597.         v->vprocclass = PINTRINSIC;
  1598.     else if(v->vprocclass != PINTRINSIC)
  1599.         dclerr("invalid intrinsic declaration", v);
  1600. }
  1601.  
  1602.  
  1603.  
  1604. /* setext -- Set External declaration -- assume that unknowns will become
  1605.    procedures */
  1606.  
  1607.  void
  1608. #ifdef KR_headers
  1609. setext(v)
  1610.     register Namep v;
  1611. #else
  1612. setext(register Namep v)
  1613. #endif
  1614. {
  1615.     if(v->vclass == CLUNKNOWN)
  1616.         v->vclass = CLPROC;
  1617.     else if(v->vclass != CLPROC)
  1618.         dclerr("invalid external declaration", v);
  1619.  
  1620.     if(v->vprocclass == PUNKNOWN)
  1621.         v->vprocclass = PEXTERNAL;
  1622.     else if(v->vprocclass != PEXTERNAL)
  1623.         dclerr("invalid external declaration", v);
  1624. } /* setext */
  1625.  
  1626.  
  1627.  
  1628.  
  1629. /* create dimensions block for array variable */
  1630.  
  1631.  void
  1632. #ifdef KR_headers
  1633. setbound(v, nd, dims)
  1634.     register Namep v;
  1635.     int nd;
  1636.     struct Dims *dims;
  1637. #else
  1638. setbound(register Namep v, int nd, struct Dims *dims)
  1639. #endif
  1640. {
  1641.     register expptr q, t;
  1642.     register struct Dimblock *p;
  1643.     int i;
  1644.     extern chainp new_vars;
  1645.     char buf[256];
  1646.  
  1647.     if(v->vclass == CLUNKNOWN)
  1648.         v->vclass = CLVAR;
  1649.     else if(v->vclass != CLVAR)
  1650.     {
  1651.         dclerr("only variables may be arrays", v);
  1652.         return;
  1653.     }
  1654.  
  1655.     v->vdim = p = (struct Dimblock *)
  1656.         ckalloc( sizeof(int) + (3+2*nd)*sizeof(expptr) );
  1657.     p->ndim = nd--;
  1658.     p->nelt = ICON(1);
  1659.     doin_setbound = 1;
  1660.  
  1661.     for(i = 0; i <= nd; ++i)
  1662.     {
  1663.         if( (q = dims[i].ub) == NULL)
  1664.         {
  1665.             if(i == nd)
  1666.             {
  1667.                 frexpr(p->nelt);
  1668.                 p->nelt = NULL;
  1669.             }
  1670.             else
  1671.                 err("only last bound may be asterisk");
  1672.             p->dims[i].dimsize = ICON(1);
  1673.             p->dims[i].dimexpr = NULL;
  1674.         }
  1675.         else
  1676.         {
  1677.  
  1678.             if(dims[i].lb)
  1679.             {
  1680.                 q = mkexpr(OPMINUS, q, cpexpr(dims[i].lb));
  1681.                 q = mkexpr(OPPLUS, q, ICON(1) );
  1682.             }
  1683.             if( ISCONST(q) )
  1684.             {
  1685.                 p->dims[i].dimsize = q;
  1686.                 p->dims[i].dimexpr = (expptr) PNULL;
  1687.             }
  1688.             else {
  1689.                 sprintf(buf, " %s_dim%d", v->fvarname, i+1);
  1690.                 p->dims[i].dimsize = (expptr)
  1691.                     autovar(1, tyint, EXNULL, buf);
  1692.                 p->dims[i].dimexpr = q;
  1693.                 if (i == nd)
  1694.                     v->vlastdim = new_vars;
  1695.                 v->vdimfinish = 1;
  1696.             }
  1697.             if(p->nelt)
  1698.                 p->nelt = mkexpr(OPSTAR, p->nelt,
  1699.                     cpexpr(p->dims[i].dimsize) );
  1700.         }
  1701.     }
  1702.  
  1703.     q = dims[nd].lb;
  1704.     if(q == NULL)
  1705.         q = ICON(1);
  1706.  
  1707.     for(i = nd-1 ; i>=0 ; --i)
  1708.     {
  1709.         t = dims[i].lb;
  1710.         if(t == NULL)
  1711.             t = ICON(1);
  1712.         if(p->dims[i].dimsize)
  1713.             q = mkexpr(OPPLUS, t,
  1714.                 mkexpr(OPSTAR, cpexpr(p->dims[i].dimsize), q));
  1715.     }
  1716.  
  1717.     if( ISCONST(q) )
  1718.     {
  1719.         p->baseoffset = q;
  1720.         p->basexpr = NULL;
  1721.     }
  1722.     else
  1723.     {
  1724.         sprintf(buf, " %s_offset", v->fvarname);
  1725.         p->baseoffset = (expptr) autovar(1, tyint, EXNULL, buf);
  1726.         p->basexpr = q;
  1727.         v->vdimfinish = 1;
  1728.     }
  1729.     doin_setbound = 0;
  1730. }
  1731.  
  1732.  
  1733.  void
  1734. #ifdef KR_headers
  1735. wr_abbrevs(outfile, function_head, vars)
  1736.     FILE *outfile;
  1737.     int function_head;
  1738.     chainp vars;
  1739. #else
  1740. wr_abbrevs(FILE *outfile, int function_head, chainp vars)
  1741. #endif
  1742. {
  1743.     for (; vars; vars = vars -> nextp) {
  1744.     Namep name = (Namep) vars -> datap;
  1745.     if (!name->visused)
  1746.         continue;
  1747.  
  1748.     if (function_head)
  1749.         nice_printf (outfile, "#define ");
  1750.     else
  1751.         nice_printf (outfile, "#undef ");
  1752.     out_name (outfile, name);
  1753.  
  1754.     if (function_head) {
  1755.         Extsym *comm = &extsymtab[name -> vardesc.varno];
  1756.  
  1757.         nice_printf (outfile, " (");
  1758.         extern_out (outfile, comm);
  1759.         nice_printf (outfile, "%d.", comm->curno);
  1760.         nice_printf (outfile, "%s)", name->cvarname);
  1761.     } /* if function_head */
  1762.     nice_printf (outfile, "\n");
  1763.     } /* for */
  1764. } /* wr_abbrevs */
  1765.