Main Page   Alphabetical List   Compound List   File List   Compound Members   File Members  

exprNodeList.c

Go to the documentation of this file.
00001 /*
00002 ** LCLint - annotation-assisted static program checker
00003 ** Copyright (C) 1994-2000 University of Virginia,
00004 **         Massachusetts Institute of Technology
00005 **
00006 ** This program is free software; you can redistribute it and/or modify it
00007 ** under the terms of the GNU General Public License as published by the
00008 ** Free Software Foundation; either version 2 of the License, or (at your
00009 ** option) any later version.
00010 ** 
00011 ** This program is distributed in the hope that it will be useful, but
00012 ** WITHOUT ANY WARRANTY; without even the implied warranty of
00013 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 ** General Public License for more details.
00015 ** 
00016 ** The GNU General Public License is available from http://www.gnu.org/ or
00017 ** the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
00018 ** MA 02111-1307, USA.
00019 **
00020 ** For information on lclint: lclint-request@cs.virginia.edu
00021 ** To report a bug: lclint-bug@cs.virginia.edu
00022 ** For more information: http://lclint.cs.virginia.edu
00023 */
00024 /*
00025 ** exprNodeList.c
00026 **
00027 ** based on list_template.c
00028 **
00029 ** where T has T_equal (or change this) and T_unparse
00030 */
00031 
00032 # include "lclintMacros.nf"
00033 # include "basic.h"
00034 
00035 /*@only@*/ exprNodeList
00036 exprNodeList_new ()
00037 {
00038   exprNodeList s = (exprNodeList) dmalloc (sizeof (*s));
00039   
00040   s->nelements = 0;
00041   s->nspace = exprNodeListBASESIZE; 
00042   s->elements = (exprNode *)
00043     dmalloc (sizeof (*s->elements) * exprNodeListBASESIZE);
00044   s->current = 0;
00045 
00046     return (s);
00047 }
00048 
00049 static void
00050 exprNodeList_grow (exprNodeList s)
00051 {
00052   int i;
00053   exprNode *newelements; 
00054   int numnew;
00055 
00056   if (s->nelements < exprNodeListBASESIZE)
00057     {
00058       numnew = exprNodeListBASESIZE;
00059     }
00060   else
00061     {
00062       numnew = s->nelements;
00063     }
00064 
00065   s->nspace = numnew + s->nspace; 
00066 
00067   newelements = (exprNode *) dmalloc (sizeof (*newelements) * (s->nelements + numnew));
00068 
00069   if (newelements == (exprNode *) 0)
00070     {
00071       llfatalerror (cstring_makeLiteral ("exprNodeList_grow: out of memory!"));
00072     }
00073 
00074   for (i = 0; i < s->nelements; i++)
00075     {
00076       newelements[i] = s->elements[i];
00077     }
00078   
00079   sfree (s->elements);
00080   s->elements = newelements;
00081 }
00082 
00083 void exprNodeList_addh (exprNodeList s, /*@only@*/ exprNode el)
00084 {
00085   llassert (exprNodeListBASESIZE > 0);
00086 
00087   if (s->nspace <= 0)
00088     exprNodeList_grow (s);
00089   
00090   s->nspace--;
00091   s->elements[s->nelements] = el;
00092   s->nelements++;
00093 }
00094 
00095 void exprNodeList_reset (exprNodeList s)
00096 {
00097   s->current = 0;
00098 }
00099 
00100 void exprNodeList_advance (exprNodeList s)
00101 {
00102   s->current++;
00103   llassert (s->current <= s->nelements);
00104 }
00105 
00106 /*@observer@*/ exprNode exprNodeList_head (exprNodeList s)
00107 {
00108   llassert (s->nelements > 0);
00109   return (s->elements[0]);
00110 }
00111 
00112 /*@observer@*/ exprNode exprNodeList_current (exprNodeList s)
00113 {
00114   llassert (s->current >= 0 && s->current < s->nelements);
00115   return (s->elements[s->current]);
00116 }
00117 
00118 exprNode exprNodeList_getN (exprNodeList s, int n)
00119 {
00120   llassert (n >= 0 && n < s->nelements);
00121   return (s->elements[n]);
00122 }
00123 
00124 /*@only@*/ exprNodeList exprNodeList_singleton (/*@only@*/ exprNode e)
00125 {
00126   exprNodeList s = (exprNodeList) dmalloc (sizeof (*s));
00127   
00128   s->nelements = 1;
00129   s->nspace = exprNodeListBASESIZE - 1; 
00130   s->elements = (exprNode *) dmalloc (sizeof (*s->elements) * exprNodeListBASESIZE);
00131   s->elements[0] = e;
00132   s->current = 0;
00133 
00134   return (s);
00135 }
00136 
00137 exprNodeList exprNodeList_push (/*@returned@*/ exprNodeList args, /*@only@*/ exprNode e)
00138 {
00139   exprNodeList_addh (args, e);
00140   return (args);
00141 }
00142 
00143 /*@exposed@*/ exprNode
00144 exprNodeList_nth (exprNodeList args, int n)
00145 {
00146   if (n >= exprNodeList_size (args) || n < 0)
00147     {
00148       llcontbug (message ("exprNodeList_nth: out of range: %q arg %d\n", 
00149                           exprNodeList_unparse (args), n));
00150       return exprNode_undefined;
00151     }
00152 
00153   return args->elements[n]; 
00154 }
00155 
00156 /*@only@*/ cstring
00157 exprNodeList_unparse (exprNodeList s)
00158 {
00159    int i;
00160    cstring st = cstring_undefined;
00161 
00162       for (i = 0; i < s->nelements; i++)
00163      {
00164        if (i == 0)
00165          {
00166            st = cstring_copy (exprNode_unparse (s->elements[i]));
00167          }
00168        else
00169          st = message ("%q, %s", st, exprNode_unparse (s->elements[i]));
00170      }
00171    
00172    return st;
00173 }
00174 
00175 void
00176 exprNodeList_free (exprNodeList s)
00177 {
00178   int i;
00179 
00180   for (i = 0; i < s->nelements; i++)
00181     {
00182       exprNode_free (s->elements[i]); 
00183     }
00184   
00185   sfree (s->elements); 
00186   sfree (s);
00187 }
00188 
00189 void
00190 exprNodeList_freeShallow (/*@only@*/ exprNodeList s)
00191 {
00192   int i;
00193 
00194   for (i = 0; i < s->nelements; i++)
00195     {
00196       exprNode_freeShallow (s->elements[i]); 
00197     }
00198   
00199   sfree (s->elements); 
00200   sfree (s);
00201 }
00202 

Generated at Fri Nov 3 18:57:41 2000 for LCLint by doxygen1.2.3 written by Dimitri van Heesch, © 1997-2000