home *** CD-ROM | disk | FTP | other *** search
/ The Fred Fish Collection 1.5 / ffcollection-1-5-1992-11.iso / ff_progs / prog_c / zc.lzh / ZC / ZCSRC.LZH / src / decl.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-06-02  |  11.1 KB  |  618 lines

  1. /* Copyright (c) 1988 by Sozobon, Limited.  Author: Johann Ruegg
  2.  *
  3.  * Permission is granted to anyone to use this software for any purpose
  4.  * on any computer system, and to redistribute it freely, with the
  5.  * following restrictions:
  6.  * 1) No charge may be made other than reasonable charges for reproduction.
  7.  * 2) Modified versions must be clearly marked as such.
  8.  * 3) The authors are not responsible for any harmful consequences
  9.  *    of using this software, even if they result from defects in it.
  10.  *
  11.  *    decl.c
  12.  *
  13.  *    Do all declarations
  14.  *
  15.  *    Currently,
  16.  *        struct tags are local
  17.  *        struct members are tied to the struct
  18.  *        enum tags are ignored
  19.  *        enum members are local
  20.  */
  21.  
  22. #include <stdio.h>
  23. #include "param.h"
  24. #include "tok.h"
  25. #include "nodes.h"
  26.  
  27. extern NODE *cur;
  28. extern level;
  29.  
  30. NODEP symtab[NHASH], tagtab;
  31. extern NODE *blktab;
  32.  
  33. NODEP alltags(), allsyms(), llook(), hlook();
  34.  
  35. extern int oflags[];
  36. #define debug    oflags['v'-'a']
  37.  
  38. /* look for global data decls
  39.     return when see something weird
  40.     return last ID declared */
  41. NODEP
  42. glb_decls()
  43. {
  44.     register NODEP head, xp;
  45.     NODEP d_type(), def_type(), d_declr();
  46.     int sclass;
  47.  
  48.     for(;;) {
  49.         sclass = d_scl(HERE_SC);
  50.         head = d_type();
  51.         if (head == NULL)
  52.             head = def_type();
  53.         if (ok_gsh(sclass, head) == 0)
  54.             continue;
  55.     more:
  56.         xp = d_declr(head,0);
  57.         if (ok_gx(xp,head)) {
  58.             xp->e_sc = sclass;
  59.             opt_ginit(xp);
  60.             if ( sclass == K_EXTERN )
  61.                 xp->n_flags |= N_XREF;
  62.             else if ( sclass == HERE_SC)
  63.                 xp->n_flags |= N_XDEF;
  64.             new_sym(symtab,xp);
  65.             if (xp->n_tptr->t_token == '(') {       /* func */
  66.                 if (cur->e_token == ',' ||
  67.                     cur->e_token == ';')
  68.                     fix_fun(xp);
  69.                 else
  70.                     return xp;
  71.             }
  72.         }
  73.  
  74.         if (cur->e_token == ',') {
  75.             fadvnode();
  76.             goto more;
  77.         }
  78.  
  79.         if (cur->e_token == ';') {
  80.             fadvnode();
  81.         } else
  82.             return NULL;
  83.     }
  84. }
  85.  
  86. /* do local or arg decls
  87.     return 1 if see something */
  88. loc_decls()
  89. {
  90.     register NODEP head, xp;
  91.     NODEP d_type(), def_type(), d_declr();
  92.     int sclass;
  93.     int regs;
  94.     long size;
  95.     int rv = 0;
  96.  
  97.     size = level > 2 ? blktab->n_next->b_size : 0;
  98.     regs = level > 1 ? blktab->n_next->b_regs : 0;
  99.     while (is_ty_start()) {
  100.         rv++;
  101.         sclass = d_scl(K_AUTO);
  102.         head = d_type();
  103.         if (head == NULL)
  104.             head = def_type();
  105.         if (ok_lsh(sclass, head) == 0)
  106.             continue;
  107.     more:
  108.         xp = d_declr(head,0);
  109.         if (ok_lx(xp,head)) {
  110.             xp->e_sc = sclass;
  111.             if (level > 1) /* not args */
  112.                 lc_size(&size, ®s, xp);
  113.             fix_fun(xp);
  114.             if (level>1 && xp->e_sc == K_EXTERN ){
  115.                 xp->n_flags |= N_XREF;
  116.                 new_sym(symtab,xp);
  117.             } else
  118.                 new_sym(&blktab->b_syms,xp);
  119.             opt_linit(xp,sclass);
  120.         }
  121.  
  122.         if (cur->e_token == ',') {
  123.             fadvnode();
  124.             goto more;
  125.         }
  126.  
  127.         if (cur->e_token == ';') {
  128.             fadvnode();
  129.         } else {
  130.             error("expect ;");
  131.             return 1;
  132.         }
  133.     }
  134.     while (STACKALN & size)
  135.         size++;
  136.     blktab->b_size = size;
  137.     blktab->b_regs = regs;
  138.     return rv;
  139. }
  140.  
  141. /* Decls inside Struct/Union */
  142. su_decls(listpp, isunion, sizep, alnp)
  143. NODEP *listpp;
  144. long *sizep;
  145. char *alnp;
  146. {
  147.     register NODEP head, xp;
  148.     NODEP d_type(), d_declr();
  149.     long size;
  150.     char aln;
  151.     int fldw, fldoff;
  152.  
  153.     aln = 0;
  154.     size = 0;
  155.     fldoff = 0;
  156.     for(;;) {
  157.         head = d_type();
  158.         if (head == NULL)
  159.             goto out;
  160.         if (ok_suh(head) == 0)
  161.             continue;
  162.     more:
  163.         xp = d_declr(head,0);
  164.         opt_field(xp,&fldw,isunion);
  165.         if (ok_sux(xp,head)) {
  166.             if (fldw > 0) { /* handle field */
  167.                 su_fld(&size,&aln,xp,fldw,&fldoff);
  168.                 xp->e_offs = size;
  169.             } else {        /* handle non-field */
  170.                 afterfld(&size,&fldoff);
  171.                 xp->e_offs = isunion ? 0 : size;
  172.                 su_size(&size,&aln,xp,isunion);
  173.             }
  174.             new_sym(listpp,xp);
  175.             listpp = &xp->n_next;
  176.         } else if (fldw == 0) {
  177.             afterfld(&size, &fldoff);
  178.         }
  179.  
  180.         if (cur->e_token == ',') {
  181.             fadvnode();
  182.             goto more;
  183.         }
  184.  
  185.         if (cur->e_token == ';') {
  186.             fadvnode();
  187.         } else
  188.             goto out;
  189.     }
  190. out:
  191.     afterfld(&size,&fldoff);
  192.     while (aln & size)
  193.         size++;
  194.     *sizep = size;
  195.     *alnp = aln;
  196.     return;
  197. }
  198.  
  199. /* Decls inside Enum */
  200. en_decls()
  201. {
  202.     register NODEP head, xp;
  203.     NODEP bas_type(), d_declr();
  204.     int curval = 0;
  205.  
  206.     for(;;) {
  207.         head = bas_type(K_INT);
  208.     more:
  209.         xp = d_declr(head,0);
  210.         if (ok_enx(xp,head)) {
  211.             opt_enval(&curval);
  212.             xp->e_ival = curval++;
  213.             xp->e_sc = ENUM_SC;
  214.             new_sym(level ? blktab->b_syms : (NODE *)symtab,
  215.                 xp);
  216.         }
  217.  
  218.         if (cur->e_token == ',') {
  219.             fadvnode();
  220.             goto more;
  221.         }
  222.  
  223.         return;
  224.     }
  225. }
  226.  
  227. /*
  228.  * called from expr.c, make a cast
  229.  * only called if is_ty_start();
  230.  */
  231. NODE *
  232. makecast()
  233. {
  234.     NODEP head, xp;
  235.     register NODEP np;
  236.     NODEP d_type(), d_declr(), def_type();
  237.  
  238.     head = d_type();        /* we know this is not NULL */
  239.     xp = d_declr(head, 1);
  240.     if (ok_cast(xp,head) == 0) {
  241.         xp = def_type();        /* return cast to INT */
  242.     }
  243.     np = allocnode();
  244.     np->e_token = TCONV;
  245.     np->n_tptr = xp;
  246.     if (xp == head)
  247.         np->n_flags |= N_COPYT;
  248.     if (debug) {
  249.         printf("Make cast");
  250.         printnode(np);
  251.     }
  252.     return np;
  253. }
  254.  
  255. is_ty_start()
  256. {
  257.     NODEP rv;
  258.  
  259.     if (is_tykw(cur->e_token))
  260.         return 1;
  261.     if (cur->e_token == ID) {
  262.         rv = allsyms(cur);
  263.         if (rv && rv->e_sc == K_TYPEDEF)
  264.             return 1;
  265.     }
  266.     return 0;
  267. }
  268.  
  269. /* assemble decl and put in listpp */
  270. new_sym(listpp, xp)
  271. NODEP *listpp;
  272. NODEP xp;
  273. {
  274.     NODEP old;
  275.  
  276.     if (xp == NULL)
  277.         return 0;
  278. /* put in table */
  279.     if (debug) {
  280.         printf("New sym sc %c", "EARTSCH"[xp->e_sc-K_EXTERN]);
  281.         printnode(xp);
  282.     }
  283.     /* later look for previous definition */
  284.     if (listpp == (NODE **)symtab) {
  285.         old = hlook(listpp, xp);
  286.         if (old == NULL || def2nd(old, xp))
  287.             puthlist(listpp, xp);
  288.     } else {
  289.         old = llook(*listpp, xp);
  290.         if (old == NULL || def2nd(old, xp))
  291.             putlist(listpp, xp);
  292.     }
  293.     return 1;
  294. }
  295.  
  296. /* look for storage class */
  297. d_scl(defau)
  298. {
  299.     int rv;
  300.  
  301.     if (is_sclass(cur->e_token)) {
  302.         rv = cur->e_token;
  303.         fadvnode();
  304.         return rv;
  305.     }
  306.     /* no storage class specified */
  307.     return defau;
  308. }
  309.  
  310. NODEP
  311. d_declr(head, forcast)
  312. NODEP head;
  313. {
  314.     NODEP e1;
  315.     NODEP declarator(), rev_decl();
  316.     NODEP xp, tailp;
  317.  
  318.     e1 = declarator();
  319.     xp = rev_decl(e1, &tailp, forcast);
  320.     if (xp) {
  321.         tailp->n_tptr = head;
  322.         tailp->n_flags |= N_COPYT;
  323.         return xp;
  324.     } else if (forcast)
  325.         return head;
  326.     else
  327.         return NULL;
  328. }
  329.  
  330. NODEP
  331. rev_decl(np,tailpp,forcast)
  332. NODEP np, *tailpp;
  333. {
  334.     NODEP rv, scan, nxt;
  335.  
  336.     rv = NULL;
  337.     for (scan = np; scan != NULL; scan = nxt) {
  338.         nxt = scan->n_next;
  339.         scan->n_next = NULL;
  340.         if (rv == NULL) {
  341.             *tailpp = scan;
  342.             scan->n_tptr = NULL;
  343.             rv = scan;
  344.         } else {
  345.             scan->n_tptr = rv;
  346.             rv = scan;
  347.         }
  348.         e_to_t(rv);
  349.         switch (rv->t_token) {
  350.         case UNARY '*':
  351.             sprintf(rv->n_name, "Ptr to");
  352.             break;
  353.         case '(':
  354.             sprintf(rv->n_name, "Fun ret");
  355.             break;
  356.         case '[':
  357.             sprintf(rv->n_name, "Ary of");
  358.             break;
  359.         case ID:
  360.             break;
  361.         default:
  362.             error("bad type xpr");
  363.             return NULL;
  364.         }
  365.     }
  366.     /* if normal decl and see something, must see id first */
  367.     if (!ok_revx(rv,forcast))
  368.         rv = NULL;
  369.     return rv;
  370. }
  371.  
  372. /*
  373.  * Looking for type part of a decl
  374.  */
  375. NODEP
  376. d_type()
  377. {
  378.     int btype, adj;
  379.     NODEP rv;
  380.     NODEP bas_type(), decl_su(), decl_enum();
  381.  
  382.     /* look for 'struct', 'union', 'enum' or typedef ID */
  383.     switch (cur->e_token) {
  384.     case ID:
  385.         rv = allsyms(cur);
  386.         if (rv && rv->e_sc == K_TYPEDEF) {
  387.             fadvnode();
  388.             rv = rv->n_tptr;
  389.             return rv;
  390.         }
  391.         return NULL;
  392.     case K_STRUCT:
  393.         return decl_su(0);
  394.     case K_UNION:
  395.         return decl_su(1);
  396.     case K_ENUM:
  397.         return decl_enum();
  398.     }
  399.  
  400.     /* look for modifiers 'long', 'short', 'unsigned' */
  401.     adj = 0;
  402.     while (is_tadj(cur->e_token)) {
  403.         switch (cur->e_token) {
  404.         case K_SHORT:
  405.             adj |= SAW_SHORT;
  406.             break;
  407.         case K_LONG:
  408.             adj |= SAW_LONG;
  409.             break;
  410.         case K_UNSIGNED:
  411.             adj |= SAW_UNS;
  412.             break;
  413.         }
  414.         fadvnode();
  415.     }
  416.  
  417.     /* look for base type 'char', 'int', 'float', 'double', 'void'*/
  418.     if (is_btype(cur->e_token)) {
  419.         btype = cur->e_token;
  420.         fadvnode();
  421.     } else if (adj == 0)    /* saw nothing */
  422.         return NULL;
  423.     else
  424.         btype = K_INT;
  425.  
  426.     if (adj)
  427.         btype = adj_type(btype, adj);
  428.     rv = bas_type(btype);
  429.     return rv;
  430. }
  431.  
  432. NODEP
  433. decl_enum()
  434. {
  435.     NODEP bas_type();
  436.  
  437.     fadvnode();     /* skip 'enum' */
  438.  
  439.     if (cur->e_token == ID) {       /* ignore tag */
  440.         fadvnode();
  441.     }
  442.     if (cur->e_token == '{') {      /* new declaration */
  443.         fadvnode();     /* skip '{' */
  444.         en_decls();     /* global scope */
  445.         if (cur->e_token != '}')
  446.             error("expect }");
  447.         else
  448.             fadvnode();     /* skip '}' */
  449.     }
  450.     return bas_type(K_INT);
  451. }
  452.  
  453. extern lineno;
  454.  
  455. NODEP
  456. decl_su(isunion)
  457. {
  458.     register NODEP rv, tagp;
  459.     NODEP *attab;
  460.  
  461.     fadvnode();     /* skip 'struct' or 'union' */
  462.  
  463.     attab = level ? &blktab->b_tags : &tagtab;
  464.     tagp = NULL;
  465.     if (cur->e_token == ID) {       /* hold on to ID node */
  466.         tagp = cur;
  467.         e_to_t(tagp);
  468.         advnode();
  469.         nnmadd(tagp, isunion ? ".U" : ".S");
  470.     }
  471.     if (cur->e_token == '{') {      /* new declaration */
  472.         if (tagp == NULL) {     /* make fake name */
  473.             tagp = allocnode();
  474.             sprintf(tagp->n_name, isunion ? "%dU" :
  475.                     "%dS", lineno);
  476.         }
  477.         fadvnode();     /* skip '{' */
  478.         if (rv = llook(*attab, tagp)) {
  479.             freenode(tagp);
  480.             if (rv->n_right) {
  481.                 errors("struct redefined", rv->n_name);
  482.                 freenode(rv->n_right);
  483.                 rv->n_right = NULL;
  484.             }
  485.         } else {        /* new defn */
  486.             rv = tagp;
  487.             rv->t_token = isunion ? K_UNION : K_STRUCT;
  488.             rv->n_flags |= N_BRKPR; /* break print loops */
  489.             putlist(attab, rv);
  490.         }
  491.         su_decls(&rv->n_right, isunion,
  492.                 &rv->t_size, &rv->t_aln);
  493.         if (cur->e_token != '}')
  494.             error("expect }");
  495.         else
  496.             fadvnode();     /* skip '}' */
  497.     } else {        /* reference to old */
  498.         if (tagp == NULL) {
  499.             error("nonsense struct");
  500.             goto out;
  501.         }
  502.         /* ANSI special decl
  503.             struct <tag> ;
  504.            for hiding old tag within block */
  505.         if (cur->e_token == ';' && level)
  506.             rv = llook(*attab, tagp);
  507.         else
  508.             rv = alltags(tagp);
  509.         if (rv == NULL) {       /* delayed tag */
  510.             rv = tagp;
  511.             rv->t_token = isunion ? K_UNION : K_STRUCT;
  512.             rv->n_flags |= N_BRKPR; /* break print loops */
  513.             putlist(attab, rv);
  514.             goto out;
  515.         } else
  516.             freenode(tagp);
  517.     }
  518. out:
  519.     return rv;
  520. }
  521.  
  522. NODE *
  523. alltags(np)
  524. NODE *np;
  525. {
  526.     register NODE *bp;
  527.     NODE *rv;
  528.  
  529.     for (bp=blktab; bp != NULL; bp = bp->n_next)
  530.         if ((rv = llook(bp->b_tags, np)) != NULL)
  531.             return rv;
  532.     return llook(tagtab, np);
  533. }
  534.  
  535. NODE *
  536. allsyms(np)
  537. NODE *np;
  538. {
  539.     register NODE *bp;
  540.     NODE *rv;
  541.  
  542.     for (bp=blktab; bp != NULL; bp = bp->n_next)
  543.         if ((rv = llook(bp->b_syms, np)) != NULL)
  544.             return rv;
  545.     return hlook(symtab, np);
  546. }
  547.  
  548. sim_type(a,b)
  549. register NODE *a, *b;
  550. {
  551. more:
  552.     if (a == b)
  553.         return 1;
  554.     if (a == NULL || b == NULL)
  555.         return 0;
  556.     if (a->t_token != b->t_token)
  557.         return 0;
  558.     if (a->t_size != b->t_size && a->t_size && b->t_size)
  559.         return 0;
  560.     a = a->n_tptr;
  561.     b = b->n_tptr;
  562.     goto more;
  563. }
  564.  
  565. /* 2nd def of same name at same level */
  566. /* OK if one extern and types the same */
  567. def2nd(old,new)
  568. NODEP old, new;
  569. {
  570.     int osc, nsc;
  571.  
  572.     if (sim_type(old->n_tptr, new->n_tptr) == 0)
  573.         goto bad;
  574.     osc = old->e_sc;
  575.     nsc = new->e_sc;
  576.     if (nsc == K_EXTERN) {  /* works only if no further use allowed */
  577.         freenode(new);
  578.         return 0;
  579.     }
  580.     if (osc == K_EXTERN) {
  581.         /* replace old def with new one */
  582.         /* for now, just put new one on list too */
  583.         if ( nsc == K_STATIC )
  584.             old->n_flags &= ~(N_XREF|N_XDEF);
  585.         return 1;
  586.     }
  587. bad:
  588.     errorn("bad 2nd decl of ", new);
  589.     /* use 2nd def so other stuff works */
  590.     return 1;
  591. }
  592.  
  593. /* saw fun but no body */
  594. static
  595. fix_fun(np)
  596. NODE *np;
  597. {
  598.     if (np == NULL) return;
  599.     if (np->n_tptr->t_token == '(') {       /* fix to extern */
  600.         if (np->e_sc != K_TYPEDEF){
  601.             np->e_sc = K_EXTERN;
  602.             np->n_flags |= N_XREF;
  603.             np->n_flags &= ~N_XDEF;
  604.         }
  605.     }
  606. }
  607.  
  608. e_to_t(np)
  609. NODE *np;
  610. {
  611.     int token;
  612.  
  613.     token = np->e_token;
  614.     np->t_token = token;
  615.     np->t_size = 0;
  616.     np->t_aln = 0;
  617. }
  618.