Make declarator parsing use only detailed types (just fill in old type at end)
authorNick Downing <downing.nick@gmail.com>
Wed, 15 Feb 2017 09:19:24 +0000 (20:19 +1100)
committerNick Downing <downing.nick@gmail.com>
Wed, 15 Feb 2017 09:19:24 +0000 (20:19 +1100)
c03.c

diff --git a/c03.c b/c03.c
index dbf0d83..21e4acb 100644 (file)
--- a/c03.c
+++ b/c03.c
@@ -394,16 +394,25 @@ static int oldtype(dtype, dimp, strp) struct type *dtype; struct tdim *dimp; str
  /*fprintf(stderr, ")", type);*/
  return type;
 }
+static int eqtype(struct type *t0, struct type *t1) {
+ while (t0->t_id == t1->t_id) {
+  if (t0->t_id < PTR)
+   return 1;
+  t0 = ((struct ptype *)t0)->pt_reftype;
+  t1 = ((struct ptype *)t1)->pt_reftype;
+ }
+ return 0;
+}
 int decl1(askw, atptr, offset, absname) int askw; struct /*nmlist*/type *atptr; int offset; struct nmlist *absname; {
        int t1, a, elsize;
        register int skw;
- struct type *dtype;
-       int type;
+ struct type *dtype, *dt, *dt1;
+ /*    int type;*/
        register struct nmlist *dsym;
        register struct /*nmlist*/type *tptr;
        struct tdim dim;
- struct SS *strp;
-       int *dp;
/*struct SS *strp;
+       int *dp;*/
        int isinit;
 
        skw = askw;
@@ -451,10 +460,6 @@ int decl1(askw, atptr, offset, absname) int askw; struct /*nmlist*/type *atptr;
                t1 >>= TYLEN;
        }
        type |= tptr->nl_type&TYPE;*/
- dim.rank = 0;
- /*fprintf(stderr, "oldtype ");*/
- type = oldtype(dtype, &dim, &strp);
- /*fprintf(stderr, "\n");*/
        if (/*(type&XTYPE)*/dtype->t_id == FUNC) {
                if (skw==AUTO)
                        skw = EXTERN;
@@ -486,58 +491,70 @@ int decl1(askw, atptr, offset, absname) int askw; struct /*nmlist*/type *atptr;
                } else
                        defsym = dsym = pushdecl(dsym);
        }
- /* XXX change all of this stuff to recursively compare types */
-       if (dim.rank == 0)
+ /*    if (dim.rank == 0)
                dsym->nl_subsp = NULL;
-       else {
+       else {*/
                /*
                 * If an array is declared twice, make sure the declarations
                 * agree in dimension.  This happens typically when a .h
                 * and .c file both declare a variable.
                 */
-               if (dsym->nl_subsp) {
-                       for (a=0, t1 = dsym->nl_type;
-                           a<dim.rank && (t1&XTYPE)==ARRAY;
-                           a++, t1 >>= TYLEN)
+               if (dsym->/*nl_subsp*/nl_dtype) {
+                       for (/*a=0*/dt = dtype, /*t1 = dsym->nl_type*/dt1 = dsym->nl_dtype;
+                           /*a<dim.rank*/dt->t_id==ARRAY && /*(t1&XTYPE)*/dt1->t_id==ARRAY;
+                           /*a++*/dt=((struct atype *)dt)->at_reftype, /*t1 >>= TYLEN*/dt1=((struct atype *)dt1)->at_reftype)
+#define adt ((struct atype *)dt)
+#define adt1 ((struct atype *)dt1)
                                /*
                                 * If we haven't seen a declaration for this
                                 * dimension yet, take what's been given now.
                                 */
-                               if (!dsym->nl_subsp[a])
-                                       dsym->nl_subsp[a] = dim.dimens[a];
-                               else if (dim.dimens[a]
-                                   && dim.dimens[a] != dsym->nl_subsp[a])
+                               if (!/*dsym->nl_subsp[a]*/adt1->at_nelt)
+                                       /*dsym->nl_subsp[a]*/adt1->at_nelt = /*dim.dimens[a]*/adt->at_nelt;
+                               else if (/*dim.dimens[a]*/adt->at_nelt
+                                   && /*dim.dimens[a]*/adt->at_nelt != /*dsym->nl_subsp[a]*/adt1->at_nelt)
                                        redec();
-                       if (a<dim.rank || (t1&XTYPE)==ARRAY)
+#undef adt
+#undef adt1
+                       if (/*a<dim.rank*/dt->t_id==ARRAY || /*(t1&XTYPE)*/dt1->t_id==ARRAY)
                                redec();
-               } else {
+               } /*else {
                        dp = (int *)Dblock(dim.rank*sizeof(dim.rank));
                        for (a=0; a<dim.rank; a++)
                                dp[a] = dim.dimens[a];
                        dsym->nl_subsp = dp;
                }
-       }
+       }*/
        if (!(dsym->nl_class==0
           || ((skw==ARG||skw==AREG) && dsym->nl_class==ARG1)
-          || (skw==EXTERN && dsym->nl_class==EXTERN && dsym->nl_type==type))) {
+          || (skw==EXTERN && dsym->nl_class==EXTERN && /*dsym->nl_type==type*/eqtype(dsym->nl_dtype, dtype)))) {
                redec();
                goto syntax;
        }
      if (dsym->nl_class && (dsym->nl_type&TYPE)==STRUCT && (type&TYPE)==STRUCT)
