home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / bsd_srcs / usr.bin / pascal / src / yyid.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-16  |  6.9 KB  |  291 lines

  1. /*-
  2.  * Copyright (c) 1980 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the University of
  16.  *    California, Berkeley and its contributors.
  17.  * 4. Neither the name of the University nor the names of its contributors
  18.  *    may be used to endorse or promote products derived from this software
  19.  *    without specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. static char sccsid[] = "@(#)yyid.c    5.3 (Berkeley) 4/16/91";
  36. #endif /* not lint */
  37.  
  38. #include "whoami.h"
  39. #include <0.h>
  40. #include "tree_ty.h"    /* must be included for yy.h */
  41. #include "yy.h"
  42.  
  43. #ifdef PI
  44. extern    union semstack *yypv;
  45. /*
  46.  * Determine whether the identifier whose name
  47.  * is "cp" can possibly be a kind, which is a
  48.  * namelist class.  We look through the symbol
  49.  * table for the first instance of cp as a non-field,
  50.  * and at all instances of cp as a field.
  51.  * If any of these are ok, we return true, else false.
  52.  * It would be much better to handle with's correctly,
  53.  * even to just know whether we are in a with at all.
  54.  *
  55.  * Note that we don't disallow constants on the lhs of assignment.
  56.  */
  57. identis(cp, kind)
  58.     register char *cp;
  59.     int kind;
  60. {
  61.     register struct nl *p;
  62.     int i;
  63.  
  64.     /*
  65.      * Cp is NIL when error recovery inserts it.
  66.      */
  67.     if (cp == NIL)
  68.         return (1);
  69.  
  70.     /*
  71.      * Record kind we want for possible later use by yyrecover
  72.      */
  73.     yyidwant = kind;
  74.     yyidhave = NIL;
  75.     i = ( (int) cp ) & 077;
  76.     for (p = disptab[i]; p != NIL; p = p->nl_next)
  77.         if (p->symbol == cp) {
  78.             if (yyidok(p, kind))
  79.                 goto gotit;
  80.             if (p->class != FIELD && p->class != BADUSE)
  81.                 break;
  82.         }
  83.     if (p != NIL)
  84.         for (p = p->nl_next; p != NIL; p = p->nl_next)
  85.             if (p->symbol == cp && p->class == FIELD && yyidok(p, kind))
  86.                 goto gotit;
  87.     return (0);
  88. gotit:
  89.     if (p->class == BADUSE && !Recovery) {
  90.         yybadref(p, OY.Yyeline);
  91.         yypv[0].i_entry = NIL;
  92.     }
  93.     return (1);
  94. }
  95.  
  96. /*
  97.  * A bad reference to the identifier cp on line
  98.  * line and use implying the addition of kindmask
  99.  * to the mask of kind information.
  100.  */
  101. struct nl *
  102. yybaduse(cp, line, kindmask)
  103.     register char *cp;
  104.     int line, kindmask;
  105. {
  106.     register struct nl *p, *oldp;
  107.     int i;
  108.  
  109.     i = ( (int) cp ) & 077;
  110.     for (p = disptab[i]; p != NIL; p = p->nl_next)
  111.         if (p->symbol == cp)
  112.             break;
  113.     oldp = p;
  114.     if (p == NIL || p->class != BADUSE)
  115.         p = enter(defnl(cp, BADUSE, NLNIL, 0));
  116.     p->value[NL_KINDS] |= kindmask;
  117.     yybadref(p, line);
  118.     return (oldp);
  119. }
  120.  
  121.     /*
  122.      *    ud is initialized so that esavestr will allocate
  123.      *    sizeof ( struct udinfo ) bytes for the 'real' struct udinfo
  124.      */
  125. struct    udinfo ud = { ~0 , (struct udinfo *) ~0 , 0};
  126. /*
  127.  * Record a reference to an undefined identifier,
  128.  * or one which is improperly used.
  129.  */
  130. yybadref(p, line)
  131.     register struct nl *p;
  132.     int line;
  133. {
  134.     register struct udinfo *udp;
  135.  
  136.     if (p->chain != NIL && ((struct udinfo *) p->chain)->ud_line == line)
  137.         return;
  138.     udp = (struct udinfo *) esavestr((char *) &ud);
  139.     udp->ud_line = line;
  140.     udp->ud_next = (struct udinfo *) p->chain;
  141.     p->chain = (struct nl *) udp;
  142. }
  143.  
  144. #define    varkinds    ((1<<CONST)|(1<<VAR)|(1<<REF)|(1<<ARRAY)|(1<<PTR) \
  145.             |(1<<RECORD)|(1<<FIELD)|(1<<FUNC)|(1<<FVAR) \
  146.             |(1<<FFUNC)|(1<<PROC)|(1<<FPROC))
  147. /*
  148.  * Is the symbol in the p entry of the namelist
  149.  * even possibly a kind kind?  If not, update
  150.  * what we have based on this encounter.
  151.  */
  152. yyidok(p, kind)
  153.     register struct nl *p;
  154.     int kind;
  155. {
  156.  
  157.     if (p->class == BADUSE) {
  158.         if (kind == VAR)
  159.             return (p->value[0] & varkinds);
  160.         return (p->value[0] & (1 << kind));
  161.     }
  162.     if (yyidok1(p, kind))
  163.         return (1);
  164.     if (yyidhave != NIL)
  165.         yyidhave = IMPROPER;
  166.     else
  167.         yyidhave = p->class;
  168.     return (0);
  169. }
  170.  
  171. yyidok1(p, kind)
  172.     register struct nl *p;
  173.     int kind;
  174. {
  175.  
  176.     switch (kind) {
  177.         default:
  178.         case FUNC:
  179.             return (   p -> class == FUNC
  180.                 || p -> class == FVAR
  181.                 || p -> class == FFUNC );
  182.         case PROC:
  183.             return ( p -> class == PROC || p -> class == FPROC );
  184.         case CONST:
  185.         case TYPE:
  186.         case FIELD:
  187.             return (p->class == kind);
  188.         case VAR:
  189.             return (p->class == CONST || yyisvar(p, NIL));
  190.         case ARRAY:
  191.         case RECORD:
  192.             return (yyisvar(p, kind));
  193.         case PTRFILE:
  194.             return (yyisvar(p, PTR) || yyisvar(p, FILET));
  195.     }
  196. }
  197.  
  198. yyisvar(p, varclass)
  199.     register struct nl *p;
  200.     int varclass;
  201. {
  202.  
  203.     switch (p->class) {
  204.         case FIELD:
  205.         case VAR:
  206.         case REF:
  207.         case FVAR:
  208.         /*
  209.          * We would prefer to return
  210.          * parameterless functions only.
  211.          */
  212.         case FUNC:
  213.         case FFUNC:
  214.             return (varclass == NIL || (p->type != NIL && p->type->class == varclass));
  215.         case PROC:
  216.         case FPROC:
  217.             return ( varclass == NIL );
  218.     }
  219.     return (0);
  220. }
  221. #endif
  222. #ifdef PXP
  223. #ifndef DEBUG
  224. identis()
  225. {
  226.  
  227.     return (1);
  228. }
  229. #endif
  230. #ifdef DEBUG
  231. extern    char *classes[];
  232.  
  233. char    kindchars[]    "UCTVAQRDPF";
  234. /*
  235.  * Fake routine "identis" for pxp when testing error recovery.
  236.  * Looks at letters in variable names to answer questions
  237.  * about attributes.  Mapping is
  238.  *    C    const_id
  239.  *    T    type_id
  240.  *    V    var_id        also if any of AQRDF
  241.  *    A    array_id
  242.  *    Q    ptr_id
  243.  *    R    record_id
  244.  *    D    field_id    D for "dot"
  245.  *    P    proc_id
  246.  *    F    func_id
  247.  */
  248. identis(cp, kind)
  249.     register char *cp;
  250.     int kind;
  251. {
  252.     register char *dp;
  253.     char kindch;
  254.  
  255.     /*
  256.      * Don't do anything unless -T
  257.      */
  258.     if (!typetest)
  259.         return (1);
  260.  
  261.     /*
  262.      * Inserted symbols are always correct
  263.      */
  264.     if (cp == NIL)
  265.         return (1);
  266.     /*
  267.      * Set up the names for error messages
  268.      */
  269.     yyidwant = classes[kind];
  270.     for (dp = kindchars; *dp; dp++)
  271.         if (any(cp, *dp)) {
  272.             yyidhave = classes[dp - kindchars];
  273.             break;
  274.         }
  275.  
  276.     /*
  277.      * U in the name means undefined
  278.      */
  279.     if (any(cp, 'U'))
  280.         return (0);
  281.  
  282.     kindch = kindchars[kind];
  283.     if (kindch == 'V')
  284.         for (dp = "AQRDF"; *dp; dp++)
  285.             if (any(cp, *dp))
  286.                 return (1);
  287.     return (any(cp, kindch));
  288. }
  289. #endif
  290. #endif
  291.