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