-               if (dsym->nl_strp != /*tptr->nl_strp*/(union str *)&((struct stype *)tptr)->st_S) {
/*    if (dsym->nl_class && (dsym->nl_type&TYPE)==STRUCT && (type&TYPE)==STRUCT)
+               if (dsym->nl_strp != *//*tptr->nl_strp*//*(union str *)&((struct stype *)tptr)->st_S) {
                        error0("structure redeclaration");
-               }
- /* to here */
- /*fprintf(stderr, "dsym %s class %08x type %08x\n", dsym->nl_name, skw, type);
- fprintf(stderr, "rank %d", dim.rank);
+               }*/
+ if (dsym->nl_class) {
+  for (dt = dtype; dt->t_id >= PTR; dt = ((struct ptype *)dt)->pt_reftype)
+   ;
+  for (dt1 = dsym->nl_dtype; dt1->t_id >= PTR; dt1 = ((struct ptype *)dt1)->pt_reftype)
+   ;
+  if (dt->t_id == STRUCT && dt1->t_id == STRUCT && dt != dt1)
+   error0("structure redeclaration");
+ }
+ /*    dsym->nl_type = type;
+       if (tptr->nl_strp)
+               dsym->nl_strp = tptr->nl_strp;*/
+ dim.rank = 0;
+ /*fprintf(stderr, "oldtype ");*/
+ dsym->nl_type = oldtype(dtype, &dim, &dsym->nl_strp);
+ /*fprintf(stderr, "\n");*/
+ if (dim.rank && dsym->nl_subsp == 0)
+  dsym->nl_subsp = (int *)Dblock(dim.rank*sizeof(dim.rank));
  for (a=0; a<dim.rank; a++)
-  fprintf(stderr, " %d", dim.dimens[a]);
- fprintf(stderr, "\n");*/
-       dsym->nl_type = type;
+  dsym->nl_subsp[a] = dim.dimens[a];
  dsym->nl_dtype = dtype;
- /*    if (tptr->nl_strp)
-               dsym->nl_strp = tptr->nl_strp;*/
- dsym->nl_strp = (union str *)strp;
        if (skw==TYPEDEF) {
                dsym->nl_class = TYPEDEF;
                return(0);
@@ -626,7 +643,7 @@ int decl1(askw, atptr, offset, absname) int askw; struct /*nmlist*/type *atptr;
                cinit(dsym, 0, REG);
                isinit = 0;
        } else if (skw==ENUM) {
-               if (type!=INT)
+               if (/*type*/dtype->t_id!=INT)
                        error0("Illegal enumeration %s", dsym->nl_name);
                dsym->nl_class = ENUMCON;
                dsym->nl_offset = offset;
@@ -655,6 +672,7 @@ struct nmlist *pushdecl(sp) register struct nmlist *sp; {
        nsp->nl_class = 0;
        nsp->nl_flag &= FKIND;
        nsp->nl_type = 0;
+ nsp->nl_dtype = NULL;
        nsp->nl_offset = 0;
        nsp->nl_blklev = blklev;
        nsp->nl_strp = NULL;
@@ -926,6 +944,7 @@ void decsyn(o) int o; {
  */
 void redec() {
        error0("%s redeclared", defsym->nl_name);
+ abort();
 }
 
 /*