]> andersk Git - splint.git/blob - src/context.c
*** empty log message ***
[splint.git] / src / context.c
1 /*
2 ** LCLint - annotation-assisted static program checker
3 ** Copyright (C) 1994-2001 University of Virginia,
4 **         Massachusetts Institute of Technology
5 **
6 ** This program is free software; you can redistribute it and/or modify it
7 ** under the terms of the GNU General Public License as published by the
8 ** Free Software Foundation; either version 2 of the License, or (at your
9 ** option) any later version.
10 ** 
11 ** This program is distributed in the hope that it will be useful, but
12 ** WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 ** General Public License for more details.
15 ** 
16 ** The GNU General Public License is available from http://www.gnu.org/ or
17 ** the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
18 ** MA 02111-1307, USA.
19 **
20 ** For information on lclint: lclint-request@cs.virginia.edu
21 ** To report a bug: lclint-bug@cs.virginia.edu
22 ** For more information: http://lclint.cs.virginia.edu
23 */
24 /*
25 ** context.c
26 */
27 /*
28  * Modified by Herbert 04/19/97:
29  * - added include for new header portab.h containing OS dependent stuff.
30  * - changed occurrances of '/' as path delimiters to macro from portab.h
31  * - changed the handling of the tmp directory -- will no longer always be
32  *   ".", if possible, environment variables "TMP" or, if not set, "TMP", 
33  *   or, if not set "." will be used.
34  */
35
36 # include "lclintMacros.nf"
37 # include "llbasic.h"
38
39 # ifndef NOLCL
40 # include "usymtab_interface.h"
41 # endif
42
43 # include "exprChecks.h"
44 # include "filelocStack.h"
45 # include "fileIdList.h"
46 # include "llmain.h"
47 # include "intSet.h"
48 # include "osd.h"
49 # include "portab.h"
50
51 extern /*@external@*/ int yydebug;
52 extern /*@external@*/ int mtdebug;
53
54 typedef struct
55
56   cstring file; 
57   typeIdSet daccess ; 
58 } maccesst;
59
60 typedef enum { 
61   CX_ERROR,
62
63   CX_GLOBAL, CX_INNER, 
64   CX_FUNCTION, CX_FCNDECLARATION,
65   CX_MACROFCN, CX_MACROCONST, CX_UNKNOWNMACRO, 
66   CX_ITERDEF, CX_ITEREND, 
67   CX_OLDSTYLESCOPE, /* Parsing old-style parameter declarations */
68   CX_LCL, CX_LCLLIB, CX_MT
69 } kcontext;
70
71 static struct
72 {
73   int linesprocessed;
74   int speclinesprocessed;
75
76   flagMarkerList markers;
77
78   /*
79   **  used to record state where a macro must match a function name
80   **   (i.e., no params were listed in the macro definition
81   */
82
83   bool macroMissingParams BOOLBITS;
84   bool preprocessing BOOLBITS;
85   bool incommandline BOOLBITS;
86   bool insuppressregion  BOOLBITS;
87   bool inDerivedFile BOOLBITS;
88   bool instandardlib  BOOLBITS;
89   bool inimport  BOOLBITS;
90   bool inheader  BOOLBITS;
91   bool inmacrocache  BOOLBITS;
92   bool protectVars  BOOLBITS;
93   bool neednl  BOOLBITS;
94   bool showfunction  BOOLBITS;
95   bool savedFlags  BOOLBITS;
96   bool justpopped  BOOLBITS;
97   bool anyExports BOOLBITS;
98   bool inFunctionHeader BOOLBITS;
99
100   flagcode library;
101
102   ynm isNullGuarded;
103   fileloc saveloc;
104   fileloc pushloc;
105
106   clauseStack clauses; 
107   clause inclause;
108
109   int numerrors;
110
111   filelocStack locstack;
112   fileTable ftab;
113   cstring msgAnnote;
114   /*@observer@*/ sRef aliasAnnote;
115   /*@observer@*/ sRef aliasAnnoteAls;
116   messageLog  msgLog;
117
118   macrocache  mc;
119   /*@observer@*/ sRefSet mods;
120
121   /* file access types */
122   typeIdSet facct;   
123
124   /* local access types (this function) */
125   typeIdSet acct;  
126
127   /* no access types (@noaccess) */
128   typeIdSet nacct; 
129
130   /*@observer@*/ globSet globs;
131   /*@only@*/ globSet globs_used;
132   
133   int nmods;
134   int maxmods;
135   /*@reldef@*/ maccesst *moduleaccess; /* Not defined is nmods == 0. */
136   
137   kcontext kind;
138
139   ctype boolType;
140
141   bool flags[NUMFLAGS];
142   bool saveflags[NUMFLAGS];
143   bool setGlobally[NUMFLAGS];
144   bool setLocally[NUMFLAGS];
145
146   int values[NUMVALUEFLAGS];
147   int counters[NUMVALUEFLAGS];
148
149   o_cstring strings[NUMSTRINGFLAGS];
150   sRefSetList modrecs; /*@i32 ???? what is this for? */
151
152   metaStateTable stateTable; /* User-defined state information. */
153   annotationTable annotTable; /* User-defined annotations table. */
154   union u_cont
155     {
156       bool glob;
157       int  cdepth;
158       /*@dependent@*/ /*@exposed@*/ uentry  fcn;
159     } cont;
160
161   kcontext savekind;
162   union u_cont savecont;
163 } gc;
164
165 static /*@exposed@*/ cstring context_exposeString (flagcode p_flag) ;
166 static void context_restoreFlagSettings (void) /*@modifies gc@*/ ;
167 static void context_saveFlagSettings (void) /*@modifies gc@*/ ;
168 static void context_exitClauseAux (exprNode p_pred, exprNode p_tbranch)
169    /*@modifies gc@*/ ;
170 static void context_exitClauseSimp (void)  /*@modifies gc@*/ ;
171 static void context_exitClausePlain (void) /*@modifies gc@*/ ;
172 static void context_setJustPopped (void) /*@modifies gc.justpopped@*/ ;
173 static void context_setValue (flagcode p_flag, int p_val) /*@modifies gc.flags@*/ ;
174 static void context_setFlag (flagcode p_f, bool p_b)
175   /*@modifies gc.flags@*/ ;
176
177 static void
178   context_setFlagAux (flagcode p_f, bool p_b, bool p_inFile, bool p_isRestore)
179   /*@modifies gc.flags@*/ ;
180
181 static void context_restoreFlag (flagcode p_f) /*@modifies gc.flags@*/ ;
182
183 /*@+enumindex@*/ 
184
185 cstring context_unparseFlagMarkers ()
186 {
187   return (flagMarkerList_unparse (gc.markers));
188 }
189
190 void context_setPreprocessing (void)
191 {
192   llassert (!gc.preprocessing);
193   gc.preprocessing = TRUE;
194 }
195
196 void context_clearPreprocessing (void)
197 {
198   llassert (gc.preprocessing);
199   gc.preprocessing = FALSE;
200 }
201
202 bool context_isPreprocessing (void)
203 {
204   return gc.preprocessing;
205 }
206
207 bool context_loadingLibrary (void)
208 {
209   return (fileloc_isLib (g_currentloc));
210 }
211
212 bool context_inXHFile (void)
213 {
214   return (fileloc_isXHFile (g_currentloc));
215 }
216
217 void context_setInCommandLine (void)
218 {
219   llassert (!gc.incommandline);
220   gc.incommandline = TRUE;
221 }
222
223 void context_clearInCommandLine (void)
224 {
225   llassert (gc.incommandline);
226   gc.incommandline = FALSE;
227 }
228
229 bool context_isInCommandLine (void)
230 {
231   return gc.incommandline;
232 }
233
234 static
235 void pushClause (clause c) /*@modifies gc.clauses, gc.inclause@*/
236 {
237   gc.inclause = c;
238   clauseStack_push (gc.clauses, c);
239
240   if (clause_isConditional (c)
241       && context_getFlag (FLG_CONTROLNESTDEPTH))
242     {
243       int maxdepth = context_getValue (FLG_CONTROLNESTDEPTH);
244       int depth = clauseStack_controlDepth (gc.clauses);
245       
246       if (depth == maxdepth + 1)
247         {
248           voptgenerror 
249             (FLG_CONTROLNESTDEPTH,
250              message ("Maximum control nesting depth "
251                       "(%d) exceeded",
252                       maxdepth),
253              g_currentloc);
254         }
255     }
256 }
257
258 static
259 clause topClause (clauseStack s) /*@*/
260 {
261   if (clauseStack_isEmpty (s)) return NOCLAUSE;
262   return ((clause) clauseStack_top (s));
263 }
264
265 void
266 context_addMacroCache (/*@only@*/ cstring def)
267 {
268   DPRINTF (("macro cache: %s", def));
269   macrocache_addEntry (gc.mc, fileloc_copy (g_currentloc), def);
270 }
271
272 void
273 context_addComment (/*@only@*/ cstring def)
274 {
275   DPRINTF (("macro comment: %s", def));
276   macrocache_addComment (gc.mc, fileloc_copy (g_currentloc), def);
277 }
278
279 /*
280 ** returns TRUE is fl is in ignore region, or region where -code
281 **
282 ** the logic is fuzzy...
283 */
284
285 static bool
286 context_inSuppressFlagZone (fileloc fl, flagcode code)
287 {
288   ynm ret = flagMarkerList_suppressError (gc.markers, code, fl);
289   bool res = FALSE;
290   
291   if (ynm_isMaybe (ret))
292     {
293       /*
294       ** whas is dis?
295       */
296
297       if (gc.savedFlags)
298         {
299           res = !gc.saveflags[code];
300         }
301       else
302         {
303           res = !context_getFlag (code);
304         }
305     }
306   else
307     {
308       res = ynm_toBoolStrict (ret);
309     }
310   
311     return res;
312 }
313
314 static bool
315 context_suppressSystemMsg (fileloc fl)
316 {
317   if (context_getFlag (FLG_SYSTEMDIRERRORS))
318     {
319       return FALSE;
320     }
321   else
322     {
323       return (fileloc_isSystemFile (fl));
324     }
325 }
326
327 bool 
328 context_suppressFlagMsg (flagcode flag, fileloc fl)
329 {
330   if (context_suppressSystemMsg (fl))
331     {
332       return TRUE;
333     }
334
335   DPRINTF (("Checking suppress: %s / %s", fileloc_unparse (fl), fileloc_unparse (g_currentloc)));
336
337   /* want same object compare here */
338
339   if (fileloc_equal (fl, g_currentloc) || gc.inDerivedFile)
340     {
341       DPRINTF (("In derived file: %s", bool_unparse (gc.inDerivedFile)));
342
343       return (!context_getFlag (flag)
344               || context_inSuppressRegion ()
345               || context_inSuppressZone (fl)
346               || (/*@!@@#@ gc.inDerivedFile && */ context_inSuppressFlagZone (fl, flag)));
347     }
348   else
349     {
350       return (context_inSuppressFlagZone (fl, flag));
351     }
352 }
353
354 bool 
355 context_suppressNotFlagMsg (flagcode flag, fileloc fl)
356 {
357   
358   if (context_suppressSystemMsg (fl))
359     {
360       return TRUE;
361     }
362
363   /*@access fileloc@*/ 
364   if (fl == g_currentloc)
365     /*@noaccess fileloc@*/
366     {
367       return (context_getFlag (flag) || context_inSuppressRegion ());
368     }
369   else
370     {
371       /* for now... */
372       return (context_getFlag (flag) || context_inSuppressRegion ());
373     }
374 }
375
376 bool
377 context_inSuppressZone (fileloc fl) 
378 {
379   if (context_suppressSystemMsg (fl))
380     {
381       return TRUE;
382     }
383
384   return (flagMarkerList_inIgnore (gc.markers, fl));
385 }
386
387 bool
388 context_inSuppressRegion (void)
389 {
390   return (gc.insuppressregion);
391 }
392
393 void
394 context_enterSuppressRegion (void)
395 {
396   if (gc.insuppressregion)
397     {
398       gc.insuppressregion = FALSE;      /* get this msg! */
399       llmsg (message
400              ("%q: New ignore errors region entered while in ignore errors region",
401               fileloc_unparse (g_currentloc)));
402     }
403   
404   gc.insuppressregion = TRUE;
405   flagMarkerList_add (gc.markers, flagMarker_createIgnoreOn (g_currentloc));
406 }
407
408 static void
409 context_addFlagMarker (flagcode code, ynm set)
410 {
411   flagMarkerList_add (gc.markers,
412                       flagMarker_createLocalSet (code, set, g_currentloc));
413 }
414
415 void
416 context_enterSuppressLine (int count)
417 {
418   fileloc nextline = fileloc_copy (g_currentloc);
419
420   flagMarkerList_add (gc.markers,
421                       flagMarker_createIgnoreCount (count, g_currentloc));
422
423   fileloc_nextLine (nextline);
424   flagMarkerList_add (gc.markers,
425                       flagMarker_createIgnoreOff (nextline));
426   fileloc_free (nextline);
427 }
428
429 void context_checkSuppressCounts (void)
430 {
431   if (context_getFlag (FLG_SUPCOUNTS))
432     {
433       flagMarkerList_checkSuppressCounts (gc.markers);
434     }
435 }
436
437 void context_incLineno (void)
438 {
439   gc.linesprocessed++;
440   incLine ();
441 }
442
443 void
444 context_exitSuppressRegion (void)
445 {
446   if (!gc.insuppressregion)
447     {
448       llerrorlit (FLG_SYNTAX, 
449                   "End ignore errors in region while not ignoring errors");
450     }
451
452     gc.insuppressregion = FALSE;
453   flagMarkerList_add (gc.markers, flagMarker_createIgnoreOff (g_currentloc));
454 }
455
456 void
457 context_enterMTfile (void)
458 {
459   gc.kind = CX_MT;
460 }
461
462 void
463 context_exitMTfile (void)
464 {
465   llassert (gc.kind == CX_MT);
466   gc.kind = CX_GLOBAL;
467 }
468
469 # ifndef NOLCL
470 void
471 context_enterLCLfile (void)
472 {
473   gc.kind = CX_LCL;
474   gc.facct = typeIdSet_emptySet ();
475 }
476 # endif
477
478 static void
479 addModuleAccess (/*@only@*/ cstring fname, typeIdSet mods)
480 {
481   int i;
482
483   for (i = 0; i < gc.nmods; i++)
484     {
485       if (cstring_equal (gc.moduleaccess[i].file, fname))
486         {
487           gc.moduleaccess[i].daccess = typeIdSet_union (gc.moduleaccess[i].daccess, mods);
488           cstring_free (fname);
489           return;
490         }
491     }
492   
493   if (gc.nmods == gc.maxmods)
494     {
495       maccesst *oldmods;
496       
497       gc.maxmods = gc.maxmods + DEFAULTMAXMODS;      
498       oldmods = gc.moduleaccess;
499       
500       gc.moduleaccess = (maccesst *) dmalloc (sizeof (*gc.moduleaccess) * (gc.maxmods));
501       
502       for (i = 0; i < gc.nmods; i++)
503         {
504           gc.moduleaccess[i] = oldmods[i];
505         }
506       
507       sfree (oldmods);
508     }
509   
510   gc.moduleaccess[gc.nmods].file = fname;
511   gc.moduleaccess[gc.nmods].daccess = mods;
512   
513   gc.nmods++;
514 }
515
516 static void
517 insertModuleAccess (cstring fname, typeId t)
518 {
519   int i;
520   
521   for (i = 0; i < gc.nmods; i++)
522     {
523       if (cstring_equal (gc.moduleaccess[i].file, fname))
524         {
525           gc.moduleaccess[i].daccess = typeIdSet_insert (gc.moduleaccess[i].daccess, t);
526           break;
527         }
528     }
529   
530     addModuleAccess (cstring_copy (fname), typeIdSet_single (t));
531 }
532
533 # ifndef NOLCL
534 void
535 context_exitLCLfile (void)
536 {
537   if (gc.kind != CX_LCLLIB)
538     {
539       cstring lclname =  
540         fileLib_withoutExtension (fileName (currentFile ()), LCL_EXTENSION);
541       
542       addModuleAccess (fileLib_removePath (lclname), gc.facct);
543       cstring_free (lclname);
544     }
545   
546   gc.kind = CX_LCL;
547   gc.kind = CX_GLOBAL;
548   gc.facct = typeIdSet_emptySet ();
549 }
550 # endif
551
552 void
553 context_dumpModuleAccess (FILE *fout)
554 {
555   int i = 0;
556
557   for (i = 0; i < gc.nmods; i++)
558     {
559       cstring td = typeIdSet_dump (gc.moduleaccess[i].daccess);
560
561       fprintf (fout, "%s#%s@\n", 
562                cstring_toCharsSafe (gc.moduleaccess[i].file), 
563                cstring_toCharsSafe (td));
564       
565       cstring_free (td);
566     }
567 }
568
569 bool context_usingPosixLibrary ()
570 {
571   return (gc.library == FLG_POSIXLIB 
572           || gc.library == FLG_POSIXSTRICTLIB
573           || gc.library == FLG_UNIXLIB
574           || gc.library == FLG_UNIXSTRICTLIB);
575 }
576
577 bool context_usingAnsiLibrary ()
578 {
579   return (gc.library != FLG_NOLIB);
580 }
581
582 flagcode context_getLibrary ()
583 {
584   return gc.library;
585 }
586
587 void context_setLibrary (flagcode code)
588 {
589   gc.library = code;
590 }
591
592 /*@observer@*/ cstring context_selectedLibrary ()
593 {
594   switch (gc.library)
595     {
596     case FLG_STRICTLIB:
597       return cstring_makeLiteralTemp (LLSTRICTLIBS_NAME);
598     case FLG_POSIXLIB:
599       return cstring_makeLiteralTemp (LLPOSIXLIBS_NAME);
600     case FLG_POSIXSTRICTLIB:
601       return cstring_makeLiteralTemp (LLPOSIXSTRICTLIBS_NAME);
602     case FLG_UNIXLIB:
603       return cstring_makeLiteralTemp (LLUNIXLIBS_NAME);    
604     case FLG_UNIXSTRICTLIB:
605       return cstring_makeLiteralTemp (LLUNIXSTRICTLIBS_NAME);
606     case FLG_ANSILIB:
607       return cstring_makeLiteralTemp (LLSTDLIBS_NAME);
608     BADDEFAULT;
609     }
610 }
611   
612
613 void
614 context_loadModuleAccess (FILE *in)
615 {
616   char *s = mstring_create (MAX_DUMP_LINE_LENGTH);
617   char *lasts = s;
618   char *name = mstring_create (MAX_NAME_LENGTH);
619   char *oname = name;
620 # ifndef NOFREE
621   char *os = s;
622 # endif
623
624   while ((reader_readLine (in, s, MAX_DUMP_LINE_LENGTH) != NULL )
625          && *s == ';')
626     {
627       ;
628     }
629
630   while (s != NULL && *s != ';' && *s != '\0')
631     {
632       name = oname;
633       
634       while (*s != '#' && *s != '\0')
635         {
636           *name++ = *s++;
637         }
638
639       *name = '\0';
640
641       if (*s != '#')
642         {
643           llcontbug (message ("context_loadModuleAccess: bad library line: %s\n", 
644                               cstring_fromChars (s)));
645           break;
646         }
647
648       s++;
649
650       addModuleAccess (cstring_copy (cstring_fromChars (oname)), 
651                        typeIdSet_undump (&s)); 
652
653       (void) reader_readLine (in, s, MAX_DUMP_LINE_LENGTH);
654       llassert (s != lasts);
655       lasts = s;
656     }
657
658   sfree (oname);
659 # ifndef NOFREE
660   sfree (os);
661 # endif
662 }
663
664 typeIdSet context_fileAccessTypes (void)
665 {
666   return gc.facct;
667 }
668
669 void
670 context_resetModeFlags (void)
671 {
672   
673   allFlagCodes (code)
674     {
675       if (flagcode_isModeFlag (code))
676         {
677           context_setFlag (code, FALSE);
678         }
679     } end_allFlagCodes;
680
681   }
682
683 /*
684 ** resetAllFlags
685 **
686 ** Set all flags to FALSE, except for a few which are
687 ** true by default.
688 **
689 ** Set all values and strings to appropriate defaults.
690 ** Set all counters to 0.
691 */
692
693 static void
694 conext_resetAllCounters (void)
695 {
696   int i;
697
698   for (i = 0; i < NUMVALUEFLAGS; i++)
699     {
700       gc.counters[i] = 0;
701     }
702 }
703
704 void
705 context_resetAllFlags (void) 
706 {
707   allFlagCodes (code)
708     {
709       gc.flags[code] = FALSE;
710
711       if (flagcode_hasValue (code))
712         {
713           int val = 0;
714           
715           /*@-loopswitchbreak@*/
716           switch (code)
717             {
718             case FLG_LIMIT: 
719               val = DEFAULT_LIMIT; break;
720             case FLG_BUGSLIMIT:
721               val = DEFAULT_BUGSLIMIT; break;
722             case FLG_LINELEN: 
723               val = DEFAULT_LINELEN; break;
724             case FLG_INDENTSPACES: 
725               val = DEFAULT_INDENTSPACES; break;
726             case FLG_EXTERNALNAMELEN:
727               val = DEFAULT_EXTERNALNAMELEN; break;
728             case FLG_INTERNALNAMELEN:
729               val = DEFAULT_INTERNALNAMELEN; break;
730             case FLG_COMMENTCHAR: 
731               val = (int) DEFAULT_COMMENTCHAR; break;
732             case FLG_CONTROLNESTDEPTH:
733               val = (int) DEFAULT_CONTROLNESTDEPTH; break;
734             case FLG_STRINGLITERALLEN:
735               val = (int) DEFAULT_STRINGLITERALLEN; break;
736             case FLG_INCLUDENEST:
737               val = (int) DEFAULT_INCLUDENEST; break;
738             case FLG_NUMSTRUCTFIELDS:
739               val = (int) DEFAULT_NUMSTRUCTFIELDS; break;
740             case FLG_NUMENUMMEMBERS:
741               val = (int) DEFAULT_NUMENUMMEMBERS; break;
742             case FLG_EXPECT:
743             case FLG_LCLEXPECT:
744               break;
745             default:
746               llbug (message ("Bad value flag: %s", flagcode_unparse (code)));
747             }
748           /*@=loopswitchbreak@*/          
749
750           context_setValue (code, val);
751         }
752       else if (flagcode_hasString (code))
753         {
754           cstring val = cstring_undefined;
755           
756           switch (code)
757             { /*@-loopswitchbreak@*/
758             case FLG_LARCHPATH:
759               {
760                 cstring larchpath = osd_getEnvironmentVariable (LARCH_PATH);
761                 
762                 if (cstring_isDefined (larchpath))
763                   {
764                     val = cstring_copy (larchpath);
765                   }
766                 else
767                   {
768                     val = cstring_makeLiteral (DEFAULT_LARCHPATH);
769                   }
770                 
771                 break;
772               }
773             case FLG_LCLIMPORTDIR:
774               {
775                 val = cstring_copy (osd_getEnvironment (cstring_makeLiteralTemp (LCLIMPORTDIR), cstring_makeLiteralTemp (DEFAULT_LCLIMPORTDIR)));
776                 break;
777               }
778             case FLG_TMPDIR: 
779 # if defined(OS2) || defined(MSDOS) || defined(WIN32)
780               {
781                 char *env = osd_getEnvironmentVariable ("TMP");
782
783                 if (env == NULL)
784                   {
785                     env = osd_getEnvironmentVariable ("TEMP");
786                   }
787
788                 val = cstring_makeLiteral (env != NULL ? env : DEFAULT_TMPDIR);
789               }
790 # else
791               val = cstring_makeLiteral (DEFAULT_TMPDIR);
792 # endif /* !defined(OS2) && !defined(MSDOS) */
793
794               break;
795             case FLG_BOOLTYPE:
796               val = cstring_makeLiteral (DEFAULT_BOOLTYPE); break;
797             case FLG_BOOLFALSE:
798               val = cstring_makeLiteral ("FALSE"); break;
799             case FLG_BOOLTRUE:
800               val = cstring_makeLiteral ("TRUE"); break;
801             case FLG_MACROVARPREFIX: 
802               val = cstring_makeLiteral ("m_"); break;
803             case FLG_SYSTEMDIRS:
804               val = cstring_makeLiteral (DEFAULT_SYSTEMDIR); break;
805             default:
806               break;
807             } /*@=loopswitchbreak@*/
808           
809           context_setString (code, val);
810         }
811       else
812         { 
813           ; /* nothing to set */
814         }
815     } end_allFlagCodes;
816   
817   /*
818   ** These flags are true by default.
819   */
820
821   /*@i34 move this into flags.def */
822     
823   gc.flags[FLG_MODIFIES] = TRUE;
824   gc.flags[FLG_NESTCOMMENT] = TRUE;
825   gc.flags[FLG_GLOBALS] = TRUE;
826   gc.flags[FLG_FULLINITBLOCK] = TRUE;
827   gc.flags[FLG_LIKELYBOOL] = TRUE;
828   gc.flags[FLG_ZEROPTR] = TRUE;
829   gc.flags[FLG_NUMLITERAL] = TRUE;
830   gc.flags[FLG_DUPLICATEQUALS] = TRUE;
831   gc.flags[FLG_SKIPANSIHEADERS] = TRUE;
832   gc.flags[FLG_SKIPPOSIXHEADERS] = TRUE;
833   gc.flags[FLG_SYSTEMDIREXPAND] = TRUE;
834   gc.flags[FLG_UNRECOGCOMMENTS] = TRUE;
835   gc.flags[FLG_UNRECOGFLAGCOMMENTS] = TRUE;
836   gc.flags[FLG_CASTFCNPTR] = TRUE;
837   gc.flags[FLG_DOLCS] = TRUE;
838   gc.flags[FLG_USEVARARGS] = TRUE;
839   gc.flags[FLG_MAINTYPE] = TRUE;
840   gc.flags[FLG_SPECMACROS] = TRUE;
841   gc.flags[FLG_REDEF] = TRUE;
842   gc.flags[FLG_MACRONEXTLINE] = TRUE;
843
844   gc.flags[FLG_SIZEOFFORMALARRAY] = TRUE;
845   gc.flags[FLG_FIXEDFORMALARRAY] = TRUE;
846
847   gc.flags[FLG_WARNUSE] = TRUE;
848   gc.flags[FLG_PREDASSIGN] = TRUE;
849   gc.flags[FLG_MODOBSERVER] = TRUE;
850   gc.flags[FLG_MACROVARPREFIXEXCLUDE] = TRUE;
851   gc.flags[FLG_EXTERNALNAMECASEINSENSITIVE] = TRUE;
852
853   gc.flags[FLG_PARAMIMPTEMP] = TRUE;
854   gc.flags[FLG_RETIMPONLY] = TRUE;
855   gc.flags[FLG_GLOBIMPONLY] = TRUE;
856   gc.flags[FLG_STRUCTIMPONLY] = TRUE;
857   gc.flags[FLG_PREPROC] = TRUE;
858   gc.flags[FLG_NAMECHECKS] = TRUE;
859   gc.flags[FLG_FORMATCODE] = TRUE;
860   gc.flags[FLG_FORMATTYPE] = TRUE;
861   gc.flags[FLG_BADFLAG] = TRUE;
862   gc.flags[FLG_WARNFLAGS] = TRUE;
863   gc.flags[FLG_FILEEXTENSIONS] = TRUE;
864   gc.flags[FLG_WARNUNIXLIB] = TRUE;
865   gc.flags[FLG_WARNPOSIX] = TRUE;
866   gc.flags[FLG_SHOWCOL] = TRUE;
867   gc.flags[FLG_SHOWFUNC] = TRUE;
868   gc.flags[FLG_SUPCOUNTS] = TRUE;
869   gc.flags[FLG_HINTS] = TRUE;
870   gc.flags[FLG_SYNTAX] = TRUE;
871   gc.flags[FLG_TYPE] = TRUE;
872   gc.flags[FLG_INCOMPLETETYPE] = TRUE;
873   gc.flags[FLG_ABSTRACT] = TRUE;
874   gc.flags[FLG_ITER] = TRUE;
875   gc.flags[FLG_CONTROL] = TRUE;
876   gc.flags[FLG_UNRECOG] = TRUE;
877   gc.flags[FLG_SYSTEMUNRECOG] = TRUE;
878   gc.flags[FLG_LINTCOMMENTS] = TRUE;
879   gc.flags[FLG_ACCESSCZECH] = TRUE;
880   gc.flags[FLG_ACCESSMODULE] = TRUE;
881   gc.flags[FLG_ACCESSFILE] = TRUE;
882   gc.flags[FLG_MACROVARPREFIX] = TRUE;
883
884   gc.flags[FLG_ANNOTATIONERROR] = TRUE;
885   gc.flags[FLG_COMMENTERROR] = TRUE;
886
887   /*
888   ** Changed for version 2.4.
889   */
890
891   gc.flags[FLG_GNUEXTENSIONS] = TRUE;
892
893   /*
894   ** On by default for Win32, but not Unix (to support MS/VC++ error message format).
895   */
896
897 # ifdef WIN32
898   gc.flags[FLG_PARENFILEFORMAT] = TRUE;
899 # endif
900 }
901
902 /*
903 ** C is way-lame, and you can initialize an array to a constant except where
904 ** it is declared.  Hence, a macro is used to set the modeflags.
905 */
906
907 /*@notfunction@*/
908 # define SETFLAGS() \
909   { int i = 0; while (modeflags[i] != INVALID_FLAG) { \
910       if (!flagcode_isModeFlag (modeflags[i])) \
911         { llbug (message ("not a mode flag: %s", \
912                           flagcode_unparse (modeflags[i]))); } \
913       else { context_setFlag (modeflags[i], TRUE); }  i++; }}
914
915 void
916 context_setMode (cstring s)
917 {
918   intSet setflags = intSet_new ();
919   
920   allFlagCodes (code)
921     {
922       if (flagcode_isModeFlag (code))
923         {
924           if (gc.setGlobally[code])
925             {
926               (void) intSet_insert (setflags, (int) code);
927             }
928         }
929     } end_allFlagCodes;
930   
931   if (!intSet_isEmpty (setflags))
932     {
933       cstring rflags = cstring_undefined;
934       int num = 0;
935
936       intSet_elements (setflags, el)
937         {
938           if (cstring_isUndefined (rflags))
939             {
940               rflags = cstring_copy (flagcode_unparse ((flagcode) (el)));
941             }
942           else
943             {
944               rflags = message ("%q, %s", rflags, 
945                                 flagcode_unparse ((flagcode) el));
946             }
947
948           num++;
949           if (num > 4 && intSet_size (setflags) > 6)
950             {
951               rflags = message ("%q, (%d others) ...", rflags, 
952                                 intSet_size (setflags) - num);
953               break;
954             }
955         } end_intSet_elements ;
956       
957       voptgenerror (FLG_WARNFLAGS,
958                     message ("Setting mode %s after setting mode flags will "
959                              "override set values of flags: %s",
960                              s, rflags),
961                     g_currentloc);
962
963       cstring_free (rflags);
964     }
965
966   intSet_free (setflags);
967
968   context_resetModeFlags ();
969
970   if (cstring_equalLit (s, "standard"))
971     {
972       flagcode modeflags[] = 
973         {
974           FLG_ENUMINT, FLG_MACROMATCHNAME,
975           FLG_MACROUNDEF, FLG_RELAXQUALS, 
976           FLG_USEALLGLOBS, FLG_CHECKSTRICTGLOBALS,
977           FLG_CHECKSTRICTGLOBALIAS,
978           FLG_CHECKEDGLOBALIAS,
979           FLG_CHECKMODGLOBALIAS,
980           FLG_PREDBOOLOTHERS, FLG_PREDBOOLINT,
981           FLG_PARAMUNUSED, FLG_VARUNUSED, FLG_FUNCUNUSED, 
982           FLG_TYPEUNUSED,
983           FLG_CONSTUNUSED, FLG_ENUMMEMUNUSED, FLG_FIELDUNUSED,
984           FLG_PTRNUMCOMPARE, FLG_BOOLCOMPARE, FLG_MUTREP, 
985           FLG_NOEFFECT, FLG_IMPTYPE,
986           FLG_RETVALOTHER, FLG_RETVALBOOL, FLG_RETVALINT,
987           FLG_SPECUNDEF, FLG_INCONDEFS, FLG_INCONDEFSLIB, FLG_MISPLACEDSHAREQUAL,
988           FLG_MATCHFIELDS,
989           FLG_FORMATCONST,
990           FLG_MACROPARAMS, FLG_MACROASSIGN, FLG_SEFPARAMS, 
991           FLG_MACROSTMT, FLG_MACROPARENS, 
992           FLG_MACROFCNDECL,
993           FLG_MACROCONSTDECL,
994           FLG_MACROREDEF, FLG_INFLOOPS, FLG_UNREACHABLE, 
995           FLG_NORETURN, FLG_CASEBREAK, FLG_MISSCASE, FLG_USEDEF, 
996           FLG_FIRSTCASE,
997           FLG_NESTEDEXTERN, 
998           FLG_NUMLITERAL,
999           FLG_ZEROBOOL,
1000           /* memchecks flags */
1001           FLG_NULLDEREF, 
1002           FLG_NULLSTATE, FLG_NULLASSIGN,
1003           FLG_NULLPASS, FLG_NULLRET,        
1004
1005           FLG_COMPDEF, FLG_COMPMEMPASS, FLG_UNIONDEF,
1006           FLG_RETSTACK,
1007
1008           /* memtrans flags */
1009           FLG_EXPOSETRANS,
1010           FLG_OBSERVERTRANS,
1011           FLG_DEPENDENTTRANS,
1012           FLG_NEWREFTRANS,
1013           FLG_ONLYTRANS,
1014           FLG_OWNEDTRANS,
1015           FLG_FRESHTRANS,
1016           FLG_SHAREDTRANS,
1017           FLG_TEMPTRANS,
1018           FLG_KEPTTRANS,
1019           FLG_REFCOUNTTRANS,
1020           FLG_STATICTRANS,
1021           FLG_UNKNOWNTRANS,
1022           FLG_KEEPTRANS,
1023           FLG_IMMEDIATETRANS,
1024
1025           FLG_EXPORTLOCAL,
1026
1027           FLG_USERELEASED, FLG_ALIASUNIQUE, FLG_MAYALIASUNIQUE,
1028           FLG_MUSTFREE, FLG_MUSTDEFINE, FLG_GLOBSTATE, 
1029           FLG_COMPDESTROY, FLG_MUSTNOTALIAS,
1030           FLG_MEMIMPLICIT,
1031           FLG_BRANCHSTATE, 
1032           FLG_STATETRANSFER, FLG_STATEMERGE,
1033           FLG_EVALORDER, FLG_SHADOW, FLG_READONLYSTRINGS,
1034           FLG_EXITARG,
1035           FLG_IMPCHECKEDSPECGLOBALS,
1036           FLG_MODGLOBS, FLG_WARNLINTCOMMENTS,
1037           FLG_IFEMPTY, FLG_REALCOMPARE,
1038           FLG_BOOLOPS, FLG_PTRNEGATE,
1039           FLG_SHIFTSIGNED,
1040           FLG_BUFFEROVERFLOWHIGH,
1041           FLG_BUFFEROVERFLOW,
1042           INVALID_FLAG 
1043         } ;
1044
1045       SETFLAGS ();
1046     }
1047   else if (cstring_equalLit (s, "weak"))
1048     {
1049       flagcode modeflags[] = 
1050         { 
1051           FLG_BOOLINT, FLG_CHARINT, FLG_FLOATDOUBLE,
1052           FLG_ENUMINT, FLG_RELAXQUALS, FLG_FORWARDDECL, 
1053           FLG_CHARINDEX, FLG_ABSTVOIDP, FLG_USEALLGLOBS, 
1054           FLG_CHARUNSIGNEDCHAR,
1055           FLG_PREDBOOLOTHERS, 
1056           FLG_VARUNUSED, FLG_FUNCUNUSED, 
1057           FLG_TYPEUNUSED,
1058           FLG_CHECKSTRICTGLOBALS, FLG_MACROMATCHNAME,
1059           FLG_RETVALOTHER,
1060           FLG_IFEMPTY, 
1061           FLG_RETSTACK, FLG_PTRNEGATE,
1062           FLG_STATETRANSFER, FLG_STATEMERGE,
1063           FLG_LONGUNSIGNEDINTEGRAL,
1064           FLG_LONGUNSIGNEDUNSIGNEDINTEGRAL,
1065           FLG_NUMLITERAL,
1066           FLG_CHARINTLITERAL,
1067           FLG_ZEROBOOL,
1068           FLG_BUFFEROVERFLOWHIGH,
1069           INVALID_FLAG 
1070           } ;
1071
1072       SETFLAGS ();
1073     }
1074   else if (cstring_equalLit (s, "checks"))
1075     {
1076       flagcode modeflags[] = 
1077         { 
1078           FLG_EXPORTLOCAL, FLG_IMPTYPE,
1079           FLG_STATETRANSFER, FLG_STATEMERGE,
1080           FLG_CHECKSTRICTGLOBALIAS,
1081           FLG_CHECKEDGLOBALIAS,
1082           FLG_CHECKMODGLOBALIAS,
1083           FLG_UNCHECKEDGLOBALIAS,
1084           FLG_FORMATCONST,
1085           FLG_EXITARG, FLG_PTRNUMCOMPARE, 
1086           FLG_BOOLCOMPARE, FLG_MACROUNDEF, 
1087           FLG_MUSTMOD, FLG_ALLGLOBALS,
1088           FLG_PREDBOOLOTHERS, FLG_PREDBOOLPTR, FLG_PREDBOOLINT,
1089           FLG_USEALLGLOBS, FLG_MUTREP, FLG_RETALIAS, 
1090           FLG_RETEXPOSE, FLG_ASSIGNEXPOSE, FLG_CASTEXPOSE,
1091           FLG_FUNCUNUSED, FLG_GLOBALSIMPMODIFIESNOTHING,
1092           FLG_TYPEUNUSED, FLG_FIELDUNUSED, FLG_PARAMUNUSED, FLG_VARUNUSED,
1093           FLG_CONSTUNUSED, FLG_ENUMMEMUNUSED,
1094           FLG_NOEFFECT, FLG_EXPORTHEADER, FLG_EXPORTHEADERVAR,
1095           FLG_RETVALOTHER, FLG_RETVALBOOL, FLG_RETVALINT,
1096           FLG_SPECUNDEF, FLG_IMPCHECKMODINTERNALS,
1097           FLG_DECLUNDEF, FLG_INCONDEFS, FLG_INCONDEFSLIB, 
1098           FLG_MISPLACEDSHAREQUAL, FLG_REDUNDANTSHAREQUAL,
1099           FLG_MATCHFIELDS, 
1100           FLG_MACROPARAMS,
1101           FLG_MACROASSIGN,
1102           FLG_DECLPARAMMATCH,
1103           FLG_FCNDEREF,
1104           FLG_FIRSTCASE,
1105           FLG_SEFPARAMS, FLG_SEFUNSPEC, FLG_MACROSTMT, FLG_MACROPARENS, 
1106           FLG_MACROCONSTDECL,
1107           FLG_MACROFCNDECL,
1108           FLG_MACROREDEF, 
1109           FLG_INFLOOPS, FLG_INFLOOPSUNCON,
1110           FLG_UNREACHABLE, 
1111           FLG_NORETURN, FLG_CASEBREAK, FLG_MISSCASE,
1112           FLG_EVALORDER, FLG_USEDEF, 
1113
1114           FLG_NESTEDEXTERN, 
1115
1116           /* memchecks flags */
1117
1118           FLG_NULLSTATE, FLG_NULLDEREF, FLG_NULLASSIGN,
1119           FLG_NULLPASS, FLG_NULLRET,
1120
1121           FLG_COMPDEF, FLG_COMPMEMPASS, FLG_UNIONDEF, FLG_RETSTACK,       
1122
1123           /* memtrans flags */
1124           FLG_EXPOSETRANS,
1125           FLG_OBSERVERTRANS,
1126           FLG_DEPENDENTTRANS,
1127           FLG_NEWREFTRANS,
1128           FLG_ONLYTRANS,
1129           FLG_OWNEDTRANS,
1130           FLG_FRESHTRANS,
1131           FLG_SHAREDTRANS,
1132           FLG_TEMPTRANS,
1133           FLG_KEPTTRANS,
1134           FLG_REFCOUNTTRANS,
1135           FLG_STATICTRANS,
1136           FLG_UNKNOWNTRANS,
1137           FLG_STATICINITTRANS,
1138           FLG_UNKNOWNINITTRANS,
1139           FLG_KEEPTRANS,
1140           FLG_IMMEDIATETRANS,
1141           FLG_ONLYUNQGLOBALTRANS,
1142           FLG_USERELEASED, FLG_ALIASUNIQUE, FLG_MAYALIASUNIQUE,
1143           FLG_MUSTFREE, FLG_MUSTDEFINE, FLG_GLOBSTATE, 
1144           FLG_COMPDESTROY, FLG_MUSTNOTALIAS,
1145           FLG_MEMIMPLICIT,
1146           FLG_BRANCHSTATE, 
1147           FLG_NULLPOINTERARITH,
1148           FLG_SHADOW, FLG_DEPARRAYS,
1149           FLG_REDECL, FLG_READONLYSTRINGS, FLG_READONLYTRANS,
1150           FLG_LOOPLOOPBREAK, FLG_SWITCHLOOPBREAK, FLG_MODGLOBS,
1151           FLG_CHECKSTRICTGLOBALS, FLG_IMPCHECKEDSPECGLOBALS,
1152           FLG_MACROMATCHNAME, FLG_WARNLINTCOMMENTS,
1153           FLG_INCLUDENEST, FLG_ANSIRESERVED, FLG_CPPNAMES, 
1154           FLG_NOPARAMS, FLG_IFEMPTY, FLG_WHILEEMPTY, FLG_REALCOMPARE,
1155           FLG_BOOLOPS, FLG_SHIFTSIGNED,
1156           FLG_BUFFEROVERFLOWHIGH, FLG_BUFFEROVERFLOW,
1157           INVALID_FLAG } ;
1158
1159       SETFLAGS ();
1160     }
1161   else if (cstring_equalLit (s, "strict"))
1162     {
1163       flagcode modeflags[] = 
1164         { 
1165           FLG_CHECKSTRICTGLOBALIAS,
1166           FLG_CHECKEDGLOBALIAS,
1167           FLG_CHECKMODGLOBALIAS,
1168           FLG_UNCHECKEDGLOBALIAS,
1169           FLG_MODFILESYSTEM,
1170           FLG_MACROMATCHNAME,
1171           FLG_FORMATCONST,
1172           FLG_STATETRANSFER, FLG_STATEMERGE,
1173           FLG_MACROUNDEF, FLG_MUTREP, FLG_MUSTMOD,
1174           FLG_ALLGLOBALS, FLG_IMPTYPE,
1175           FLG_MODNOMODS, FLG_MODGLOBSUNSPEC, FLG_MODSTRICTGLOBSUNSPEC,
1176           FLG_GLOBUNSPEC, FLG_SIZEOFTYPE,
1177           FLG_EXPORTHEADER, FLG_EXPORTHEADERVAR,
1178           FLG_NOPARAMS, FLG_OLDSTYLE, FLG_EXITARG, 
1179           FLG_RETSTACK,
1180           FLG_FCNDEREF,
1181           FLG_ONLYUNQGLOBALTRANS,
1182           FLG_GLOBALSIMPMODIFIESNOTHING,
1183           FLG_PREDBOOLOTHERS, FLG_PREDBOOLPTR, FLG_PREDBOOLINT,
1184           FLG_INTERNALGLOBS, FLG_INTERNALGLOBSNOGLOBS,
1185           FLG_USEALLGLOBS, FLG_RETALIAS, 
1186           FLG_MODGLOBS, FLG_MODGLOBSUNSPEC, FLG_MODGLOBSUNCHECKED,
1187           FLG_RETEXPOSE, FLG_ASSIGNEXPOSE, FLG_CASTEXPOSE,
1188           FLG_NOEFFECTUNCON, FLG_EVALORDERUNCON,
1189           FLG_FUNCUNUSED,
1190           FLG_EXPORTITER, FLG_EXPORTCONST,
1191           FLG_TYPEUNUSED, FLG_FIELDUNUSED, FLG_PARAMUNUSED, FLG_TOPUNUSED,
1192           FLG_CONSTUNUSED, FLG_ENUMMEMUNUSED,
1193           FLG_VARUNUSED, 
1194           FLG_NULLPOINTERARITH, FLG_POINTERARITH, 
1195           FLG_PTRNUMCOMPARE, 
1196           FLG_BOOLCOMPARE, FLG_NOEFFECT, 
1197           FLG_RETVALINT, FLG_RETVALBOOL, FLG_RETVALOTHER, 
1198           FLG_ANSIRESERVED, FLG_ANSIRESERVEDLOCAL, FLG_CPPNAMES,
1199           FLG_RETVALBOOL, FLG_RETVALINT, FLG_SPECUNDEF, 
1200           FLG_DECLUNDEF, FLG_STRICTOPS, FLG_INCONDEFS, 
1201           FLG_MISPLACEDSHAREQUAL, FLG_REDUNDANTSHAREQUAL,
1202           FLG_INCONDEFSLIB, FLG_MATCHFIELDS, FLG_EXPORTMACRO, FLG_EXPORTVAR, 
1203           FLG_EXPORTFCN, FLG_EXPORTTYPE, FLG_EXPORTLOCAL, FLG_MACROPARAMS, 
1204           FLG_MACROASSIGN,
1205           FLG_SEFPARAMS, FLG_SEFUNSPEC, FLG_MACROSTMT, FLG_MACROPARENS, 
1206           FLG_MACROFCNDECL,
1207           FLG_MACROCONSTDECL,
1208           FLG_MACROREDEF, FLG_MACROEMPTY,
1209           FLG_INFLOOPS, FLG_INFLOOPSUNCON,
1210           FLG_UNREACHABLE, 
1211           FLG_NORETURN, FLG_CASEBREAK, FLG_MISSCASE, FLG_USEDEF,
1212           FLG_EVALORDER,
1213           FLG_MODUNCON, FLG_MODUNCONNOMODS, FLG_MODINTERNALSTRICT,
1214           FLG_MODOBSERVERUNCON,
1215
1216           FLG_NESTEDEXTERN, 
1217           FLG_FIRSTCASE,
1218
1219           /* memchecks flags */
1220           FLG_NULLSTATE, FLG_NULLDEREF, FLG_NULLASSIGN,
1221           FLG_NULLPASS, FLG_NULLRET,
1222
1223           FLG_COMPDEF, FLG_COMPMEMPASS, FLG_UNIONDEF,
1224
1225           /* memtrans flags */
1226           FLG_EXPOSETRANS,
1227           FLG_OBSERVERTRANS,
1228           FLG_DEPENDENTTRANS,
1229           FLG_NEWREFTRANS,
1230           FLG_ONLYTRANS,
1231           FLG_OWNEDTRANS,
1232           FLG_FRESHTRANS,
1233           FLG_SHAREDTRANS,
1234           FLG_TEMPTRANS,
1235           FLG_KEPTTRANS,
1236           FLG_REFCOUNTTRANS,
1237           FLG_STATICTRANS,
1238           FLG_UNKNOWNTRANS,
1239           FLG_KEEPTRANS,
1240           FLG_IMMEDIATETRANS,
1241           FLG_STATICINITTRANS,
1242           FLG_UNKNOWNINITTRANS,
1243
1244           FLG_USERELEASED, FLG_ALIASUNIQUE, FLG_MAYALIASUNIQUE,
1245           FLG_MUSTFREE, FLG_MUSTDEFINE, FLG_GLOBSTATE, 
1246           FLG_COMPDESTROY, FLG_MUSTNOTALIAS,
1247           FLG_MEMIMPLICIT,
1248           FLG_BRANCHSTATE, 
1249
1250           FLG_DECLPARAMNAME, FLG_DECLPARAMMATCH,
1251
1252           FLG_SHADOW, FLG_DEPARRAYS, 
1253           FLG_STRICTDESTROY, FLG_STRICTUSERELEASED, FLG_STRICTBRANCHSTATE,
1254           FLG_REDECL, FLG_READONLYSTRINGS, FLG_READONLYTRANS,
1255           FLG_LOOPLOOPBREAK, FLG_LOOPSWITCHBREAK, FLG_SWITCHLOOPBREAK,
1256           FLG_SWITCHSWITCHBREAK, FLG_LOOPLOOPCONTINUE,
1257           FLG_CHECKSTRICTGLOBALS, FLG_IMPCHECKEDSPECGLOBALS,
1258           FLG_ALLGLOBALS, FLG_IMPCHECKEDSTRICTGLOBALS,
1259           FLG_IMPCHECKEDSTRICTSTATICS,
1260           FLG_IMPCHECKEDSTRICTSPECGLOBALS,
1261           FLG_IMPCHECKMODINTERNALS,
1262           FLG_WARNMISSINGGLOBALS, FLG_WARNMISSINGGLOBALSNOGLOBS,
1263           FLG_WARNLINTCOMMENTS, FLG_ANSIRESERVEDLOCAL,
1264           FLG_INCLUDENEST, FLG_STRINGLITERALLEN,
1265           FLG_NUMSTRUCTFIELDS, FLG_NUMENUMMEMBERS,
1266           FLG_CONTROLNESTDEPTH,
1267           FLG_FORBLOCK, FLG_WHILEBLOCK,
1268           FLG_FOREMPTY, FLG_WHILEEMPTY,
1269           FLG_IFEMPTY, FLG_IFBLOCK,
1270           FLG_ELSEIFCOMPLETE,
1271           FLG_REALCOMPARE, FLG_BOOLOPS,
1272           FLG_SYSTEMDIRERRORS, FLG_UNUSEDSPECIAL,
1273
1274           FLG_SHIFTSIGNED, FLG_BITWISEOPS,
1275           FLG_BUFFEROVERFLOWHIGH, FLG_BUFFEROVERFLOW,
1276           INVALID_FLAG
1277         } ;
1278
1279       SETFLAGS ();
1280     }
1281   else
1282     {
1283       llcontbug (message ("context_setMode: bad mode: %s", s));
1284      }
1285 }
1286
1287 bool
1288 context_isSpecialFile (cstring fname)
1289 {
1290   cstring ext = fileLib_getExtension (fname);
1291   
1292   return (cstring_equalLit (ext, ".y") 
1293           || cstring_equalLit (ext, ".l")
1294           || cstring_equalLit (fname, "lex.yy.c"));
1295 }
1296
1297 bool
1298 context_isSystemDir (cstring dir)
1299 {
1300   cstring sysDirs = context_exposeString (FLG_SYSTEMDIRS);
1301   char *thisdir = cstring_toCharsSafe (sysDirs);
1302   char *nextdir = strchr (thisdir, PATH_SEPARATOR);
1303
1304   if (nextdir != NULL)
1305     {
1306       *nextdir = '\0';
1307       nextdir += 1;
1308     }
1309
1310   while (thisdir != NULL)
1311     {
1312       DPRINTF (("Test: %s / %s", dir, thisdir));
1313
1314       if (cstring_equalCanonicalPrefix (dir, thisdir))
1315         {
1316           if (nextdir != NULL)
1317             {
1318               *(nextdir - 1) = PATH_SEPARATOR;
1319             }
1320           
1321           return TRUE;
1322         }
1323
1324       if (nextdir != NULL)
1325         {
1326           *(nextdir - 1) = PATH_SEPARATOR;
1327         }
1328
1329       if (nextdir != NULL)
1330         {
1331           thisdir = nextdir;
1332           nextdir = strchr (thisdir, PATH_SEPARATOR);
1333           
1334           if (nextdir != NULL)
1335             {
1336               *nextdir = '\0';
1337               nextdir += 1;
1338             } 
1339         }
1340       else
1341         {
1342           break;
1343         }
1344     } 
1345
1346   DPRINTF (("Returns FALSE"));
1347   return FALSE;
1348 }
1349
1350 void
1351 context_addFileAccessType (typeId t)
1352 {
1353   cstring base;
1354
1355   if (context_inFunctionLike ())
1356     {
1357       gc.acct = typeIdSet_insert (gc.acct, t);
1358     }
1359   
1360   gc.facct = typeIdSet_insert (gc.facct, t);
1361   
1362   base = fileloc_getBase (g_currentloc);
1363   insertModuleAccess (base, t);
1364   DPRINTF (("Add file access: %s / %s", typeIdSet_unparse (gc.facct),
1365             typeIdSet_unparse (gc.acct)));
1366 }
1367
1368 void
1369 context_removeFileAccessType (typeId t)
1370 {
1371   if (gc.kind == CX_FUNCTION || gc.kind == CX_MACROFCN 
1372       || gc.kind == CX_UNKNOWNMACRO)
1373     {
1374       gc.acct = typeIdSet_removeFresh (gc.acct, t);
1375     }
1376   
1377   gc.facct = typeIdSet_removeFresh (gc.facct, t);
1378   gc.nacct = typeIdSet_insert (gc.nacct, t);
1379 }
1380
1381 void context_enterFunctionHeader (void)
1382 {
1383   if (context_getFlag (FLG_GRAMMAR))
1384     {
1385       lldiagmsg (message ("Enter function header: %q", context_unparse ()));
1386     }
1387
1388   if (gc.kind != CX_GLOBAL)
1389     {
1390       llparseerror (cstring_makeLiteral
1391                     ("Likely parse error.  Function header outside global context."));
1392     }
1393   else
1394     {
1395       llassert (gc.kind == CX_GLOBAL);
1396       DPRINTF (("Enter function header!"));
1397       gc.inFunctionHeader = TRUE;
1398     }
1399 }
1400
1401 void context_exitFunctionHeader (void)
1402 {
1403   if (context_getFlag (FLG_GRAMMAR))
1404     {
1405       lldiagmsg (message ("Exit function header: %q", context_unparse ()));
1406     }
1407
1408   DPRINTF (("Exit function header!"));
1409   gc.inFunctionHeader = FALSE;
1410 }
1411
1412 bool context_inFunctionHeader (void)
1413 {
1414   return (gc.inFunctionHeader);
1415 }
1416
1417 void context_enterFunctionDeclaration (uentry e)
1418 {
1419   if (context_getFlag (FLG_GRAMMAR))
1420     {
1421       lldiagmsg (message ("Enter function declaration: %q", context_unparse ()));
1422     }
1423
1424   DPRINTF (("Enter function decl"));
1425   llassert (gc.savekind == CX_ERROR);
1426   gc.savekind = gc.kind;
1427   gc.savecont = gc.cont;
1428   gc.kind = CX_FCNDECLARATION;
1429   gc.cont.fcn = e;
1430 }
1431
1432 void context_exitFunctionDeclaration (void)
1433 {
1434   if (context_getFlag (FLG_GRAMMAR))
1435     {
1436       lldiagmsg (message ("Exit function declaration: %q", context_unparse ()));
1437     }
1438
1439   DPRINTF (("Exit function decl"));
1440   llassert (gc.savekind != CX_ERROR);
1441   llassert (gc.kind == CX_FCNDECLARATION);
1442   gc.kind = gc.savekind;
1443   gc.cont = gc.savecont;
1444
1445   gc.savekind = CX_ERROR;
1446
1447   if (context_getFlag (FLG_GRAMMAR))
1448     {
1449       lldiagmsg (message ("After exit function declaration: %q", context_unparse ()));
1450     }
1451 }
1452
1453 bool context_inFunctionDeclaration (void)
1454 {
1455   return (gc.kind == CX_FCNDECLARATION);
1456 }
1457
1458
1459 void
1460 context_enterMacro (/*@observer@*/ uentry e)
1461 {
1462   context_enterFunction (e);
1463   gc.kind = CX_MACROFCN;
1464 }
1465
1466 void
1467 context_enterUnknownMacro (/*@dependent@*/ uentry e)
1468 {
1469   llassert (uentry_isFunction (e));
1470   context_enterFunction (e);
1471   gc.kind = CX_UNKNOWNMACRO;
1472 }
1473
1474 void context_enterAndClause (exprNode e)
1475 {
1476   
1477   usymtab_trueBranch (guardSet_copy (exprNode_getGuards (e)));
1478   pushClause (ANDCLAUSE);
1479 }
1480
1481 void context_enterOrClause (exprNode e)
1482 {
1483   usymtab_trueBranch (guardSet_invert (exprNode_getGuards (e)));
1484   pushClause (ORCLAUSE);
1485 }
1486
1487 bool context_inDeepLoop (void)
1488 {
1489   bool inLoop = FALSE;
1490
1491   clauseStack_elements (gc.clauses, el)
1492     {
1493       if (clause_isLoop (el))
1494         {
1495           if (inLoop)
1496             {
1497               return TRUE;
1498             }
1499
1500           inLoop = TRUE;
1501         }
1502     } end_clauseStack_elements;
1503
1504   return FALSE;
1505 }
1506
1507 bool context_inDeepSwitch (void)
1508 {
1509   bool inLoop = FALSE;
1510
1511   clauseStack_elements (gc.clauses, el)
1512     {
1513       if (clause_isSwitch (el))
1514         {
1515           if (inLoop)
1516             {
1517               return TRUE;
1518             }
1519
1520           inLoop = TRUE;
1521         }
1522     } end_clauseStack_elements;
1523
1524   return FALSE;
1525 }
1526
1527 bool context_inDeepLoopSwitch (void)
1528 {
1529   bool inLoop = FALSE;
1530
1531   clauseStack_elements (gc.clauses, el)
1532     {
1533       if (clause_isBreakable (el))
1534         {
1535           if (inLoop)
1536             {
1537               return TRUE;
1538             }
1539
1540           inLoop = TRUE;
1541         }
1542     } end_clauseStack_elements;
1543
1544   return FALSE;
1545 }
1546
1547 clause context_breakClause (void)
1548 {
1549   clauseStack_elements (gc.clauses, el)
1550     {
1551       if (clause_isSwitch (el))
1552         {
1553           return el;
1554         }
1555       else if (clause_isLoop (el))
1556         {
1557           return el;
1558         }
1559       else
1560         {
1561           ;
1562         }
1563     } end_clauseStack_elements;
1564
1565   return NOCLAUSE;
1566 }
1567
1568 clause context_nextBreakClause (void)
1569 {
1570   bool hasOne = FALSE;
1571
1572   clauseStack_elements (gc.clauses, el)
1573     {
1574       if (clause_isBreakable (el))
1575         {
1576           if (hasOne)
1577             {
1578               return el;
1579             }
1580           else
1581             {
1582               hasOne = TRUE;
1583             }
1584         }
1585     } end_clauseStack_elements;
1586
1587   return NOCLAUSE;
1588 }
1589   
1590 bool context_inConditional (void)
1591 {
1592   clauseStack_elements (gc.clauses, el)
1593     {
1594       /*
1595       ** Should also include TRUECLAUSE and FALSECLAUSE, but need
1596       ** to distinguish if from ? for this
1597       */
1598
1599       if (clause_isBreakable (el) && (el != DOWHILECLAUSE))
1600         {
1601           return TRUE;
1602         }
1603     } end_clauseStack_elements;
1604
1605   return FALSE;
1606 }
1607
1608 void context_exitAndClause (exprNode pred, exprNode tbranch)
1609 {
1610   context_setJustPopped ();
1611   
1612   llassert (gc.inclause == ANDCLAUSE);
1613   
1614   usymtab_popAndBranch (pred, tbranch);
1615   clauseStack_pop (gc.clauses);
1616   gc.inclause = topClause (gc.clauses);
1617 }
1618
1619 void context_exitOrClause (exprNode pred, exprNode tbranch)
1620 {
1621   context_setJustPopped ();
1622   
1623   llassert (gc.inclause == ORCLAUSE);
1624   
1625   usymtab_popOrBranch (pred, tbranch);
1626   clauseStack_pop (gc.clauses);
1627   gc.inclause = topClause (gc.clauses);
1628 }
1629
1630 static void context_enterCondClauseAux (clause cl)
1631      /*@modifies gc@*/
1632 {
1633   pushClause (cl);
1634 }
1635
1636 static void context_enterTrueAux (exprNode e, clause cl)
1637    /*@modifies gc@*/
1638 {
1639   usymtab_trueBranch (guardSet_copy (exprNode_getGuards (e)));
1640   pushClause (cl);
1641 }
1642
1643 void context_enterIterClause (void)
1644 {
1645   context_enterTrueAux (exprNode_undefined, ITERCLAUSE);
1646 }
1647
1648 void context_enterDoWhileClause (void)
1649 {
1650   pushClause (DOWHILECLAUSE);
1651 }
1652
1653 void context_enterWhileClause (exprNode e)
1654 {
1655   context_enterTrueAux (e, WHILECLAUSE);
1656 }
1657
1658 void context_enterForClause (exprNode e)
1659 {
1660   context_enterTrueAux (e, FORCLAUSE);
1661 }
1662
1663 void context_enterTrueClause (exprNode e)
1664 {
1665   context_enterTrueAux (e, TRUECLAUSE);
1666 }
1667
1668 void context_enterSwitch (exprNode e)
1669 {
1670   
1671   usymtab_switchBranch (e);
1672   context_enterCondClauseAux (SWITCHCLAUSE);
1673 }
1674
1675 void context_exitSwitch (exprNode e, bool allpaths)
1676 {
1677     usymtab_exitSwitch (e, allpaths);
1678   
1679   while (clause_isCase (clauseStack_top (gc.clauses)))
1680     {
1681       clauseStack_pop (gc.clauses);
1682     }
1683   
1684   context_exitClauseSimp ();
1685 }
1686
1687 void context_enterCaseClause (exprNode e)
1688 {
1689   bool branch = FALSE;
1690  
1691   DPRINTF (("Enter case clause!"));
1692
1693   branch = usymtab_newCase (exprNode_undefined, e);
1694   
1695   if (branch)
1696     {
1697       context_enterCondClauseAux (CASECLAUSE);
1698     }
1699 }
1700
1701 static void context_enterFalseClauseAux (exprNode e, clause cl)
1702      /*@modifies gc@*/
1703 {
1704   usymtab_altBranch (guardSet_invert (exprNode_getGuards (e)));
1705   gc.inclause = cl;
1706   clauseStack_switchTop (gc.clauses, cl);
1707 }
1708
1709 void context_enterFalseClause (exprNode e)
1710 {
1711   
1712   context_enterFalseClauseAux (e, FALSECLAUSE);
1713 }
1714
1715 void
1716 context_enterConstantMacro (/*@exposed@*/ /*@dependent@*/ uentry e)
1717 {
1718   gc.kind = CX_MACROCONST;
1719   gc.cont.fcn = e;
1720   gc.showfunction = context_getFlag (FLG_SHOWFUNC);
1721
1722   gc.acct = typeIdSet_subtract (typeIdSet_union (gc.facct, uentry_accessType (e)), 
1723                                 gc.nacct);
1724
1725   
1726   usymtab_enterScope ();
1727   sRef_enterFunctionScope ();
1728
1729   gc.globs = globSet_undefined;
1730   globSet_clear (gc.globs_used);
1731   gc.mods = sRefSet_undefined;
1732 }
1733
1734 uentry context_getHeader (void)
1735 {
1736   if (!(context_inFunctionLike () || (gc.kind == CX_MACROCONST)))
1737     {
1738       llfatalbug (message ("context_getHeader: bad call: %q",
1739                            context_unparse ()));
1740     }
1741
1742   return (gc.cont.fcn);
1743 }
1744
1745 void
1746 context_setFunctionDefined (fileloc loc)
1747 {
1748   switch (gc.kind)
1749     {
1750     case CX_UNKNOWNMACRO:
1751     case CX_FUNCTION:
1752     case CX_MACROFCN:
1753       uentry_setFunctionDefined (gc.cont.fcn, loc);
1754       break;
1755     default:
1756       /* (not a bug because of parse errors) */
1757       break;
1758     }
1759 }
1760
1761 void
1762 context_enterFunction (/*@exposed@*/ uentry e)
1763 {
1764   gc.kind = CX_FUNCTION;
1765   gc.cont.fcn = e;
1766
1767   DPRINTF (("Enter function: %s", uentry_unparse (e)));
1768
1769   if (uentry_hasAccessType (e))
1770     {
1771       gc.acct = typeIdSet_subtract (typeIdSet_union (gc.facct, uentry_accessType (e)), 
1772                                     gc.nacct);
1773     }
1774   else
1775     {
1776       gc.acct = gc.facct;
1777     }
1778
1779   DPRINTF (("Enter function: %s / %s", uentry_unparse (e), 
1780             typeIdSet_unparse (gc.acct)));
1781
1782   gc.showfunction = context_getFlag (FLG_SHOWFUNC);
1783   
1784   gc.globs = uentry_getGlobs (e);
1785   globSet_clear (gc.globs_used);
1786   gc.mods = uentry_getMods (e);
1787
1788   usymtab_enterFunctionScope (e);
1789   sRef_enterFunctionScope ();
1790 }
1791
1792 void
1793 context_enterOldStyleScope (void)
1794 {
1795   gc.kind = CX_OLDSTYLESCOPE;
1796   DPRINTF (("Enter old style scope!"));
1797   usymtab_enterFunctionScope (uentry_undefined);
1798 }
1799
1800 void 
1801 context_completeOldStyleFunction (uentry e)
1802 {
1803   llassert (gc.kind == CX_OLDSTYLESCOPE);
1804
1805   gc.kind = CX_FUNCTION;
1806   gc.cont.fcn = e;
1807   
1808   DPRINTF (("Enter function: %s", uentry_unparse (e)));
1809   
1810   if (uentry_hasAccessType (e))
1811     {
1812       gc.acct = typeIdSet_subtract (typeIdSet_union (gc.facct, uentry_accessType (e)), 
1813                                     gc.nacct);
1814     }
1815   else
1816     {
1817       gc.acct = gc.facct;
1818     }
1819   
1820   DPRINTF (("Enter function: %s / %s", uentry_unparse (e), 
1821             typeIdSet_unparse (gc.acct)));
1822   
1823   gc.showfunction = context_getFlag (FLG_SHOWFUNC);
1824   
1825   if (!globSet_isEmpty (uentry_getGlobs (e))) 
1826     {
1827       llfatalerror (message ("%q: Old-style function declaration uses a clause (rewrite with function parameters): %q",
1828                              fileloc_unparse (g_currentloc), uentry_unparse (e)));
1829     }
1830
1831   gc.showfunction = context_getFlag (FLG_SHOWFUNC);
1832   
1833   gc.globs = uentry_getGlobs (e);
1834   globSet_clear (gc.globs_used);
1835
1836   gc.mods = uentry_getMods (e);
1837
1838   if (!sRefSet_isEmpty (gc.mods))
1839     {
1840       llfatalerror (message ("%q: Old-style function declaration uses a clause (rewrite with function parameters): %q",
1841                              fileloc_unparse (g_currentloc), uentry_unparse (e)));
1842     }
1843
1844   sRef_enterFunctionScope ();
1845 }
1846
1847 static bool context_checkStrictGlobals (void)
1848 {
1849   return (context_getFlag (FLG_CHECKSTRICTGLOBALS));
1850 }
1851
1852 static bool context_hasGlobs (void)
1853 {
1854   if (context_inFunctionLike ())
1855     {
1856       return (uentry_hasGlobs (gc.cont.fcn));
1857     }
1858   else
1859     {
1860       return (FALSE);
1861     }
1862 }
1863
1864 static bool context_checkCheckedGlobals (void)
1865 {
1866   return (context_getFlag (FLG_GLOBALS)
1867           && (context_getFlag (FLG_GLOBUNSPEC)
1868               || context_hasGlobs ()));
1869 }
1870
1871 static bool context_checkUnknownGlobals (void)
1872 {
1873   /* should be uentry_hasGlobs ? */
1874
1875   return (context_getFlag (FLG_ALLGLOBALS)
1876           && (context_getFlag (FLG_GLOBUNSPEC)
1877               || context_hasGlobs ()));
1878 }      
1879
1880 static bool context_checkUncheckedGlobals (void)
1881 {
1882   return (FALSE);
1883 }      
1884
1885 bool 
1886 context_checkExport (uentry e)
1887 {
1888   if (!gc.anyExports) return FALSE;
1889
1890   if (uentry_isFunction (e)
1891       || (uentry_isVariable (e) && ctype_isFunction (uentry_getType (e))))
1892     {
1893       return context_maybeSet (FLG_EXPORTFCN);
1894     }
1895   else if (uentry_isExpandedMacro (e))
1896     {
1897       return context_maybeSet (FLG_EXPORTMACRO);
1898     }
1899   else if (uentry_isVariable (e))
1900     {
1901       return context_maybeSet (FLG_EXPORTVAR);
1902     }
1903   else if (uentry_isEitherConstant (e))
1904     {
1905       return context_maybeSet (FLG_EXPORTCONST);
1906     }
1907   else if (uentry_isIter (e) || uentry_isEndIter (e))
1908     {
1909       return context_maybeSet (FLG_EXPORTITER);
1910     }
1911   else if (uentry_isDatatype (e))
1912     {
1913       return context_maybeSet (FLG_EXPORTTYPE);
1914     }
1915   else
1916     {
1917       BADEXIT;
1918     }
1919 }
1920               
1921 bool
1922 context_checkGlobUse (uentry glob)
1923 {
1924   
1925   if (uentry_isCheckedStrict (glob))
1926     {
1927       return context_checkStrictGlobals ();
1928     }
1929   else if (uentry_isChecked (glob))
1930     {
1931       return context_checkCheckedGlobals ();
1932     }
1933   else if (uentry_isCheckedUnknown (glob) || uentry_isCheckMod (glob))
1934     {
1935       return context_checkUnknownGlobals ();
1936     }
1937   else 
1938     {
1939       llassert (uentry_isUnchecked (glob));
1940
1941       return context_checkUncheckedGlobals ();
1942     }
1943 }
1944
1945 bool
1946 context_checkAliasGlob (uentry glob)
1947 {
1948   if (uentry_isCheckedStrict (glob))
1949     {
1950       return gc.flags[FLG_CHECKSTRICTGLOBALIAS];
1951     }
1952   else if (uentry_isChecked (glob))
1953     {
1954       return gc.flags[FLG_CHECKEDGLOBALIAS];
1955     }
1956   else if (uentry_isCheckMod (glob))
1957     {
1958       return gc.flags[FLG_CHECKMODGLOBALIAS];
1959     }
1960   else 
1961     {
1962       llassert (uentry_isUnchecked (glob) || uentry_isCheckedUnknown (glob));
1963
1964       return gc.flags[FLG_UNCHECKEDGLOBALIAS];
1965     }
1966 }
1967
1968 bool context_checkInternalUse (void)
1969 {
1970   if (context_hasGlobs ())
1971     {
1972       return (gc.flags[FLG_INTERNALGLOBS]);
1973     }
1974   else
1975     {
1976       return (gc.flags[FLG_INTERNALGLOBSNOGLOBS]);
1977     }
1978 }
1979
1980 bool
1981 context_checkGlobMod (sRef el)
1982 {
1983   uentry ue = sRef_getUentry (el);
1984
1985   /* no: llassert (sRef_isFileOrGlobalScope (el)); also check local statics */
1986
1987   if (uentry_isCheckedModify (ue)
1988       || (!uentry_isUnchecked (ue) && (gc.flags[FLG_ALLGLOBALS])))
1989     {
1990       if (context_hasMods ())
1991         {
1992           return (gc.flags[FLG_MODGLOBS]);
1993         }
1994       else
1995         {
1996           if (uentry_isCheckedStrict (ue))
1997             {
1998               return (gc.flags[FLG_MODGLOBSUNSPEC]);
1999             }
2000           else
2001             {
2002               return (gc.flags[FLG_MODSTRICTGLOBSUNSPEC]);
2003             }
2004         }
2005     }
2006   else
2007     {
2008       if (context_hasMods ())
2009         {
2010           return (gc.flags[FLG_MODGLOBSUNCHECKED]);
2011         }
2012       else
2013         {
2014           return FALSE;
2015         }
2016     }
2017 }
2018
2019 void
2020 context_usedGlobal (/*@exposed@*/ sRef el)
2021 {
2022   if (!globSet_member (gc.globs_used, el))
2023     {
2024       /* 
2025       ** The first time a global is used in a function, we need
2026       ** to clear the derived sRefs, since they were set for the
2027       ** previous function.
2028       */
2029
2030       sRef_clearDerived (el);
2031       gc.globs_used = globSet_insert (gc.globs_used, el);
2032     }
2033 }
2034
2035 /*@observer@*/ sRefSet
2036 context_modList (void)
2037 {
2038   return gc.mods;
2039 }
2040
2041 bool
2042 context_globAccess (sRef s)
2043 {
2044   llassert (sRef_isFileOrGlobalScope (s) || sRef_isKindSpecial (s));
2045   return (globSet_member (gc.globs, s));
2046 }
2047
2048 bool
2049 context_hasAccess (typeId t)
2050 {
2051   if (context_inFunctionLike ())
2052     {
2053       DPRINTF (("Access %d / %s",
2054                 t, typeIdSet_unparse (gc.acct)));
2055       return (typeIdSet_member (gc.acct, t));
2056     }
2057   else
2058     {
2059       return (context_hasFileAccess (t));
2060     }
2061 }
2062
2063 bool
2064 context_hasFileAccess (typeId t)
2065 {
2066   return (typeIdSet_member (gc.facct, t));
2067 }
2068
2069 /*@only@*/ cstring
2070 context_unparseAccess (void)
2071 {
2072   return (message ("%q / %q", typeIdSet_unparse (gc.acct),
2073                    typeIdSet_unparse (gc.facct)));
2074 }
2075
2076 /*@only@*/ cstring
2077 context_unparseClauses (void)
2078 {
2079   return (clauseStack_unparse (gc.clauses));
2080 }
2081
2082 bool
2083 context_couldHaveAccess (typeId t)
2084 {
2085   if (gc.kind == CX_FUNCTION || gc.kind == CX_MACROFCN || gc.kind == CX_UNKNOWNMACRO)
2086     {
2087       return (typeIdSet_member (gc.acct, t));
2088     }
2089   else
2090     {
2091       return (typeIdSet_member (gc.facct, t)); 
2092     }
2093 }
2094
2095 ctype
2096 context_getRetType (void)
2097 {
2098   ctype f = ctype_undefined;
2099
2100   if (gc.kind == CX_FUNCTION || gc.kind == CX_MACROFCN)
2101     {
2102       f = uentry_getType (gc.cont.fcn);
2103     }
2104   else if (gc.kind == CX_UNKNOWNMACRO)
2105     {
2106       return ctype_unknown;
2107     }
2108   else
2109     {
2110       llcontbuglit ("context_getRetType: not in a function context");
2111       return ctype_unknown;
2112     }
2113
2114   if (!ctype_isFunction (f))
2115     {
2116       if (ctype_isKnown (f))
2117         {
2118           llbuglit ("context_getRetType: not a function");
2119         }
2120
2121       return ctype_unknown;
2122     }
2123   return (ctype_getReturnType (f));
2124 }    
2125
2126 bool
2127 context_hasMods (void)
2128 {
2129   if (context_inFunctionLike ())
2130     {
2131       return (uentry_hasMods (gc.cont.fcn));
2132     }
2133   else
2134     {
2135       return FALSE;
2136     }
2137 }
2138
2139 void
2140 context_exitAllClauses (void)
2141 {  
2142   while (!clauseStack_isEmpty (gc.clauses))
2143     {
2144       clause el = clauseStack_top (gc.clauses);
2145       gc.inclause = el;
2146
2147       if (clause_isNone (el))
2148         {
2149           usymtab_quietExitScope (g_currentloc);
2150           clauseStack_pop (gc.clauses);
2151         }
2152       else
2153         {
2154           context_exitClausePlain ();
2155         }
2156     }
2157
2158   clauseStack_clear (gc.clauses);  
2159   gc.inclause = NOCLAUSE;
2160 }
2161
2162 void
2163 context_exitAllClausesQuiet (void)
2164 {  
2165   while (!clauseStack_isEmpty (gc.clauses))
2166     {
2167       clause el = clauseStack_top (gc.clauses);
2168       gc.inclause = el;
2169
2170       usymtab_quietExitScope (g_currentloc);
2171       clauseStack_pop (gc.clauses);
2172     }
2173
2174   clauseStack_clear (gc.clauses);  
2175   gc.inclause = NOCLAUSE;
2176 }
2177
2178 static
2179 void context_exitClauseSimp (void)
2180 {
2181   
2182   context_setJustPopped ();
2183   clauseStack_pop (gc.clauses);
2184   gc.inclause = topClause (gc.clauses);
2185 }
2186
2187 static
2188 void context_exitCaseClause (void)
2189 {
2190   context_setJustPopped ();
2191   usymtab_popCaseBranch ();
2192   clauseStack_pop (gc.clauses);
2193   gc.inclause = topClause (gc.clauses);
2194 }
2195
2196 static
2197 void context_exitClauseAux (exprNode pred, exprNode tbranch)
2198 {
2199   context_setJustPopped ();
2200   /*@i32 was makeAlt */
2201   usymtab_popTrueBranch (pred, tbranch, gc.inclause);
2202   clauseStack_pop (gc.clauses);
2203   gc.inclause = topClause (gc.clauses);
2204 }
2205
2206 void context_exitTrueClause (exprNode pred, exprNode tbranch)
2207 {
2208   DPRINTF (("Exit true clause: %s", exprNode_unparse (tbranch)));
2209
2210   if (gc.inclause != TRUECLAUSE)
2211     {
2212       llparseerror (cstring_makeLiteral
2213                     ("Likely parse error.  Conditional clauses are inconsistent."));
2214       return;
2215     }
2216   
2217   context_setJustPopped ();  
2218   usymtab_popTrueBranch (pred, tbranch, TRUECLAUSE);
2219   clauseStack_pop (gc.clauses);
2220   gc.inclause = topClause (gc.clauses);  
2221 }
2222
2223 void context_exitIterClause (exprNode body)
2224 {
2225   llassert (gc.inclause == ITERCLAUSE);
2226
2227   context_setJustPopped ();
2228
2229   if (context_getFlag (FLG_LOOPEXEC))
2230     {
2231       usymtab_popTrueExecBranch (exprNode_undefined, body, ITERCLAUSE);
2232     }
2233   else
2234     {
2235       usymtab_popTrueBranch (exprNode_undefined, body, ITERCLAUSE);
2236     }
2237
2238   clauseStack_pop (gc.clauses);
2239   gc.inclause = topClause (gc.clauses);
2240 }
2241
2242 static void context_popCase (void) {
2243   /*
2244   ** If we are exiting an outer clause, sometimes still in a switch case.
2245   **
2246   ** e.g.: 
2247   **
2248   ** switch(a)
2249   ** {
2250   **   case 1:
2251   **     while (c>3)
2252   **       {
2253   **         case 3: ++c;
2254   **       }
2255   ** }     
2256   */
2257
2258   DPRINTF (("Popping case clause: %s",
2259             clauseStack_unparse (gc.clauses)));
2260   
2261   if (gc.inclause == CASECLAUSE) {
2262     context_exitCaseClause ();
2263   }
2264 }
2265
2266 void context_exitWhileClause (exprNode pred, exprNode body)
2267 {
2268   guardSet invGuards = guardSet_invert (exprNode_getGuards (pred));
2269
2270   context_popCase (); 
2271
2272   if (gc.inclause != WHILECLAUSE) {
2273     DPRINTF (("Clause: %s / %s", clause_unparse (gc.inclause),
2274               clauseStack_unparse (gc.clauses)));
2275   }
2276
2277   llassert (gc.inclause == WHILECLAUSE);
2278
2279   context_setJustPopped ();
2280
2281   
2282   /*
2283   ** predicate must be false after while loop (unless there are breaks)
2284   */
2285
2286   if (context_getFlag (FLG_LOOPEXEC))
2287     {
2288       usymtab_popTrueExecBranch (pred, body, WHILECLAUSE);
2289     }
2290   else
2291     {
2292       usymtab_popTrueBranch (pred, body, WHILECLAUSE);
2293     }
2294
2295   
2296   usymtab_addGuards (invGuards);
2297   guardSet_free (invGuards);
2298
2299   clauseStack_pop (gc.clauses);
2300   gc.inclause = topClause (gc.clauses);  
2301 }
2302
2303 void context_exitDoWhileClause (exprNode pred)
2304 {
2305   guardSet invGuards = guardSet_invert (exprNode_getGuards (pred));
2306
2307   if (gc.inclause == CASECLAUSE) {
2308     /* handle Duff's device */
2309     clauseStack_pop (gc.clauses);
2310     gc.inclause = topClause (gc.clauses);
2311   }
2312
2313   llassert (gc.inclause == DOWHILECLAUSE);
2314
2315   context_setJustPopped ();
2316
2317     
2318   usymtab_addGuards (invGuards);
2319   guardSet_free (invGuards);
2320
2321   clauseStack_pop (gc.clauses);
2322   gc.inclause = topClause (gc.clauses);  
2323 }
2324
2325 void context_exitForClause (exprNode forPred, exprNode body)
2326 {
2327   guardSet invGuards = guardSet_invert (exprNode_getForGuards (forPred));
2328
2329   llassert (gc.inclause == FORCLAUSE);
2330   context_setJustPopped ();
2331
2332   /*
2333   ** predicate must be false after while loop (unless there are breaks)
2334   */
2335
2336   if (context_getFlag (FLG_LOOPEXEC))
2337     {
2338       usymtab_popTrueExecBranch (forPred, body, FORCLAUSE);
2339     }
2340   else
2341     {
2342       usymtab_popTrueBranch (forPred, body, FORCLAUSE);
2343     }
2344
2345   usymtab_addGuards (invGuards);
2346   guardSet_free (invGuards);
2347   clauseStack_pop (gc.clauses);
2348   gc.inclause = topClause (gc.clauses);
2349 }
2350
2351 static void context_exitClausePlain (void)
2352 {
2353   llassert (gc.inclause != NOCLAUSE);
2354   
2355   if (gc.inclause == FALSECLAUSE)
2356     {
2357       context_exitClause (exprNode_undefined, exprNode_undefined, exprNode_undefined);
2358     }
2359   else
2360     {
2361       context_exitClauseAux (exprNode_undefined, exprNode_undefined);
2362     }
2363 }
2364
2365 void context_exitClause (exprNode pred, exprNode tbranch, exprNode fbranch)
2366 {
2367   context_setJustPopped ();
2368   
2369   if (gc.inclause == FALSECLAUSE)
2370     {
2371       usymtab_popBranches (pred, tbranch, fbranch, FALSE, FALSECLAUSE);
2372       
2373       llassert (clauseStack_top (gc.clauses) == FALSECLAUSE);
2374
2375       clauseStack_pop (gc.clauses);
2376       gc.inclause = topClause (gc.clauses);
2377     }
2378   else
2379     {
2380       context_exitTrueClause (pred, tbranch);
2381     }
2382 }
2383
2384 void
2385 context_returnFunction (void)
2386 {
2387   usymtab_checkFinalScope (TRUE);
2388 }
2389
2390 void
2391 context_exitFunction (void)
2392 {    
2393   DPRINTF (("Exit function: %s", context_unparse ()));
2394
2395   if (!context_inFunction () && !context_inMacroConstant () 
2396       && !context_inUnknownMacro () 
2397       && !context_inIterDef () && !context_inIterEnd ())
2398     {
2399       /*
2400       ** not a bug because of parse errors
2401       */
2402
2403       BADBRANCH;
2404     }
2405   else
2406     {
2407       if (context_inMacro () && usymtab_inFunctionScope ())
2408         {
2409           usymtab_exitScope (exprNode_undefined);
2410         }
2411       
2412       if (uentry_hasGlobs (gc.cont.fcn))
2413         {
2414           exprChecks_checkUsedGlobs (gc.globs, gc.globs_used);
2415         }
2416       
2417             
2418       if (uentry_hasMods (gc.cont.fcn))
2419         {
2420           if (context_getFlag (FLG_MUSTMOD))
2421             {
2422               exprNode_checkAllMods (gc.mods, gc.cont.fcn);
2423             }
2424         }
2425
2426       DPRINTF (("Exit function: %s", uentry_unparse (gc.cont.fcn)));
2427
2428       /*
2429       ** clear file static modifies
2430       */
2431       
2432       /* do this first to get unused error messages */
2433
2434       usymtab_exitScope (exprNode_undefined);
2435       sRef_exitFunctionScope ();
2436       
2437       gc.showfunction = FALSE;
2438       gc.kind = CX_GLOBAL;
2439       gc.cont.glob = TRUE;
2440       gc.acct = gc.facct; 
2441       gc.globs = globSet_new ();
2442       globSet_clear (gc.globs_used);
2443       gc.mods = sRefSet_new ();
2444     }
2445
2446   llassert (clauseStack_isEmpty (gc.clauses));
2447   llassert (gc.inclause == NOCLAUSE);
2448   DPRINTF (("After exit function: %s", context_unparse ()));
2449 }
2450
2451 void
2452 context_quietExitFunction (void)
2453 {
2454   while (gc.kind == CX_INNER)
2455     { 
2456       context_exitInnerPlain ();
2457     }
2458
2459   if (!context_inFunction () && !context_inMacroConstant () && !context_inUnknownMacro () 
2460       && !context_inIterDef () && !context_inIterEnd ())
2461     {
2462     }
2463   else
2464     {
2465       usymtab_quietExitScope (g_currentloc);
2466
2467       gc.showfunction = FALSE;
2468       gc.kind = CX_GLOBAL;
2469       gc.cont.glob = TRUE;
2470       gc.acct = gc.facct; 
2471       gc.globs = globSet_new ();
2472       globSet_clear (gc.globs_used);
2473       gc.mods = sRefSet_new ();
2474
2475       sRef_exitFunctionScope ();
2476     }
2477 }
2478
2479 /*@observer@*/ uentryList
2480 context_getParams (void)
2481 {
2482   if (context_inFunctionLike ())
2483     {
2484       return (uentry_getParams (gc.cont.fcn));
2485     }
2486   else
2487     {
2488       llcontbug (message ("context_getParams: not in function: %q", context_unparse ()));
2489       return uentryList_undefined;
2490     }
2491 }
2492
2493 /*@observer@*/ globSet
2494 context_getUsedGlobs (void)
2495 {
2496   llassert (gc.kind == CX_FUNCTION || gc.kind == CX_MACROFCN 
2497             || gc.kind == CX_UNKNOWNMACRO || gc.kind == CX_ITERDEF);
2498
2499   return (gc.globs_used);
2500 }
2501
2502 cstring
2503 context_moduleName ()
2504 {
2505   return (fileloc_getBase (g_currentloc));
2506 }
2507
2508 /*@observer@*/ globSet
2509 context_getGlobs (void)
2510 {
2511   llassert (gc.kind == CX_FUNCTION || gc.kind == CX_MACROFCN 
2512             || gc.kind == CX_UNKNOWNMACRO || gc.kind == CX_ITERDEF);
2513
2514   return (gc.globs);
2515 }
2516
2517 void
2518 context_addBoolAccess (void)
2519 {
2520   cstring bname = context_getString (FLG_BOOLTYPE);
2521   typeIdSet boolt = typeIdSet_single (usymtab_getTypeId (bname));
2522   
2523   addModuleAccess (cstring_copy (bname), boolt);
2524
2525   /* for sys/types (perhaps, this is bogus!) */ 
2526   addModuleAccess (cstring_makeLiteral ("types"), boolt); 
2527 }
2528
2529 # if 0
2530 bool
2531 context_canAccessBool (void)
2532 {
2533   return TRUE;
2534 }
2535 # endif
2536
2537 /*
2538   static typeId boolType = typeId_invalid;
2539
2540   if (typeId_isInvalid (boolType))
2541     { 
2542       boolType = usymtab_getTypeId (context_getBoolName ());
2543     }
2544
2545   if (typeId_isInvalid (boolType)) {
2546     return FALSE;
2547   } else {
2548     return (typeIdSet_member (gc.acct, boolType));
2549   }
2550 }
2551 */
2552
2553 /* evs 2000-07-25: old version - replaced */
2554
2555 ctype
2556 context_boolImplementationType () {
2557   /* For now, this is bogus! */
2558   return ctype_int;
2559 }
2560
2561 bool
2562 context_canAccessBool (void)
2563 {
2564   static typeId boolType = typeId_invalid;
2565
2566   if (typeId_isInvalid (boolType))
2567     { 
2568       boolType = usymtab_getTypeId (context_getBoolName ());
2569     }
2570
2571   if (!typeId_isInvalid (boolType))
2572     { 
2573       return context_hasAccess (boolType);
2574     }
2575   else 
2576     {
2577       ;
2578     }
2579
2580   return FALSE; 
2581 }
2582
2583 void
2584 context_setMessageAnnote (/*@only@*/ cstring s)
2585 {
2586   llassert (cstring_isUndefined (gc.msgAnnote));
2587     gc.msgAnnote = s;
2588 }
2589
2590 bool
2591 context_hasMessageAnnote (void)
2592 {
2593   return (cstring_isDefined (gc.msgAnnote));
2594 }
2595
2596 void
2597 context_clearMessageAnnote (void)
2598 {
2599   if (cstring_isDefined (gc.msgAnnote))
2600     {
2601       cstring_free (gc.msgAnnote);
2602       gc.msgAnnote = cstring_undefined;
2603     }
2604 }
2605
2606 /*@only@*/ cstring
2607 context_getMessageAnnote (void)
2608 {
2609   cstring st = gc.msgAnnote;
2610
2611     gc.msgAnnote = cstring_undefined;
2612   return st;
2613 }
2614
2615 void
2616 context_setAliasAnnote (/*@observer@*/ sRef s, /*@observer@*/ sRef t)
2617 {
2618     llassert (sRef_isInvalid (gc.aliasAnnote));
2619   llassert (!sRef_sameName (s, t));
2620   gc.aliasAnnote = s;
2621   gc.aliasAnnoteAls = t;
2622 }
2623
2624 bool
2625 context_hasAliasAnnote (void)
2626 {
2627   return (sRef_isValid (gc.aliasAnnote));
2628 }
2629
2630 void
2631 context_clearAliasAnnote (void)
2632 {
2633   gc.aliasAnnote = sRef_undefined;
2634 }
2635
2636 cstring
2637 context_getAliasAnnote (void)
2638 {
2639   sRef ret = gc.aliasAnnote;
2640   sRef als = gc.aliasAnnoteAls;
2641
2642   llassert (sRef_isValid (ret) && sRef_isValid (als));
2643
2644   gc.aliasAnnote = sRef_undefined;
2645   return (message ("%q aliases %q", sRef_unparse (als), sRef_unparse (ret)));
2646 }
2647
2648 void
2649 context_recordFileModifies (sRefSet mods)
2650 {
2651   gc.modrecs = sRefSetList_add (gc.modrecs, mods);
2652 }
2653
2654 void
2655 context_recordFileGlobals (globSet mods)
2656 {
2657   DPRINTF (("Recording file globals: %s", globSet_unparse (mods)));
2658   /*@access globSet@*/ context_recordFileModifies (mods); /*@noaccess globSet@*/
2659 }
2660
2661 void
2662 context_setCommentMarkerChar (char c)
2663 {
2664   llassert (c != '\0');
2665
2666   context_setValue (FLG_COMMENTCHAR, (int) c);
2667 }
2668
2669 char
2670 context_getCommentMarkerChar (void)
2671 {
2672   return ((char) context_getValue (FLG_COMMENTCHAR));
2673 }
2674
2675 static void
2676 context_setValue (flagcode flag, int val)
2677 {
2678   int index = flagcode_valueIndex (flag);
2679
2680   llassert (index >= 0 && index <= NUMVALUEFLAGS);
2681
2682   switch (flag)
2683     {
2684     case FLG_LINELEN:
2685       if (val <= 0)
2686         {
2687           
2688           llerror_flagWarning (message ("Value for %s must be a positive "
2689                                     "number (given %d)",
2690                                     flagcode_unparse (flag), val));
2691           return;
2692         }
2693       if (flag == FLG_LINELEN && val < MINLINELEN)
2694         {
2695           llerror_flagWarning (message ("Value for %s must be at least %d (given %d)",
2696                                     flagcode_unparse (flag), 
2697                                     MINLINELEN, val));
2698           val = MINLINELEN;
2699         }
2700       break;
2701
2702     case FLG_INCLUDENEST:
2703     case FLG_CONTROLNESTDEPTH:
2704     case FLG_STRINGLITERALLEN:
2705     case FLG_NUMSTRUCTFIELDS:
2706     case FLG_NUMENUMMEMBERS:      
2707     case FLG_INDENTSPACES:
2708       if (val < 0)
2709         {
2710           
2711           llerror_flagWarning (message ("Value for %s must be a non-negative "
2712                                     "number (given %d)",
2713                                     flagcode_unparse (flag), val));
2714           return;
2715         }
2716
2717       break;
2718     default:
2719       break;
2720     }
2721     
2722   gc.values[index] = val;
2723 }
2724
2725 void
2726 context_setValueAndFlag (flagcode flag, int val)
2727 {
2728   gc.flags[flag] = TRUE;
2729   context_setValue (flag, val);
2730 }
2731
2732 int
2733 context_getValue (flagcode flag)
2734 {
2735   int index = flagcode_valueIndex (flag);
2736
2737   llassert (index >= 0 && index <= NUMVALUEFLAGS);
2738   return (gc.values[index]);
2739 }
2740
2741 int
2742 context_getCounter (flagcode flag)
2743 {
2744   int index = flagcode_valueIndex (flag);
2745
2746   llassert (index >= 0 && index <= NUMVALUEFLAGS);
2747   return (gc.counters[index]);
2748 }
2749
2750 void
2751 context_incCounter (flagcode flag)
2752 {
2753   int index = flagcode_valueIndex (flag);
2754
2755   llassert (index >= 0 && index <= NUMVALUEFLAGS);
2756   /* check limit */
2757   gc.counters[index]++;
2758 }
2759
2760 void
2761 context_decCounter (flagcode flag)
2762 {
2763   int index = flagcode_valueIndex (flag);
2764
2765   llassert (index >= 0 && index <= NUMVALUEFLAGS);
2766   gc.counters[index]--;
2767 }
2768
2769 bool context_showFunction (void)
2770 {
2771   return (gc.showfunction);
2772 }
2773
2774 void
2775 context_setString (flagcode flag, cstring val)
2776 {
2777   int index = flagcode_stringIndex (flag);
2778
2779   llassert (index >= 0 && index <= NUMSTRINGFLAGS);
2780
2781   if (flag == FLG_SYSTEMDIRS)
2782     {
2783       llassert (cstring_isDefined (val));
2784
2785       if (cstring_firstChar (val) == '\"')
2786         {
2787           cstring oval = val;
2788           cstring tval = cstring_copy (cstring_suffix (val, 1));
2789         
2790           if (cstring_lastChar (tval) != '\"')
2791             {
2792               int n = cstring_length (tval) - 1;
2793
2794               while (isspace ((int) cstring_getChar (tval, n)))
2795                 {
2796                   n--;
2797                 }
2798
2799               if (cstring_getChar (tval, n) != '\"')
2800                 {
2801                   llerror_flagWarning (message ("Setting -systemdirs to string with unmatching quotes: %s", val));
2802                 }
2803               else
2804                 {
2805                   cstring otval = tval;
2806                   tval = cstring_prefix (tval, n);
2807                   cstring_free (otval);
2808                 }
2809             }
2810           
2811           val = cstring_copy (cstring_clip (tval, cstring_length (tval) - 1));
2812           DPRINTF (("val = %s", val));
2813           cstring_free (tval);
2814           cstring_free (oval);
2815         }
2816     }
2817
2818   if (flag == FLG_TMPDIR)
2819     {
2820       llassert (cstring_isDefined (val));
2821       
2822       if (cstring_length (val) == 0)
2823         {
2824           cstring_free (val);
2825           val = message (".%s", cstring_makeLiteralTemp (CONNECTSTR));
2826         }
2827       else if (cstring_lastChar (val) != CONNECTCHAR)
2828         {
2829           val = cstring_appendChar (val, CONNECTCHAR);
2830         }
2831       else
2832         {
2833           ;
2834         }
2835     }
2836
2837   if (cstring_length (val) >= 1
2838       && cstring_firstChar (val) == '"')
2839     {
2840       llerror_flagWarning (message
2841                        ("setting %s to string beginning with \".  You probably "
2842                         "don't meant to have the \"'s.",
2843                         flagcode_unparse (flag)));
2844     }
2845
2846   if (flag == FLG_BOOLTYPE)
2847     {
2848
2849     }
2850
2851   gc.strings[index] = val;
2852 }
2853
2854 static /*@exposed@*/ cstring
2855 context_exposeString (flagcode flag)
2856 {
2857   int index = flagcode_stringIndex (flag);
2858
2859   llassert (index >= 0 && index <= NUMSTRINGFLAGS);
2860   return (gc.strings[index]);
2861 }
2862
2863 cstring
2864 context_getString (flagcode flag)
2865 {
2866   return (context_exposeString (flag));
2867 }
2868
2869 void
2870 context_resetErrors (void)
2871 {
2872   gc.numerrors = 0;
2873 }
2874
2875 void context_initMod (void)
2876    /*@globals undef gc; @*/
2877 {
2878   gc.kind = CX_GLOBAL;
2879
2880   gc.savekind = CX_ERROR;
2881   gc.savecont.glob = FALSE;
2882
2883   gc.instandardlib = FALSE;
2884   gc.numerrors = 0;
2885   gc.neednl = FALSE;
2886   gc.linesprocessed = 0;
2887   gc.speclinesprocessed = 0;
2888   gc.insuppressregion = FALSE;
2889   gc.macroMissingParams = FALSE;
2890   gc.preprocessing = FALSE;
2891   gc.incommandline = FALSE;
2892   gc.mc = macrocache_create ();
2893   gc.nmods = 0;
2894   gc.maxmods = DEFAULTMAXMODS;
2895   gc.moduleaccess = (maccesst *) dmalloc (sizeof (*gc.moduleaccess) * (gc.maxmods));
2896   
2897   gc.library = FLG_ANSILIB;
2898
2899   gc.locstack = filelocStack_new ();
2900   gc.modrecs = sRefSetList_undefined;
2901   gc.anyExports = FALSE;
2902
2903   gc.ftab = fileTable_create ();
2904   gc.msgLog = messageLog_new ();
2905   gc.inimport = FALSE;
2906   gc.inDerivedFile = FALSE;
2907   gc.inheader = FALSE;
2908   gc.markers = flagMarkerList_new ();
2909   gc.cont.glob = TRUE;
2910   gc.showfunction = FALSE;
2911   gc.msgAnnote = cstring_undefined;
2912   gc.aliasAnnote = sRef_undefined;
2913   gc.aliasAnnoteAls = sRef_undefined;
2914   gc.boolType = ctype_bool;
2915   gc.mods = sRefSet_new ();
2916
2917   gc.saveloc = fileloc_undefined;
2918
2919   gc.inmacrocache = FALSE;
2920   gc.inclause = NOCLAUSE;
2921   gc.clauses = clauseStack_new ();
2922   gc.globs = globSet_new ();
2923   gc.nacct = typeIdSet_emptySet ();
2924   gc.acct = typeIdSet_emptySet ();
2925   gc.facct = typeIdSet_emptySet ();
2926   gc.savedFlags = FALSE;
2927   gc.pushloc = fileloc_undefined;
2928   gc.protectVars = FALSE;
2929   gc.justpopped = FALSE;
2930   gc.isNullGuarded = NO;
2931   gc.globs_used = globSet_undefined;
2932   
2933   allFlagCodes (code)
2934     {
2935       gc.setGlobally[code] = FALSE;
2936       gc.setLocally[code] = FALSE;
2937     } end_allFlagCodes ;
2938
2939   usymtab_initMod ();
2940
2941   context_resetAllFlags ();
2942   assertSet (gc.flags); /* Can't use global in defines */
2943   assertSet (gc.saveflags);
2944   assertSet (gc.values);
2945   assertSet (gc.strings);
2946
2947   conext_resetAllCounters ();
2948   assertSet (gc.counters);
2949
2950   context_setMode (DEFAULT_MODE);
2951
2952   gc.stateTable = metaStateTable_create ();
2953   gc.annotTable = annotationTable_create ();
2954
2955   gc.inFunctionHeader = FALSE;
2956
2957   DPRINTF (("Annotations: \n%s",
2958             cstring_toCharsSafe (annotationTable_unparse (gc.annotTable))));
2959   DPRINTF (("State: \n%s",
2960             cstring_toCharsSafe (metaStateTable_unparse (gc.stateTable))));
2961
2962 }
2963
2964 ctype
2965 context_typeofZero (void)
2966 {
2967   ctype ct = ctype_int;
2968
2969   if (context_getFlag (FLG_ZEROPTR))
2970     {
2971       ct = ctype_makeConj (ct, ctype_voidPointer);
2972     }
2973   
2974   if (context_getFlag (FLG_ZEROBOOL)) {
2975     ct = ctype_makeConj (ct, ctype_bool); 
2976   }
2977
2978   return ct;
2979 }
2980
2981 ctype
2982 context_typeofOne (void)
2983 {
2984   ctype ct = ctype_int;
2985
2986   /* 1 is on longer a bool (was before 2.4)
2987      if (!context_getFlag (FLG_ABSTRACTBOOL))
2988      {
2989      ct = ctype_makeConj (ct, ctype_bool);
2990      }
2991      */
2992
2993   return (ct);
2994 }
2995
2996 /*@only@*/ cstring
2997 context_unparse (void)
2998 {
2999   cstring s;
3000
3001   switch (gc.kind)
3002     {
3003     case CX_LCL:
3004       s = message ("LCL File: %q", fileloc_unparse (g_currentloc));
3005       break;
3006     case CX_LCLLIB:
3007       s = message ("LCL Lib File: %q", fileloc_unparse (g_currentloc));
3008       break;
3009     case CX_GLOBAL:
3010       s = message ("Global Context:%q", fileloc_unparse (g_currentloc));
3011       break;
3012     case CX_INNER:
3013       s = message ("Inner Context [%d] : %q", 
3014                    gc.cont.cdepth,
3015                    fileloc_unparse (g_currentloc));
3016       break;
3017     case CX_FUNCTION:
3018       s = message ("Function %q :%q \n\taccess %q\n\tmodifies %q",
3019                    uentry_unparse (gc.cont.fcn),
3020                    fileloc_unparse (g_currentloc),
3021                    typeIdSet_unparse (gc.acct),
3022                    sRefSet_unparse (gc.mods));
3023       break;
3024     case CX_MACROFCN:
3025       s = message ("Function Macro %q", uentry_unparse (gc.cont.fcn));
3026       break;
3027     case CX_UNKNOWNMACRO:
3028       s = message ("Forward Specified Macro %q", uentry_unparse (gc.cont.fcn));
3029       break;
3030     case CX_MACROCONST:
3031       s = message ("Constant Macro %q", uentry_unparse (gc.cont.fcn));
3032       break;
3033     case CX_ITERDEF:
3034       s = message ("Iter definition %q", uentry_unparse (gc.cont.fcn));
3035       break;
3036     case CX_ITEREND:
3037       s = message ("Iter end %q", uentry_unparse (gc.cont.fcn));
3038       break;
3039     case CX_FCNDECLARATION:
3040       s = message ("Function declaration %q", uentry_unparse (gc.cont.fcn));
3041       break;
3042     default:
3043       s = message ("Un-unparseable context: %d", (int) gc.kind);
3044       break;
3045     }
3046   
3047   s = message ("%q\naccess: %q", s, context_unparseAccess ());
3048   return (s);
3049 }
3050
3051 extern ctype
3052 context_currentFunctionType (void)
3053 {
3054   if (gc.kind == CX_FUNCTION || gc.kind == CX_MACROFCN)
3055     {
3056             return (uentry_getType (gc.cont.fcn));
3057     }
3058   else if (gc.kind == CX_INNER)
3059     {
3060       llcontbuglit ("context_currentFunctionType: inner context");
3061       do { context_exitInnerPlain (); } while (gc.kind == CX_INNER);
3062       return (context_currentFunctionType ());
3063     }
3064   else
3065     {
3066       llcontbuglit ("context_currentFunctionType: not in function");
3067       return (ctype_undefined);
3068     }
3069 }
3070
3071 void
3072 context_enterInnerContext (void)
3073 {
3074   if (context_getFlag (FLG_GRAMMAR))
3075     {
3076       lldiagmsg (message ("Enter inner context: %q", context_unparse ()));
3077     }
3078
3079   if (gc.kind == CX_GLOBAL)
3080     {
3081       gc.kind = CX_INNER;
3082       gc.cont.cdepth = 1;
3083     }
3084   else if (gc.kind == CX_INNER)
3085     {
3086       gc.cont.cdepth++;
3087     }
3088   else
3089     {
3090       ;
3091     }
3092
3093   usymtab_enterScope ();
3094   pushClause (NOCLAUSE);
3095 }
3096
3097 void
3098 context_exitInnerPlain (void) /*@modifies gc;@*/
3099 {
3100   context_exitInner (exprNode_undefined);
3101 }
3102
3103 void
3104 context_exitInner (exprNode exp)
3105 {
3106    if (context_getFlag (FLG_GRAMMAR))
3107     {
3108       lldiagmsg (message ("Enter inner context: %q", context_unparse ()));
3109     }
3110  
3111   llassertprint (gc.inclause == NOCLAUSE || gc.inclause == CASECLAUSE,
3112                  ("inclause = %s", clause_nameTaken (gc.inclause)));
3113
3114   clauseStack_removeFirst (gc.clauses, NOCLAUSE);
3115   gc.inclause = topClause (gc.clauses);
3116
3117   if (gc.kind == CX_INNER)
3118     {
3119       if (--gc.cont.cdepth == 0)
3120         {
3121           gc.kind = CX_GLOBAL;
3122           gc.cont.glob = TRUE;
3123         }
3124     }
3125   else 
3126     {
3127       if (gc.kind == CX_GLOBAL)
3128         {
3129           llcontbuglit ("Attempt to exit global context");
3130           return;
3131         }
3132     }
3133
3134     usymtab_exitScope (exp);
3135 }
3136
3137
3138 void
3139 context_enterStructInnerContext (void)
3140 {
3141   if (context_getFlag (FLG_GRAMMAR))
3142     {
3143       lldiagmsg (message ("Enter struct inner context: %q", context_unparse ()));
3144     }
3145
3146   if (gc.kind == CX_GLOBAL)
3147     {
3148       gc.kind = CX_INNER;
3149       gc.cont.cdepth = 1;
3150     }
3151   else if (gc.kind == CX_INNER)
3152     {
3153       gc.cont.cdepth++;
3154     }
3155   else
3156     {
3157       ;
3158     }
3159
3160   usymtab_enterScope ();
3161
3162   if (context_getFlag (FLG_GRAMMAR))
3163     {
3164       lldiagmsg (message ("Enter struct inner context: %q", context_unparse ()));
3165     }
3166 }
3167
3168 void
3169 context_exitStructInnerContext (void)
3170 {
3171   if (context_getFlag (FLG_GRAMMAR))
3172     {
3173       lldiagmsg (message ("Exit struct inner context: %q [%d]", context_unparse (), gc.cont.cdepth));
3174     }
3175
3176   if (gc.kind == CX_INNER)
3177     {
3178       if (gc.cont.cdepth <= 0)
3179         {
3180           llcontbuglit ("Attempt to exit inner context with no depth");
3181           gc.kind = CX_GLOBAL;
3182           gc.cont.glob = TRUE;
3183           gc.cont.cdepth = 0;
3184         }
3185       else {
3186         gc.cont.cdepth--;
3187
3188         if (gc.cont.cdepth == 0)
3189           {
3190             gc.kind = CX_GLOBAL;
3191             gc.cont.glob = TRUE;
3192           }
3193       }
3194     }
3195   else 
3196     {
3197       if (gc.kind == CX_GLOBAL)
3198         {
3199           llcontbuglit ("Attempt to exit global context");
3200           return;
3201         }
3202     }
3203
3204   usymtab_exitScope (exprNode_undefined);
3205
3206   if (context_getFlag (FLG_GRAMMAR))
3207     {
3208       lldiagmsg (message ("After exit struct inner context: %q [%d]", context_unparse (), gc.cont.cdepth));
3209     }
3210 }
3211
3212 void
3213 context_exitInnerSafe (void)
3214 {
3215   if (context_getFlag (FLG_GRAMMAR))
3216     {
3217       lldiagmsg (message ("Exit inner safe: %q", context_unparse ()));
3218     }
3219
3220   if (gc.kind == CX_INNER)
3221     {
3222       if (--gc.cont.cdepth <= 0)
3223         {
3224           gc.cont.cdepth = 0;
3225         }
3226     }
3227   else if (gc.kind == CX_GLOBAL)
3228     {
3229       llcontbuglit ("Attempt to exit global context");
3230       return;
3231     }
3232   else
3233     {
3234       if (usymtab_inDeepScope ())
3235         {
3236           usymtab_exitScope (exprNode_undefined);
3237         }
3238     }
3239 }
3240
3241 static
3242 void setModuleAccess (void)
3243 {
3244   gc.facct = typeIdSet_emptySet ();
3245
3246   if (fileId_isValid (currentFile ()))
3247     {
3248       cstring baseName = fileloc_getBase (g_currentloc);
3249       
3250       if (context_getFlag (FLG_ACCESSFILE))
3251         {
3252           if (usymtab_existsType (baseName))
3253             {
3254               gc.facct = typeIdSet_insert (gc.facct, 
3255                                            usymtab_getTypeId (baseName));
3256             }
3257           else 
3258             {
3259               ;
3260             }
3261         }
3262       
3263       if (context_getFlag (FLG_ACCESSMODULE))
3264         {
3265           int i;
3266           bool hasaccess = FALSE;
3267           
3268           for (i = 0; i < gc.nmods; i++)
3269             {
3270               if (cstring_equal (baseName, gc.moduleaccess[i].file))
3271                 {
3272                   gc.facct = typeIdSet_union (gc.facct, gc.moduleaccess[i].daccess);
3273
3274                   hasaccess = TRUE;
3275                   break;
3276                 }
3277             }
3278         }
3279       
3280       gc.acct = gc.facct;
3281       gc.inheader = fileId_isHeader (currentFile ());
3282     }
3283   else
3284     {
3285       llcontbuglit ("Current file not defined\n");
3286       gc.facct = typeIdSet_emptySet ();
3287       gc.acct = gc.facct;
3288       gc.inheader = FALSE;
3289     }
3290   
3291   /* 17 Jan 1995: forgot to clear nacct */
3292   
3293   gc.nacct = typeIdSet_emptySet ();
3294 }
3295
3296 static void
3297 context_enterFileAux (void)
3298 {
3299   setModuleAccess ();
3300 }
3301
3302 void
3303 context_enterFile (void)
3304 {
3305   context_enterFileAux ();
3306   usymtab_enterFile ();
3307 }
3308
3309 void
3310 context_enterMacroFile (void)
3311 {
3312   context_enterFileAux ();
3313 }
3314
3315 bool 
3316 context_inFunction (void)
3317 {
3318   kcontext ck = gc.kind;
3319   
3320   return ((ck == CX_FUNCTION) || (ck == CX_MACROFCN) || (ck == CX_INNER));
3321 }
3322
3323 bool 
3324 context_inFunctionLike (void)
3325 {
3326   return (gc.kind == CX_FUNCTION || gc.kind == CX_MACROFCN 
3327           || gc.kind == CX_FCNDECLARATION
3328           || gc.kind == CX_UNKNOWNMACRO || gc.kind == CX_ITERDEF);
3329 }
3330
3331 bool 
3332 context_inRealFunction (void)
3333 {
3334   kcontext ck = gc.kind;
3335   
3336   return ((ck == CX_FUNCTION) || (ck == CX_MACROFCN));
3337 }
3338   
3339 void
3340 context_processAllMacros (void)
3341 {
3342   usymtab_enterFile ();
3343
3344   gc.inmacrocache = TRUE; 
3345   macrocache_processUndefinedElements (gc.mc);
3346   cleanupMessages ();  
3347   usymtab_exitFile ();
3348
3349   gc.inmacrocache = FALSE;
3350   macrocache_finalize ();
3351 }
3352
3353 /*
3354 ** this happens once at the end of each C file
3355 **
3356 ** check each Macro that was defined in current file.c or current file.h
3357 **
3358 */
3359
3360 static void
3361 context_processMacros (void)
3362 {
3363   if (fileId_isValid (currentFile ()))
3364     {
3365       fileloc lastfl;
3366       cstring cbase = fileLib_removePathFree (fileLib_removeAnyExtension (fileName (currentFile ())));
3367       
3368       gc.inmacrocache = TRUE;
3369
3370       DPRINTF (("Processing macros: %s", cbase));
3371       lastfl = macrocache_processFileElements (gc.mc, cbase);
3372       DPRINTF (("Processing macros: %s", fileloc_unparse (lastfl)));
3373
3374       cstring_free (cbase);
3375       
3376       if (fileloc_isDefined (lastfl))
3377         {
3378           g_currentloc = fileloc_update (g_currentloc, lastfl);
3379           cleanupMessages ();
3380         }
3381
3382       gc.inmacrocache = FALSE;
3383     }
3384 }
3385
3386 bool
3387 context_processingMacros (void)
3388 {
3389   return (gc.inmacrocache);
3390 }
3391
3392 void
3393 context_exitCFile (void)
3394 {
3395   if (gc.kind != CX_GLOBAL)
3396     {
3397       llfatalerrorLoc
3398         (cstring_makeLiteral ("File ended outside global scope"));
3399     }
3400
3401   if (gc.insuppressregion)
3402     {
3403      /* gack...don't reverse the order of these lines! ;-> */
3404       gc.insuppressregion = FALSE;
3405       llerrorlit (FLG_SYNTAX, 
3406                   "File ended in ignore errors region, "
3407                   "possible missing /*@end*/");
3408     }
3409
3410   /* fix up parse errors */
3411
3412   while (!usymtab_inFileScope ())
3413     {
3414       usymtab_quietExitScope (g_currentloc);
3415     }
3416
3417   /*
3418   ** Clear the file-specific modifies information.
3419   */
3420   
3421   sRefSetList_elements (gc.modrecs, mods)
3422     {
3423       sRefSet_clearStatics (mods);
3424     } end_sRefSetList_elements ;
3425   
3426   sRefSetList_clear (gc.modrecs);
3427   
3428   context_processMacros ();
3429   cleanupMessages (); 
3430   
3431   usymtab_exitFile ();
3432   
3433   gc.inDerivedFile = FALSE;
3434   filelocStack_clear (gc.locstack);
3435   
3436   gc.nacct = typeIdSet_emptySet (); /* empty noaccess */
3437   
3438   gc.cont.glob = TRUE;
3439   
3440   if (gc.savedFlags)
3441     {
3442       context_restoreFlagSettings ();
3443       gc.savedFlags = FALSE;
3444     }
3445   
3446   /*
3447     DPRINTF (("After exiting file: "));
3448     usymtab_printAll ();
3449   */
3450 }
3451
3452 void
3453 context_exitMacroCache (void)
3454 {
3455   if (gc.kind != CX_GLOBAL)
3456     {
3457       if (context_inMacro ()) 
3458         /* this is okay, file could end without newline in macro */
3459         {
3460           DPRINTF (("Still in macro: %s",
3461                     context_unparse ()));
3462           context_exitFunction ();
3463         }
3464       else
3465         {
3466           llcontbug (message ("context_exitMacroCache: outside global scope: %q", 
3467                               context_unparse ()));
3468           gc.kind = CX_GLOBAL; 
3469         }
3470     }
3471
3472   /*
3473   ** no longer valid here
3474   ** if (gc.insuppressregion)
3475   **   {
3476   **     gc.insuppressregion = FALSE;
3477   **     llerror ("File ended in ignore errors region, possible missing @");
3478   **   }
3479   */
3480
3481   gc.cont.glob = TRUE;
3482 }
3483
3484 void
3485 context_saveLocation (void)
3486 {
3487   /* was llassert (fileloc_isUndefined (gc.saveloc)) */
3488   fileloc_free (gc.saveloc);
3489   gc.saveloc = fileloc_copy (g_currentloc);
3490 }
3491
3492 fileloc
3493 context_getSaveLocation (void)
3494 {
3495   fileloc fl = gc.saveloc;
3496   gc.saveloc = fileloc_undefined;
3497   return fl;
3498 }
3499
3500 /*@observer@*/ cstring
3501 context_inFunctionName (void)
3502 {
3503   if (gc.kind == CX_FUNCTION
3504       || gc.kind == CX_MACROFCN || gc.kind == CX_UNKNOWNMACRO 
3505       || gc.kind == CX_MACROCONST 
3506       || gc.kind == CX_ITERDEF || gc.kind == CX_ITEREND)
3507     {
3508       return (uentry_rawName (gc.cont.fcn));
3509     }
3510   else
3511     {
3512       llcontbuglit ("context_inFunctionName: not in function");
3513       return (cstring_undefined);
3514     }
3515 }
3516
3517 void
3518 context_userSetFlag (flagcode f, bool b)
3519 {
3520   DPRINTF (("set flag: %s", flagcode_unparse (f)));
3521
3522   if (f == FLG_NEVERINCLUDE && b)
3523     {
3524       if (gc.flags[FLG_EXPORTHEADER])
3525         {
3526           llerror_flagWarning (cstring_makeLiteral
3527                            ("setting +neverinclude after +exportheader.  "
3528                             "Turning off exportheader, since headers are not checked "
3529                             "when +neverinclude is used."));
3530
3531           gc.flags[FLG_EXPORTHEADER] = FALSE;
3532         }
3533     }
3534   else 
3535     {
3536       if (f == FLG_EXPORTHEADER && b)
3537         {
3538           if (gc.flags[FLG_NEVERINCLUDE])
3539             {
3540               llerror_flagWarning (cstring_makeLiteral
3541                                ("setting +exportheader after +neverinclude.  "
3542                                 "Not setting exportheader, since headers are not checked "
3543                                 "when +neverinclude is used."));
3544               gc.flags[FLG_EXPORTHEADER] = FALSE;
3545               return;
3546             }
3547         }
3548     }
3549   
3550   if (context_getFlag (FLG_WARNFLAGS) && f != FLG_NOF && f != FLG_OPTF)
3551     {
3552       bool lastsetting = context_getFlag (f);
3553       
3554       if (bool_equal (lastsetting, b)
3555           && !flagcode_isSpecialFlag (f) 
3556           && !flagcode_isIdemFlag (f)
3557           && !flagcode_hasArgument (f))
3558         {
3559           llerror_flagWarning (message ("setting %s%s redundant with current value", 
3560                                     cstring_makeLiteralTemp (b ? "+" : "-"),
3561                                     flagcode_unparse (f)));
3562         }
3563     }
3564
3565   if (flagcode_isWarnUseFlag (f) && b)
3566     {
3567       if (!context_getFlag (FLG_WARNUSE))
3568         {
3569           llerror_flagWarning (message ("flag +%s is canceled by -warnuse",
3570                                     flagcode_unparse (f)));
3571           
3572         }
3573     }
3574
3575
3576   if (flagcode_isLibraryFlag (f)) 
3577     {
3578       if (gc.library != FLG_ANSILIB
3579           && gc.library != f)
3580         {
3581           llerror_flagWarning (message ("selecting library %s after library %s was "
3582                                         "selected (only one library may be used)",
3583                                         flagcode_unparse (f),
3584                                         flagcode_unparse (gc.library)));
3585         }
3586
3587       if (f == FLG_UNIXLIB)
3588         {
3589           if (context_getFlag (FLG_WARNUNIXLIB))
3590             {
3591               llerror_flagWarning (cstring_makeLiteral
3592                                    ("selecting unix library.  Unix library is "
3593                                     "ad hoc addition to POSIX library.  Recommend "
3594                                     "use +posixlib to select POSIX library instead. "
3595                                     "Use -warnunixlib to suppress this message."));
3596             }
3597         }
3598       
3599       gc.library = f;
3600     }
3601
3602   if (flagcode_isNameChecksFlag (f) && b && !context_maybeSet (FLG_NAMECHECKS))
3603     {
3604       llerror_flagWarning (message
3605                            ("setting +%s will not produce warnings with -namechecks. Must set +namechecks also.",
3606                             flagcode_unparse (f)));
3607     }
3608
3609   gc.setGlobally[f] = TRUE;
3610   context_setFlag (f, b);
3611 }
3612
3613 void
3614 context_fileSetFlag (flagcode f, ynm set)
3615 {
3616   if (!gc.savedFlags)
3617     {
3618       context_saveFlagSettings ();
3619     }
3620
3621   if (ynm_isOff (set))
3622     {
3623       context_setFlagAux (f, FALSE, TRUE, FALSE);
3624     }
3625   else if (ynm_isOn (set))
3626     {
3627       context_setFlagAux (f, TRUE, TRUE, FALSE);
3628       gc.setLocally[f] = TRUE;
3629     }
3630   else
3631     {
3632       context_restoreFlag (f);
3633     }
3634 }
3635
3636 static void
3637 context_restoreFlag (flagcode f)
3638 {
3639   
3640   if (!gc.savedFlags)
3641     {
3642       voptgenerror 
3643         (FLG_SYNTAX,
3644          message ("Attempt to restore flag %s when no file scope flags "
3645                   "have been set.",
3646                   flagcode_unparse (f)),
3647          g_currentloc);
3648     }
3649   else
3650     {
3651       context_addFlagMarker (f, MAYBE);
3652       context_setFlagAux (f, gc.saveflags[f], FALSE, TRUE);
3653     }
3654
3655   }
3656
3657 static void
3658 context_setFlag (flagcode f, bool b)
3659 {
3660   context_setFlagAux (f, b, FALSE, FALSE);
3661 }
3662
3663 void
3664 context_setFlagTemp (flagcode f, bool b)
3665 {
3666   DPRINTF (("Set flag temp: %s / %s", flagcode_unparse (f), bool_unparse (b)));
3667   gc.flags[f] = b;
3668 }
3669
3670 /*@notfunction@*/
3671 # define DOSET(ff,b) \
3672    do { if (inFile) { gc.setLocally[ff] = TRUE; \
3673                       context_addFlagMarker (ff, ynm_fromBool (b)); } \
3674         DPRINTF (("set flag: %s / %s", flagcode_unparse (ff), bool_unparse (b))); \
3675         gc.flags[ff] = b; } while (FALSE)
3676
3677 static void
3678   context_setFlagAux (flagcode f, bool b, bool 
3679                       inFile, /*@unused@*/ bool isRestore)
3680 {
3681   DPRINTF (("set flag: %s / %s", flagcode_unparse (f), bool_unparse (b)));
3682
3683   if (f == FLG_USESTDERR) 
3684     {
3685       if (b) {
3686         g_msgstream = stderr;
3687       } else {
3688         g_msgstream = stdout;
3689       }
3690     }
3691
3692   /*
3693   ** Removed test for special flags.
3694   */
3695
3696   if (flagcode_isIdemFlag (f))
3697     {
3698       DOSET (f, TRUE);
3699     }
3700   else
3701     {
3702       DOSET (f, b);
3703     }
3704
3705   if (f >= FLG_ITS4MOSTRISKY && f <= FLG_ITS4LOWRISK)
3706     {
3707       if (b) /* Turing higher level on, turns on all lower levels */
3708         {
3709           switch (f)
3710             {
3711             case FLG_ITS4MOSTRISKY:
3712               DOSET (FLG_ITS4VERYRISKY, b);
3713               /*@fallthrough@*/ 
3714             case FLG_ITS4VERYRISKY:
3715               DOSET (FLG_ITS4RISKY, b);
3716               /*@fallthrough@*/ 
3717             case FLG_ITS4RISKY:
3718               DOSET (FLG_ITS4MODERATERISK, b);
3719               /*@fallthrough@*/ 
3720             case FLG_ITS4MODERATERISK:
3721               DOSET (FLG_ITS4LOWRISK, b);
3722               /*@fallthrough@*/ 
3723             case FLG_ITS4LOWRISK:
3724               break;
3725               BADDEFAULT;
3726             }
3727         }
3728       else /* Turning level off, turns off all higher levels */
3729         {
3730           switch (f)
3731             {
3732             case FLG_ITS4LOWRISK:
3733               DOSET (FLG_ITS4MODERATERISK, b);
3734               /*@fallthrough@*/ 
3735             case FLG_ITS4MODERATERISK:
3736               DOSET (FLG_ITS4RISKY, b);
3737               /*@fallthrough@*/ 
3738             case FLG_ITS4RISKY:
3739               DOSET (FLG_ITS4VERYRISKY, b);
3740               /*@fallthrough@*/ 
3741             case FLG_ITS4VERYRISKY:
3742               DOSET (FLG_ITS4MOSTRISKY, b);
3743               /*@fallthrough@*/ 
3744             case FLG_ITS4MOSTRISKY:
3745               break;
3746               BADDEFAULT;
3747             }
3748         }
3749     }
3750   
3751   switch (f)
3752     {     
3753     case FLG_ALLEMPTY:
3754       DOSET (FLG_ALLEMPTY, b);
3755       DOSET (FLG_IFEMPTY, b);
3756       DOSET (FLG_WHILEEMPTY, b);
3757       DOSET (FLG_FOREMPTY, b);
3758       break;
3759     case FLG_PREDBOOL:
3760       DOSET (FLG_PREDBOOL, b);
3761       DOSET (FLG_PREDBOOLINT, b);
3762       DOSET (FLG_PREDBOOLPTR, b);
3763       DOSET (FLG_PREDBOOLOTHERS, b);
3764       break;
3765     case FLG_GLOBALIAS:
3766       DOSET (FLG_CHECKSTRICTGLOBALIAS, b);
3767       DOSET (FLG_CHECKEDGLOBALIAS, b);
3768       DOSET (FLG_CHECKMODGLOBALIAS, b);
3769       DOSET (FLG_UNCHECKEDGLOBALIAS, b);
3770       break;
3771     case FLG_ALLBLOCK:
3772       DOSET (FLG_ALLBLOCK, b);
3773       DOSET (FLG_IFBLOCK, b);
3774       DOSET (FLG_WHILEBLOCK, b);
3775       DOSET (FLG_FORBLOCK, b);
3776       break;
3777     case FLG_GRAMMAR:
3778       if (b)
3779         {
3780           yydebug = 1;
3781           mtdebug = 1;
3782         }
3783       else
3784         {
3785           yydebug = 0;
3786           mtdebug = 0;
3787         }
3788       
3789       DOSET (FLG_GRAMMAR, b);
3790       break;
3791     case FLG_CODEIMPONLY:
3792       DOSET (FLG_CODEIMPONLY, b);
3793       DOSET (FLG_GLOBIMPONLY, b);
3794       DOSET (FLG_RETIMPONLY, b);
3795       DOSET (FLG_STRUCTIMPONLY, b);
3796       break;
3797     case FLG_SPECALLIMPONLY:
3798       DOSET (FLG_SPECALLIMPONLY, b);
3799       DOSET (FLG_SPECGLOBIMPONLY, b);
3800       DOSET (FLG_SPECRETIMPONLY, b);
3801       DOSET (FLG_SPECSTRUCTIMPONLY, b);
3802       break;
3803     case FLG_ALLIMPONLY:
3804       DOSET (FLG_ALLIMPONLY, b);
3805       DOSET (FLG_GLOBIMPONLY, b);
3806       DOSET (FLG_RETIMPONLY, b);
3807       DOSET (FLG_STRUCTIMPONLY, b);
3808       DOSET (FLG_SPECGLOBIMPONLY, b);
3809       DOSET (FLG_SPECRETIMPONLY, b);
3810       DOSET (FLG_SPECSTRUCTIMPONLY, b);
3811       break;
3812     case FLG_ANSILIMITS: 
3813       DOSET (FLG_ANSILIMITS, b);
3814       DOSET (FLG_CONTROLNESTDEPTH, b);
3815       DOSET (FLG_STRINGLITERALLEN, b);
3816       DOSET (FLG_INCLUDENEST, b);
3817       DOSET (FLG_NUMSTRUCTFIELDS, b);
3818       DOSET (FLG_NUMENUMMEMBERS, b);
3819       
3820       if (b)
3821         {
3822           context_setValue (FLG_CONTROLNESTDEPTH, DEFAULT_CONTROLNESTDEPTH);
3823           context_setValue (FLG_STRINGLITERALLEN, DEFAULT_STRINGLITERALLEN);
3824           context_setValue (FLG_INCLUDENEST, DEFAULT_INCLUDENEST);
3825           context_setValue (FLG_NUMSTRUCTFIELDS, DEFAULT_NUMSTRUCTFIELDS);
3826           context_setValue (FLG_NUMENUMMEMBERS, DEFAULT_NUMENUMMEMBERS);
3827         }
3828       break;
3829     case FLG_EXTERNALNAMELEN:
3830       DOSET (FLG_DISTINCTEXTERNALNAMES, TRUE);
3831       DOSET (FLG_EXTERNALNAMELEN, TRUE);
3832       break;
3833     case FLG_INTERNALNAMELEN:
3834       DOSET (FLG_DISTINCTINTERNALNAMES, TRUE);
3835       DOSET (FLG_INTERNALNAMELEN, TRUE);
3836       break;
3837     case FLG_EXTERNALNAMECASEINSENSITIVE:
3838       DOSET (FLG_EXTERNALNAMECASEINSENSITIVE, b);
3839       
3840       if (b && !gc.flags[FLG_DISTINCTEXTERNALNAMES])
3841         {
3842           DOSET (FLG_DISTINCTEXTERNALNAMES, TRUE);
3843           context_setValue (FLG_EXTERNALNAMELEN, 0);
3844         }
3845       break;
3846     case FLG_INTERNALNAMECASEINSENSITIVE:
3847       DOSET (FLG_INTERNALNAMECASEINSENSITIVE, b);
3848       
3849       if (b && !gc.flags[FLG_DISTINCTINTERNALNAMES])
3850         {
3851           DOSET (FLG_DISTINCTINTERNALNAMES, TRUE);
3852           context_setValue (FLG_INTERNALNAMELEN, 0);
3853         }
3854       break;
3855     case FLG_INTERNALNAMELOOKALIKE:
3856       DOSET (FLG_INTERNALNAMELOOKALIKE, b);
3857       
3858       if (b && !gc.flags[FLG_DISTINCTINTERNALNAMES])
3859         {
3860           DOSET (FLG_DISTINCTINTERNALNAMES, TRUE);
3861           context_setValue (FLG_INTERNALNAMELEN, 0);
3862         }
3863       break;
3864     case FLG_MODUNSPEC:
3865       DOSET (FLG_MODNOMODS, b);
3866       DOSET (FLG_MODGLOBSUNSPEC, b);
3867       DOSET (FLG_MODSTRICTGLOBSUNSPEC, b);
3868       break;
3869     case FLG_EXPORTANY: 
3870       DOSET (FLG_EXPORTVAR, b);
3871       DOSET (FLG_EXPORTFCN, b);
3872       DOSET (FLG_EXPORTTYPE, b);
3873       DOSET (FLG_EXPORTMACRO, b);
3874       DOSET (FLG_EXPORTCONST, b);
3875       gc.anyExports = TRUE;
3876       break;
3877     case FLG_REPEXPOSE:
3878       DOSET (FLG_RETEXPOSE, b); 
3879       DOSET (FLG_ASSIGNEXPOSE, b); 
3880       DOSET (FLG_CASTEXPOSE, b); 
3881       break;
3882     case FLG_RETVAL:
3883       DOSET (FLG_RETVALBOOL, b);
3884       DOSET (FLG_RETVALINT, b);
3885       DOSET (FLG_RETVALOTHER, b);
3886       break;
3887     case FLG_PARTIAL:
3888       if (b)
3889         {
3890           DOSET (FLG_EXPORTLOCAL, FALSE);
3891           DOSET (FLG_DECLUNDEF, FALSE);
3892           DOSET (FLG_SPECUNDEF, FALSE);
3893           DOSET (FLG_TOPUNUSED, FALSE);
3894         }
3895       break;
3896     case FLG_DEEPBREAK:
3897       DOSET (FLG_LOOPLOOPBREAK, b);
3898       DOSET (FLG_LOOPSWITCHBREAK, b);
3899       DOSET (FLG_SWITCHLOOPBREAK, b);
3900       DOSET (FLG_SWITCHSWITCHBREAK, b);
3901       DOSET (FLG_LOOPLOOPCONTINUE, b);
3902       DOSET (FLG_DEEPBREAK, b);
3903       break;
3904     case FLG_ACCESSALL:
3905       DOSET (FLG_ACCESSMODULE, b);
3906       DOSET (FLG_ACCESSFILE, b);
3907       DOSET (FLG_ACCESSCZECH, b);
3908       break;
3909     case FLG_ALLMACROS:
3910       DOSET (FLG_ALLMACROS, b);
3911       DOSET (FLG_FCNMACROS, b);
3912       DOSET (FLG_CONSTMACROS, b);
3913       break;
3914     case FLG_CZECH:
3915       if (b) { DOSET (FLG_ACCESSCZECH, b); }
3916       DOSET (FLG_CZECHFUNCTIONS, b);
3917       DOSET (FLG_CZECHVARS, b);
3918       DOSET (FLG_CZECHCONSTANTS, b);
3919       DOSET (FLG_CZECHTYPES, b);
3920       break;
3921     case FLG_SLOVAK:
3922       if (b) { DOSET (FLG_ACCESSSLOVAK, b); }
3923       DOSET (FLG_SLOVAKFUNCTIONS, b);
3924       DOSET (FLG_SLOVAKVARS, b);
3925       DOSET (FLG_SLOVAKCONSTANTS, b);
3926       DOSET (FLG_SLOVAKTYPES, b);
3927       break;
3928     case FLG_CZECHOSLOVAK:
3929       if (b) { DOSET (FLG_ACCESSCZECHOSLOVAK, b); }
3930       DOSET (FLG_CZECHOSLOVAKFUNCTIONS, b);
3931       DOSET (FLG_CZECHOSLOVAKVARS, b);
3932       DOSET (FLG_CZECHOSLOVAKCONSTANTS, b);
3933       DOSET (FLG_CZECHOSLOVAKTYPES, b);
3934       break;
3935     case FLG_NULL:
3936       DOSET (FLG_NULLSTATE, b);
3937       DOSET (FLG_NULLDEREF, b);
3938       DOSET (FLG_NULLASSIGN, b);
3939       DOSET (FLG_NULLPASS, b);
3940       DOSET (FLG_NULLRET, b);
3941       break;
3942     case FLG_MEMCHECKS:
3943       DOSET (FLG_NULLSTATE, b);
3944       DOSET (FLG_NULLDEREF, b);
3945       DOSET (FLG_NULLASSIGN, b);
3946       DOSET (FLG_NULLPASS, b);
3947       DOSET (FLG_NULLRET, b);
3948       DOSET (FLG_COMPDEF, b);
3949       DOSET (FLG_COMPMEMPASS, b);
3950       DOSET (FLG_UNIONDEF, b);
3951       DOSET (FLG_MEMTRANS, b);
3952       DOSET (FLG_USERELEASED, b);
3953       DOSET (FLG_ALIASUNIQUE, b);
3954       DOSET (FLG_MAYALIASUNIQUE, b);
3955       DOSET (FLG_MUSTFREE, b);
3956       DOSET (FLG_MUSTDEFINE, b);
3957       DOSET (FLG_GLOBSTATE, b); 
3958       DOSET (FLG_COMPDESTROY, b);
3959       DOSET (FLG_MUSTNOTALIAS, b);
3960       DOSET (FLG_MEMIMPLICIT, b);
3961       DOSET (FLG_BRANCHSTATE, b); 
3962       /*@fallthrough@*/ /* also sets memtrans flags */
3963     case FLG_MEMTRANS:
3964       DOSET (FLG_MEMTRANS, b);
3965       DOSET (FLG_EXPOSETRANS, b);
3966       DOSET (FLG_OBSERVERTRANS, b);
3967       DOSET (FLG_DEPENDENTTRANS, b);
3968       DOSET (FLG_NEWREFTRANS, b);
3969       DOSET (FLG_ONLYTRANS, b);
3970       DOSET (FLG_OWNEDTRANS, b);
3971       DOSET (FLG_FRESHTRANS, b);
3972       DOSET (FLG_SHAREDTRANS, b);
3973       DOSET (FLG_TEMPTRANS, b);
3974       DOSET (FLG_KEPTTRANS, b);
3975       DOSET (FLG_REFCOUNTTRANS, b);
3976       DOSET (FLG_STATICTRANS, b);
3977       DOSET (FLG_UNKNOWNTRANS, b);
3978       DOSET (FLG_KEEPTRANS, b);
3979       DOSET (FLG_IMMEDIATETRANS, b);
3980       break;
3981       
3982     default:
3983       break;
3984     }
3985
3986   if (b && !gc.anyExports
3987       && (f == FLG_EXPORTVAR || f == FLG_EXPORTFCN
3988           || f == FLG_EXPORTTYPE || f == FLG_EXPORTMACRO
3989           || f == FLG_EXPORTCONST
3990           || f == FLG_EXPORTANY))
3991     {
3992       gc.anyExports = TRUE;
3993     }
3994 }
3995
3996 bool 
3997 context_maybeSet (flagcode d)
3998 {
3999   return (gc.flags[d] || gc.setLocally[d]);
4000 }
4001
4002 bool
4003 context_getFlag (flagcode d)
4004 {
4005   return (gc.flags[d]);
4006 }
4007
4008 bool
4009 context_flagOn (flagcode f, fileloc loc)
4010 {
4011   return (!context_suppressFlagMsg (f, loc));
4012 }
4013
4014 static void context_saveFlagSettings (void)
4015 {
4016   gc.savedFlags = TRUE;
4017   llassert (sizeof (gc.saveflags) == sizeof (gc.flags));
4018   memcpy (gc.saveflags, gc.flags, sizeof (gc.flags));
4019 }
4020
4021 static void context_restoreFlagSettings (void)
4022 {
4023   llassert (sizeof (gc.saveflags) == sizeof (gc.flags));
4024   memcpy (gc.flags, gc.saveflags, sizeof (gc.flags));
4025   gc.savedFlags = FALSE;
4026 }
4027
4028 void context_setFilename (fileId fid, int lineno) 
4029    /*@globals fileloc g_currentloc;@*/
4030    /*@modifies g_currentloc@*/
4031 {
4032   if (fileId_baseEqual (currentFile (), fid))
4033     {
4034       setLine (lineno);
4035       return;
4036     }
4037   else
4038     {
4039       fileloc_setColumn (g_currentloc, 0);
4040
4041       if (fileloc_isSpecialFile (g_currentloc))
4042         {
4043           gc.inDerivedFile = TRUE;
4044         }
4045
4046       if (filelocStack_popPushFile (gc.locstack, g_currentloc))
4047         {
4048           int maxdepth = context_getValue (FLG_INCLUDENEST);
4049
4050           if (filelocStack_size (gc.locstack) > maxdepth)
4051             {
4052               int depth = filelocStack_includeDepth (gc.locstack);
4053               
4054               if (depth > maxdepth)
4055                 {
4056                   if (optgenerror 
4057                       (FLG_INCLUDENEST,
4058                        message ("Maximum include nesting depth "
4059                                 "(%d, current depth %d) exceeded",
4060                                 maxdepth,
4061                                 depth),
4062                        filelocStack_nextTop (gc.locstack)))
4063                     {
4064                       filelocStack_printIncludes (gc.locstack);
4065                     }
4066                 }
4067             }
4068         }
4069       
4070       g_currentloc = fileloc_create (fid, lineno, 1);
4071       gc.inheader = fileId_isHeader (currentFile ());
4072
4073       context_enterFileAux ();
4074     }
4075 }
4076
4077 void context_enterIterDef (/*@observer@*/ uentry le)
4078 {
4079   context_enterMacro (le);
4080   gc.acct = typeIdSet_subtract (gc.facct, gc.nacct);
4081   gc.kind = CX_ITERDEF;
4082 }
4083
4084 void context_enterIterEnd (/*@observer@*/ uentry le)
4085 {
4086   context_enterMacro (le);
4087   gc.kind = CX_ITEREND;
4088 }
4089
4090 void 
4091 context_destroyMod (void) 
4092    /*@globals killed gc@*/
4093 {
4094   setCodePoint ();
4095   ctype_destroyMod ();
4096   setCodePoint ();
4097   usymtab_free ();
4098   setCodePoint ();
4099   fileTable_free (gc.ftab);
4100   filelocStack_free (gc.locstack);
4101   setCodePoint ();
4102   gc.ftab = fileTable_undefined;
4103
4104   macrocache_free (gc.mc);
4105   sfree (gc.moduleaccess);
4106   setCodePoint ();
4107
4108   fileloc_free (gc.saveloc); gc.saveloc = fileloc_undefined;
4109   fileloc_free (gc.pushloc); gc.pushloc = fileloc_undefined;
4110
4111   setCodePoint ();
4112   sRefSetList_free (gc.modrecs);
4113   setCodePoint ();
4114   flagMarkerList_free (gc.markers); 
4115   setCodePoint ();
4116   messageLog_free (gc.msgLog);
4117   setCodePoint ();
4118   clauseStack_free (gc.clauses);
4119   setCodePoint ();
4120   
4121   cstring_free (gc.msgAnnote);
4122   globSet_free (gc.globs_used);
4123   metaStateTable_free (gc.stateTable);
4124   annotationTable_free (gc.annotTable);
4125 }
4126
4127 /*
4128 ** Flag shortcuts.
4129 */
4130
4131 bool context_msgBoolInt (void)
4132 {
4133   return gc.flags [FLG_BOOLINT];
4134 }
4135
4136 bool context_msgCharInt (void)
4137 {
4138   return gc.flags [FLG_CHARINT];
4139 }
4140
4141 bool context_msgEnumInt (void)
4142 {
4143   return gc.flags [FLG_ENUMINT];
4144 }
4145
4146 bool context_msgPointerArith (void) 
4147 {
4148   return gc.flags [FLG_POINTERARITH];
4149 }
4150
4151 bool context_msgStrictOps (void) 
4152 {
4153   return gc.flags [FLG_STRICTOPS];
4154 }
4155
4156 # ifndef NOLCL
4157 bool context_msgLh (void)           
4158 {
4159   return gc.flags [FLG_DOLH];
4160 }
4161 # endif
4162
4163 void context_pushLoc (void) 
4164 {
4165   fileloc_free (gc.pushloc);
4166   gc.pushloc = gc.saveloc;
4167   gc.saveloc = fileloc_undefined;
4168 }
4169
4170 void context_popLoc (void) 
4171 {
4172   gc.saveloc = fileloc_update (gc.saveloc, gc.pushloc);
4173 }
4174
4175 bool context_inGlobalScope (void)
4176 {
4177   return (usymtab_inFileScope() || usymtab_inGlobalScope ());
4178 }
4179
4180 bool context_inInnerScope (void)
4181 {
4182   return (gc.kind == CX_INNER);
4183 }
4184
4185 void context_setProtectVars (void)
4186 {
4187   gc.protectVars = TRUE;
4188 }
4189
4190 bool context_anyErrors (void)
4191 {
4192   return (gc.numerrors > 0);
4193 }
4194
4195 void context_hasError (void)
4196 {
4197   gc.numerrors++;
4198   DPRINTF (("num errors: %d", gc.numerrors));
4199 }
4200
4201 int context_numErrors (void)
4202 {
4203   return gc.numerrors;
4204 }
4205
4206 bool context_neednl (void)
4207 {
4208   return gc.neednl;
4209 }
4210
4211 void context_setNeednl (void)
4212 {
4213   gc.neednl = TRUE;
4214 }
4215
4216 int context_getExpect (void)
4217 {
4218   return (context_getValue (FLG_EXPECT));
4219 }
4220
4221 # ifndef NOLCL
4222 int context_getLCLExpect (void)
4223 {
4224   return (context_getValue (FLG_LCLEXPECT));
4225 }
4226 # endif
4227
4228 int context_getLimit (void)
4229 {
4230   return (context_getValue (FLG_LIMIT));
4231 }
4232
4233 bool context_unlimitedMessages (void)
4234 {
4235   return (context_getLimit () < 0);
4236 }
4237
4238 void context_releaseVars (void)
4239 {
4240   llassert (gc.protectVars);
4241   gc.protectVars = FALSE;
4242 }
4243
4244 void context_sizeofReleaseVars (void)
4245 {
4246   /* If there is a nested sizeof, this might not hold:
4247      llassert (gc.protectVars);
4248      */
4249
4250   gc.protectVars = FALSE;
4251 }
4252
4253 bool context_inProtectVars (void)
4254 {
4255   return (gc.protectVars);
4256 }
4257
4258 void context_hideShowscan (void) 
4259 {
4260   gc.flags[FLG_SHOWSCAN] = FALSE;
4261 }
4262
4263 void context_unhideShowscan (void)
4264 {
4265   gc.flags[FLG_SHOWSCAN] = TRUE;
4266 }
4267
4268 bool context_inHeader (void)
4269 {
4270   return (gc.inheader);
4271 }
4272
4273 fileTable context_fileTable (void)
4274 {
4275   return gc.ftab;
4276 }
4277
4278 cstring context_tmpdir (void)
4279 {
4280   return (context_getString (FLG_TMPDIR));
4281 }
4282
4283 messageLog context_messageLog (void)
4284 {
4285   return gc.msgLog;
4286 }
4287
4288 bool context_inMacroFunction (void)
4289 {
4290   return (gc.kind == CX_MACROFCN);
4291 }
4292
4293 bool context_inMacroConstant (void)
4294 {   
4295   return (gc.kind == CX_MACROCONST);
4296 }
4297
4298 bool context_inUnknownMacro (void)
4299 {   
4300   return (gc.kind == CX_UNKNOWNMACRO);
4301 }
4302
4303 void context_setShownFunction (void)
4304 {
4305   gc.showfunction = FALSE;
4306 }
4307
4308 bool context_doDump (void)
4309 {   
4310   return cstring_isNonEmpty (context_getString (FLG_DUMP));
4311 }
4312
4313 bool context_doMerge (void)
4314 {
4315   return cstring_isNonEmpty (context_getString (FLG_MERGE));
4316 }
4317
4318 cstring context_getDump (void)
4319 {           
4320   return context_getString (FLG_DUMP);
4321 }
4322
4323 cstring context_getMerge (void)
4324 {
4325   return context_getString (FLG_MERGE);
4326 }
4327
4328 # ifndef NOLCL
4329 bool context_inLCLLib (void)
4330 {   
4331   return (gc.kind == CX_LCLLIB);
4332 }
4333
4334 bool context_inImport (void)
4335 {
4336   return (gc.inimport);
4337 }
4338
4339 void context_enterImport (void)
4340
4341   gc.inimport = TRUE;
4342 }
4343
4344 void context_leaveImport (void)
4345
4346   gc.inimport = FALSE;
4347 }
4348 # endif
4349
4350 bool context_inMacro (void) 
4351 {
4352   return (gc.kind == CX_MACROFCN || gc.kind == CX_MACROCONST 
4353           || gc.kind == CX_UNKNOWNMACRO
4354           || gc.kind == CX_ITERDEF || gc.kind == CX_ITEREND);
4355 }
4356
4357 bool context_inIterDef (void)
4358 {
4359   return (gc.kind == CX_ITERDEF);
4360 }
4361
4362 bool context_inIterEnd (void)
4363 {
4364   return (gc.kind == CX_ITEREND);
4365 }
4366
4367 int context_getLinesProcessed (void)    
4368 {
4369   return (gc.linesprocessed);
4370 }
4371
4372 int context_getSpecLinesProcessed (void)    
4373 {
4374   return (gc.speclinesprocessed);
4375 }
4376
4377 # ifndef NOLCL
4378 void context_processedSpecLine (void)
4379 {
4380   gc.speclinesprocessed++;
4381 }
4382
4383 void context_resetSpecLines (void)    
4384 {
4385   gc.speclinesprocessed = 0;
4386 }
4387 # endif
4388
4389 bool context_inGlobalContext (void)
4390 {
4391   return (gc.kind == CX_GLOBAL);
4392 }
4393
4394 static void context_quietExitScopes (void)
4395 {
4396   /*
4397   ** Try to restore the global scope (after an error).
4398   */
4399
4400   while (!usymtab_inFileScope ())
4401     {
4402       usymtab_quietExitScope (g_currentloc);
4403     }
4404
4405   gc.cont.glob = TRUE;
4406   gc.kind = CX_GLOBAL;
4407 }
4408
4409 void context_checkGlobalScope (void)
4410 {
4411   if (gc.kind != CX_GLOBAL)
4412     {
4413       llcontbug (message ("Not in global scope as expected: %q", context_unparse ()));
4414       context_quietExitScopes ();
4415     }
4416 }
4417
4418 void context_setFileId (fileId s)
4419 {
4420   g_currentloc = fileloc_updateFileId (g_currentloc, s); 
4421 }
4422
4423 bool context_setBoolName (void)
4424 {
4425   return (!cstring_equalLit (context_getString (FLG_BOOLTYPE),
4426                              DEFAULT_BOOLTYPE));
4427 }
4428
4429 cstring context_printBoolName (void)
4430 {
4431   if (context_setBoolName ()) 
4432     {
4433       return context_getBoolName ();
4434     }
4435   else
4436     {
4437       return cstring_makeLiteralTemp ("boolean");
4438     }
4439 }
4440
4441 cstring context_getBoolName (void)
4442 {
4443   return (context_getString (FLG_BOOLTYPE));
4444 }
4445
4446 cstring context_getFalseName (void)
4447 {
4448   return (context_getString (FLG_BOOLFALSE));
4449 }
4450
4451 cstring context_getTrueName (void)
4452 {
4453   return (context_getString (FLG_BOOLTRUE));
4454 }
4455
4456 cstring context_getLarchPath (void)
4457 {
4458   return (context_getString (FLG_LARCHPATH));
4459 }
4460
4461 cstring context_getLCLImportDir (void)
4462 {
4463   return (context_getString (FLG_LCLIMPORTDIR));
4464 }
4465
4466 static void context_setJustPopped (void)
4467 {
4468   gc.justpopped = TRUE;
4469 }
4470
4471 void context_clearJustPopped (void)
4472 {
4473   gc.justpopped = FALSE;
4474 }
4475
4476 bool context_justPopped (void)
4477 {
4478   return (gc.justpopped);
4479 }
4480
4481 void context_setMacroMissingParams (void)
4482 {
4483   gc.macroMissingParams = TRUE;
4484 }
4485
4486 void context_resetMacroMissingParams (void)
4487 {
4488   gc.macroMissingParams = FALSE;
4489 }
4490
4491 bool context_isMacroMissingParams (void)
4492 {
4493   return (gc.macroMissingParams);
4494 }
4495
4496 void context_showFilelocStack (void) 
4497 {
4498   filelocStack_printIncludes (gc.locstack);
4499 }
4500
4501 metaStateTable context_getMetaStateTable (void) 
4502 {
4503   return gc.stateTable;
4504 }
4505
4506 metaStateInfo context_lookupMetaStateInfo (cstring key)
4507 {
4508   return metaStateTable_lookup (gc.stateTable, key);
4509 }
4510
4511 /*@null@*/ annotationInfo context_lookupAnnotation (cstring annot) 
4512 {
4513   annotationInfo ainfo;
4514
4515   ainfo = annotationTable_lookup (gc.annotTable, annot);
4516
4517   return ainfo;
4518 }
4519
4520 void context_addAnnotation (annotationInfo ainfo)
4521 {
4522   if (annotationTable_contains (gc.annotTable, annotationInfo_getName (ainfo)))
4523     {
4524       voptgenerror 
4525         (FLG_SYNTAX,
4526          message ("Duplicate annotation declaration: %s", annotationInfo_getName (ainfo)),
4527          annotationInfo_getLoc (ainfo));
4528
4529       annotationInfo_free (ainfo);
4530     }
4531   else
4532     {
4533       annotationTable_insert (gc.annotTable, ainfo);
4534     }
4535 }
4536
4537 void context_addMetaState (cstring mname, metaStateInfo msinfo)
4538 {
4539   if (metaStateTable_contains (gc.stateTable, mname))
4540     {
4541       voptgenerror 
4542         (FLG_SYNTAX,
4543          message ("Duplicate metastate declaration: %s", mname),
4544          metaStateInfo_getLoc (msinfo));
4545       cstring_free (mname);
4546       metaStateInfo_free (msinfo);
4547     }
4548   else
4549     {
4550       DPRINTF (("Adding meta state: %s", mname));
4551       metaStateTable_insert (gc.stateTable, mname, msinfo); 
4552     }
4553 }
4554
4555 valueTable context_createValueTable (sRef s, stateInfo sinfo)
4556 {
4557   if (metaStateTable_size (gc.stateTable) > 0)
4558     {
4559       valueTable res = valueTable_create (metaStateTable_size (gc.stateTable));
4560       /*@i32 should use smaller value... */
4561       DPRINTF (("Value table for: %s", sRef_unparse (s)));
4562       
4563       metaStateTable_elements (gc.stateTable, msname, msi)
4564         {
4565           mtContextNode context = metaStateInfo_getContext (msi);
4566
4567           if (mtContextNode_matchesRefStrict (context, s))
4568             {
4569               DPRINTF (("Create: %s", metaStateInfo_unparse (msi)));
4570               llassert (cstring_equal (msname, metaStateInfo_getName (msi)));
4571               
4572               valueTable_insert 
4573                 (res,
4574                  cstring_copy (metaStateInfo_getName (msi)),
4575                  stateValue_createImplicit (metaStateInfo_getDefaultValue (msi, s), 
4576                                             stateInfo_copy (sinfo)));
4577             }
4578           else
4579             {
4580               DPRINTF (("No match: %s", metaStateInfo_unparse (msi)));
4581             }
4582         } 
4583       end_metaStateTable_elements ;
4584       
4585       stateInfo_free (sinfo);
4586       DPRINTF (("Value table: %s", valueTable_unparse (res)));
4587       return res;
4588     }
4589   else
4590     {
4591       stateInfo_free (sinfo);
4592       return valueTable_undefined;
4593     }
4594 }
4595
4596 valueTable context_createGlobalMarkerValueTable (stateInfo sinfo)
4597 {
4598   if (metaStateTable_size (gc.stateTable) > 0)
4599     {
4600       valueTable res = valueTable_create (metaStateTable_size (gc.stateTable));
4601       /*@i32 should use smaller value... */
4602       
4603       metaStateTable_elements (gc.stateTable, msname, msi)
4604         {
4605           /*@i23 only add global...*/
4606           DPRINTF (("Create: %s", metaStateInfo_unparse (msi)));
4607           llassert (cstring_equal (msname, metaStateInfo_getName (msi)));
4608           
4609           valueTable_insert (res,
4610                              cstring_copy (metaStateInfo_getName (msi)),
4611                              stateValue_create (metaStateInfo_getDefaultGlobalValue (msi),
4612                                                 stateInfo_copy (sinfo)));
4613         } 
4614       end_metaStateTable_elements ;
4615       
4616       stateInfo_free (sinfo);
4617       DPRINTF (("Value table: %s", valueTable_unparse (res)));
4618       return res;
4619     }
4620   else
4621     {
4622       stateInfo_free (sinfo);
4623       return valueTable_undefined;
4624     }
4625 }
4626
4627
4628
This page took 0.462651 seconds and 5 git commands to generate.