home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 1 / crawlyvol1.bin / program / compiler / sozobon / scsrc20 / hcc / decl.c < prev    next >
C/C++ Source or Header  |  1991-02-22  |  12KB  |  606 lines

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