home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / historic / v941.tgz / icon.v941src.tar / icon.v941src / src / preproc / pmem.c < prev    next >
C/C++ Source or Header  |  2001-12-12  |  6KB  |  340 lines

  1. /*
  2.  * This file does most of the memory management.
  3.  */
  4.  
  5. #include "../preproc/preproc.h"
  6. #include "../preproc/ptoken.h"
  7.  
  8. struct src *src_stack = NULL;  /* stack of token sources */
  9.  
  10. #include "../preproc/pproto.h"
  11.  
  12. /*
  13.  * new_macro - allocate a new entry for the macro symbol table.
  14.  */
  15. struct macro *new_macro(mname, category, multi_line, prmlst, body)
  16. char *mname;
  17. int category;
  18. int multi_line;
  19. struct id_lst *prmlst;
  20. struct tok_lst *body;
  21.    {
  22.    struct macro *mp;
  23.  
  24.    mp = NewStruct(macro);
  25.    mp->mname = mname;
  26.    mp->category = category;
  27.    mp->multi_line = multi_line;
  28.    mp->prmlst = prmlst;
  29.    mp->body = body;
  30.    mp->ref_cnt = 1;
  31.    mp->recurse = 0;
  32.    mp->next = NULL;
  33.    return mp;
  34.    }
  35.  
  36. /*
  37.  * new_token - allocate a new token.
  38.  */
  39. struct token *new_token(id, image, fname, line)
  40. int id;
  41. char *image;
  42. char *fname;
  43. int line;
  44.    {
  45.    struct token *t;
  46.  
  47.    t = NewStruct(token);
  48.    t->tok_id = id;
  49.    t->image = image;
  50.    t->fname = fname;
  51.    t->line = line;
  52.    t->flag = 0;
  53.    return t;
  54.    }
  55.  
  56. /*
  57.  * copy_t - make a copy of a token.
  58.  */
  59. struct token *copy_t(t)
  60. struct token *t;
  61.    {
  62.    struct token *t1;
  63.  
  64.    if (t == NULL)
  65.       return NULL;
  66.  
  67.    t1 = NewStruct(token);
  68.    *t1 = *t;
  69.    return t1;
  70.    }
  71.  
  72. /*
  73.  * new_t_lst - allocate a new element for a token list.
  74.  */
  75. struct tok_lst *new_t_lst(tok)
  76. struct token *tok;
  77.    {
  78.    struct tok_lst *tlst;
  79.  
  80.    tlst = NewStruct(tok_lst);
  81.    tlst->t = tok;
  82.    tlst->next = NULL;
  83.    return tlst;
  84.    }
  85.  
  86. /*
  87.  * new_id_lst - allocate a new element for an identifier list.
  88.  */
  89. struct id_lst *new_id_lst(id)
  90. char *id;
  91.    {
  92.    struct id_lst *ilst;
  93.  
  94.    ilst = NewStruct(id_lst);
  95.    ilst->id = id;
  96.    ilst->next = NULL;
  97.    return ilst;
  98.    }
  99.  
  100. /*
  101.  * new_cs - allocate a new structure for a source of tokens created from
  102.  *  characters.
  103.  */
  104. struct char_src *new_cs(fname, f, bufsize)
  105. char *fname;
  106. FILE *f;
  107. int bufsize;
  108.    {
  109.    struct char_src *cs;
  110.  
  111.    cs = NewStruct(char_src);
  112.    cs->char_buf = alloc(bufsize * sizeof(int));
  113.    cs->line_buf = alloc(bufsize * sizeof(int));
  114.    cs->bufsize = bufsize;
  115.    cs->fname = fname;
  116.    cs->f = f;
  117.    cs->line_adj = 0;
  118.    cs->tok_sav = NULL;
  119.    cs->dir_state = CanStart;
  120.  
  121.    return cs;
  122.    }
  123.  
  124. /*
  125.  * new_me - allocate a new structure for a source of tokens derived
  126.  *  from macro expansion.
  127.  */
  128. struct mac_expand *new_me(m, args, exp_args)
  129. struct macro *m;
  130. struct tok_lst **args;
  131. struct tok_lst **exp_args;
  132.    {
  133.    struct mac_expand *me;
  134.  
  135.    me = NewStruct(mac_expand);
  136.    me->m = m;
  137.    me->args = args;
  138.    me->exp_args = exp_args;
  139.    me->rest_bdy = m->body;
  140.    return me;
  141.    }
  142.  
  143. /*
  144.  * new_plsts - allocate a element for a list of token lists used as
  145.  *  as source of tokens derived from a sequence of token pasting
  146.  *  operations.
  147.  */
  148. struct paste_lsts *new_plsts(trigger, tlst, plst)
  149. struct token *trigger;
  150. struct tok_lst *tlst;
  151. struct paste_lsts *plst;
  152.    {
  153.    struct paste_lsts *plsts;
  154.  
  155.    plsts = NewStruct(paste_lsts);
  156.    plsts->trigger = trigger;
  157.    plsts->tlst = tlst;
  158.    plsts->next = plst;
  159.    return plsts;
  160.    }
  161.  
  162. /*
  163.  * get_sbuf - dynamically allocate a string buffer.
  164.  */
  165. struct str_buf *get_sbuf()
  166.    {
  167.    struct str_buf *sbuf;
  168.  
  169.    sbuf = NewStruct(str_buf);
  170.    init_sbuf(sbuf);
  171.    return sbuf;
  172.    }
  173.  
  174. /*
  175.  * push_src - push an entry on the stack of tokens sources. This entry
  176.  *  becomes the current source.
  177.  */
  178. void push_src(flag, ref)
  179. int flag;
  180. union src_ref *ref;
  181.    {
  182.    struct src *sp;
  183.  
  184.    sp = NewStruct(src);
  185.    sp->flag = flag;
  186.    sp->cond = NULL;
  187.    sp->u = *ref;
  188.    sp->ntoks = 0;
  189.  
  190.    if (src_stack->flag == CharSrc)
  191.       src_stack->u.cs->next_char = next_char;
  192.    sp->next = src_stack;
  193.    src_stack = sp;
  194.    }
  195.  
  196. /*
  197.  * free_t - free a token.
  198.  */
  199. void free_t(t)
  200. struct token *t;
  201.    {
  202.    if (t != NULL)
  203.       free((char *)t);
  204.    }
  205.  
  206. /*
  207.  * free_t_lst - free a token list.
  208.  */
  209. void free_t_lst(tlst)
  210. struct tok_lst *tlst;
  211.    {
  212.    if (tlst == NULL)
  213.       return;
  214.    free_t(tlst->t);
  215.    free_t_lst(tlst->next);
  216.    free((char *)tlst);
  217.    }
  218.  
  219. /*
  220.  * free_id_lst - free an identifier list.
  221.  */
  222. void free_id_lst(ilst)
  223. struct id_lst *ilst;
  224.    {
  225.    if (ilst == NULL)
  226.        return;
  227.    free_id_lst(ilst->next);
  228.    free((char *)ilst);
  229.    }
  230.  
  231. /*
  232.  * free_m - if there are no more pointers to this macro entry, free it
  233.  *  and other associated storage.
  234.  */
  235. void free_m(m)
  236. struct macro *m;
  237.    {
  238.    if (--m->ref_cnt != 0)
  239.       return;
  240.    free_id_lst(m->prmlst);
  241.    free_t_lst(m->body);
  242.    free((char *)m);
  243.    }
  244.  
  245. /*
  246.  * free_m_lst - free a hash chain of macro symbol table entries.
  247.  */
  248. void free_m_lst(m)
  249. struct macro *m;
  250.    {
  251.    if (m == NULL)
  252.       return;
  253.    free_m_lst(m->next);
  254.    free_m(m);
  255.    }
  256.  
  257. /*
  258.  * free_plsts - free an entry from a list of token lists used in
  259.  *  token pasting.
  260.  */
  261. void free_plsts(plsts)
  262. struct paste_lsts *plsts;
  263.    {
  264.    free((char *)plsts);
  265.    }
  266.  
  267. /*
  268.  * rel_sbuf - free a string buffer.
  269.  */
  270. void rel_sbuf(sbuf)
  271. struct str_buf *sbuf;
  272.    {
  273.    free((char *)sbuf);
  274.    }
  275.  
  276. /*
  277.  * pop_src - pop the top entry from the stack of tokens sources.
  278.  */
  279. void pop_src()
  280.    {
  281.    struct src *sp;
  282.    struct char_src *cs;
  283.    struct mac_expand *me;
  284.    int i;
  285.  
  286.    if (src_stack->flag == DummySrc)
  287.       return; /* bottom of stack */
  288.  
  289.    sp = src_stack;
  290.    src_stack = sp->next; /* pop */
  291.  
  292.    /*
  293.     * If the new current source is a character source, reload global
  294.     *  variables used in tokenizing the characters.
  295.     */
  296.    if (src_stack->flag == CharSrc) {
  297.       first_char = src_stack->u.cs->char_buf;
  298.       next_char = src_stack->u.cs->next_char;
  299.       last_char = src_stack->u.cs->last_char;
  300.       }
  301.  
  302.    /*
  303.     * Make sure there is no unclosed conditional compilation in the
  304.     *  source we are poping.
  305.     */
  306.    if (sp->cond != NULL)
  307.       errt2(sp->cond->t, "no matching #endif for #", sp->cond->t->image);
  308.  
  309.    /*
  310.     * Free any storage that the stack entry still references.
  311.     */
  312.    switch (sp->flag) {
  313.       case CharSrc:
  314.          cs = sp->u.cs;
  315.          if (cs->f != NULL)
  316.             fclose(cs->f);
  317.          free((char *)cs);
  318.          break;
  319.       case MacExpand:
  320.          me = sp->u.me;
  321.          if (me->args != NULL) {
  322.             for (i = 0; i < me->m->category; i++) {
  323.                free_t_lst(me->args[i]);
  324.                free_t_lst(me->exp_args[i]);
  325.                }
  326.             free((char *)me->args);
  327.             free((char *)me->exp_args);
  328.             }
  329.          --me->m->recurse;
  330.          free_m(me->m);
  331.          free((char *)me);
  332.          break;
  333.       }
  334.  
  335.    /*
  336.     * Free the stack entry.
  337.     */
  338.    free((char *)sp);
  339.    }
  340.