]> 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_UNSIGNEDCOMPARE,
982           FLG_PARAMUNUSED, FLG_VARUNUSED, FLG_FUNCUNUSED, 
983           FLG_TYPEUNUSED,
984           FLG_CONSTUNUSED, FLG_ENUMMEMUNUSED, FLG_FIELDUNUSED,
985           FLG_PTRNUMCOMPARE, FLG_BOOLCOMPARE, FLG_UNSIGNEDCOMPARE,
986           FLG_MUTREP, FLG_NOEFFECT, FLG_IMPTYPE,
987           FLG_RETVALOTHER, FLG_RETVALBOOL, FLG_RETVALINT,
988           FLG_SPECUNDEF, FLG_INCONDEFS, FLG_INCONDEFSLIB, FLG_MISPLACEDSHAREQUAL,
989           FLG_MATCHFIELDS,
990           FLG_FORMATCONST,
991           FLG_MACROPARAMS, FLG_MACROASSIGN, FLG_SEFPARAMS, 
992           FLG_MACROSTMT, FLG_MACROPARENS, 
993           FLG_MACROFCNDECL,
994           FLG_MACROCONSTDECL,
995           FLG_MACROREDEF, FLG_INFLOOPS, FLG_UNREACHABLE, 
996           FLG_NORETURN, FLG_CASEBREAK, FLG_MISSCASE, FLG_USEDEF, 
997           FLG_FIRSTCASE,
998           FLG_NESTEDEXTERN, 
999           FLG_NUMLITERAL,
1000           FLG_ZEROBOOL,
1001           /* memchecks flags */
1002           FLG_NULLDEREF, 
1003           FLG_NULLSTATE, FLG_NULLASSIGN,
1004           FLG_NULLPASS, FLG_NULLRET,        
1005
1006           FLG_COMPDEF, FLG_COMPMEMPASS, FLG_UNIONDEF,
1007           FLG_RETSTACK,
1008
1009           /* memtrans flags */
1010           FLG_EXPOSETRANS,
1011           FLG_OBSERVERTRANS,
1012           FLG_DEPENDENTTRANS,
1013           FLG_NEWREFTRANS,
1014           FLG_ONLYTRANS,
1015           FLG_OWNEDTRANS,
1016           FLG_FRESHTRANS,
1017           FLG_SHAREDTRANS,
1018           FLG_TEMPTRANS,
1019           FLG_KEPTTRANS,
1020           FLG_REFCOUNTTRANS,
1021           FLG_STATICTRANS,
1022           FLG_UNKNOWNTRANS,
1023           FLG_KEEPTRANS,
1024           FLG_IMMEDIATETRANS,
1025
1026           FLG_EXPORTLOCAL,
1027
1028           FLG_USERELEASED, FLG_ALIASUNIQUE, FLG_MAYALIASUNIQUE,
1029           FLG_MUSTFREE, FLG_MUSTDEFINE, FLG_GLOBSTATE, 
1030           FLG_COMPDESTROY, FLG_MUSTNOTALIAS,
1031           FLG_MEMIMPLICIT,
1032           FLG_BRANCHSTATE, 
1033           FLG_STATETRANSFER, FLG_STATEMERGE,
1034           FLG_EVALORDER, FLG_SHADOW, FLG_READONLYSTRINGS,
1035           FLG_EXITARG,
1036           FLG_IMPCHECKEDSPECGLOBALS,
1037           FLG_MODGLOBS, FLG_WARNLINTCOMMENTS,
1038           FLG_IFEMPTY, FLG_REALCOMPARE,
1039           FLG_BOOLOPS, FLG_PTRNEGATE,
1040           FLG_SHIFTSIGNED,
1041           FLG_BUFFEROVERFLOWHIGH,
1042           FLG_BUFFEROVERFLOW,
1043           INVALID_FLAG 
1044         } ;
1045
1046       SETFLAGS ();
1047     }
1048   else if (cstring_equalLit (s, "weak"))
1049     {
1050       flagcode modeflags[] = 
1051         { 
1052           FLG_BOOLINT, FLG_CHARINT, FLG_FLOATDOUBLE,
1053           FLG_ENUMINT, FLG_RELAXQUALS, FLG_FORWARDDECL, 
1054           FLG_CHARINDEX, FLG_ABSTVOIDP, FLG_USEALLGLOBS, 
1055           FLG_CHARUNSIGNEDCHAR,
1056           FLG_PREDBOOLOTHERS, 
1057           FLG_VARUNUSED, FLG_FUNCUNUSED, 
1058           FLG_TYPEUNUSED,
1059           FLG_CHECKSTRICTGLOBALS, FLG_MACROMATCHNAME,
1060           FLG_RETVALOTHER,
1061           FLG_IFEMPTY, 
1062           FLG_BUFFEROVERFLOWHIGH,
1063           FLG_RETSTACK, FLG_PTRNEGATE,
1064           FLG_STATETRANSFER, FLG_STATEMERGE,
1065           FLG_LONGUNSIGNEDINTEGRAL,
1066           FLG_LONGUNSIGNEDUNSIGNEDINTEGRAL,
1067           FLG_NUMLITERAL,
1068           FLG_CHARINTLITERAL,
1069           FLG_ZEROBOOL,
1070           FLG_BUFFEROVERFLOWHIGH,
1071           INVALID_FLAG 
1072           } ;
1073
1074       SETFLAGS ();
1075     }
1076   else if (cstring_equalLit (s, "checks"))
1077     {
1078       flagcode modeflags[] = 
1079         { 
1080           FLG_EXPORTLOCAL, FLG_IMPTYPE,
1081           FLG_STATETRANSFER, FLG_STATEMERGE,
1082           FLG_CHECKSTRICTGLOBALIAS,
1083           FLG_CHECKEDGLOBALIAS,
1084           FLG_CHECKMODGLOBALIAS,
1085           FLG_UNCHECKEDGLOBALIAS,
1086           FLG_FORMATCONST,
1087           FLG_EXITARG, FLG_PTRNUMCOMPARE, 
1088           FLG_BOOLCOMPARE, FLG_UNSIGNEDCOMPARE, 
1089           FLG_MACROUNDEF, FLG_MUSTMOD, FLG_ALLGLOBALS,
1090           FLG_PREDBOOLOTHERS, FLG_PREDBOOLPTR, FLG_PREDBOOLINT,
1091           FLG_USEALLGLOBS, FLG_MUTREP, FLG_RETALIAS, 
1092           FLG_RETEXPOSE, FLG_ASSIGNEXPOSE, FLG_CASTEXPOSE,
1093           FLG_FUNCUNUSED, FLG_GLOBALSIMPMODIFIESNOTHING,
1094           FLG_TYPEUNUSED, FLG_FIELDUNUSED, FLG_PARAMUNUSED, FLG_VARUNUSED,
1095           FLG_CONSTUNUSED, FLG_ENUMMEMUNUSED,
1096           FLG_NOEFFECT, FLG_EXPORTHEADER, FLG_EXPORTHEADERVAR,
1097           FLG_RETVALOTHER, FLG_RETVALBOOL, FLG_RETVALINT,
1098           FLG_SPECUNDEF, FLG_IMPCHECKMODINTERNALS,
1099           FLG_DECLUNDEF, FLG_INCONDEFS, FLG_INCONDEFSLIB, 
1100           FLG_MISPLACEDSHAREQUAL, FLG_REDUNDANTSHAREQUAL,
1101           FLG_MATCHFIELDS, 
1102           FLG_MACROPARAMS,
1103           FLG_MACROASSIGN,
1104           FLG_DECLPARAMMATCH,
1105           FLG_FCNDEREF,
1106           FLG_FIRSTCASE,
1107           FLG_SEFPARAMS, FLG_SEFUNSPEC, FLG_MACROSTMT, FLG_MACROPARENS, 
1108           FLG_MACROCONSTDECL,
1109           FLG_MACROFCNDECL,
1110           FLG_MACROREDEF, 
1111           FLG_INFLOOPS, FLG_INFLOOPSUNCON,
1112           FLG_UNREACHABLE, 
1113           FLG_NORETURN, FLG_CASEBREAK, FLG_MISSCASE,
1114           FLG_EVALORDER, FLG_USEDEF, 
1115
1116           FLG_NESTEDEXTERN, 
1117
1118           /* warn use flags */
1119           FLG_MULTITHREADED, FLG_PORTABILITY, FLG_SUPERUSER, FLG_IMPLEMENTATIONOPTIONAL,
1120           FLG_BUFFEROVERFLOWHIGH,
1121
1122           /* memchecks flags */
1123
1124           FLG_NULLSTATE, FLG_NULLDEREF, FLG_NULLASSIGN,
1125           FLG_NULLPASS, FLG_NULLRET,
1126
1127           FLG_COMPDEF, FLG_COMPMEMPASS, FLG_UNIONDEF, FLG_RETSTACK,       
1128
1129           /* memtrans flags */
1130           FLG_EXPOSETRANS,
1131           FLG_OBSERVERTRANS,
1132           FLG_DEPENDENTTRANS,
1133           FLG_NEWREFTRANS,
1134           FLG_ONLYTRANS,
1135           FLG_OWNEDTRANS,
1136           FLG_FRESHTRANS,
1137           FLG_SHAREDTRANS,
1138           FLG_TEMPTRANS,
1139           FLG_KEPTTRANS,
1140           FLG_REFCOUNTTRANS,
1141           FLG_STATICTRANS,
1142           FLG_UNKNOWNTRANS,
1143           FLG_STATICINITTRANS,
1144           FLG_UNKNOWNINITTRANS,
1145           FLG_KEEPTRANS,
1146           FLG_IMMEDIATETRANS,
1147           FLG_ONLYUNQGLOBALTRANS,
1148           FLG_USERELEASED, FLG_ALIASUNIQUE, FLG_MAYALIASUNIQUE,
1149           FLG_MUSTFREE, FLG_MUSTDEFINE, FLG_GLOBSTATE, 
1150           FLG_COMPDESTROY, FLG_MUSTNOTALIAS,
1151           FLG_MEMIMPLICIT,
1152           FLG_BRANCHSTATE, 
1153           FLG_NULLPOINTERARITH,
1154           FLG_SHADOW, FLG_DEPARRAYS,
1155           FLG_REDECL, FLG_READONLYSTRINGS, FLG_READONLYTRANS,
1156           FLG_LOOPLOOPBREAK, FLG_SWITCHLOOPBREAK, FLG_MODGLOBS,
1157           FLG_CHECKSTRICTGLOBALS, FLG_IMPCHECKEDSPECGLOBALS,
1158           FLG_MACROMATCHNAME, FLG_WARNLINTCOMMENTS,
1159           FLG_INCLUDENEST, FLG_ANSIRESERVED, FLG_CPPNAMES, 
1160           FLG_NOPARAMS, FLG_IFEMPTY, FLG_WHILEEMPTY, FLG_REALCOMPARE,
1161           FLG_BOOLOPS, FLG_SHIFTSIGNED,
1162           FLG_BUFFEROVERFLOWHIGH, FLG_BUFFEROVERFLOW,
1163           INVALID_FLAG } ;
1164
1165       SETFLAGS ();
1166     }
1167   else if (cstring_equalLit (s, "strict"))
1168     {
1169       flagcode modeflags[] = 
1170         { 
1171           FLG_CHECKSTRICTGLOBALIAS,
1172           FLG_CHECKEDGLOBALIAS,
1173           FLG_CHECKMODGLOBALIAS,
1174           FLG_UNCHECKEDGLOBALIAS,
1175           FLG_MODFILESYSTEM,
1176           FLG_MACROMATCHNAME,
1177           FLG_FORMATCONST,
1178           FLG_STATETRANSFER, FLG_STATEMERGE,
1179           FLG_MACROUNDEF, FLG_MUTREP, FLG_MUSTMOD,
1180           FLG_ALLGLOBALS, FLG_IMPTYPE,
1181           FLG_MODNOMODS, FLG_MODGLOBSUNSPEC, FLG_MODSTRICTGLOBSUNSPEC,
1182           FLG_GLOBUNSPEC, FLG_SIZEOFTYPE,
1183           FLG_EXPORTHEADER, FLG_EXPORTHEADERVAR,
1184           FLG_NOPARAMS, FLG_OLDSTYLE, FLG_EXITARG, 
1185           FLG_RETSTACK,
1186           FLG_FCNDEREF,
1187           FLG_ONLYUNQGLOBALTRANS,
1188           FLG_GLOBALSIMPMODIFIESNOTHING,
1189           FLG_PREDBOOLOTHERS, FLG_PREDBOOLPTR, FLG_PREDBOOLINT,
1190           FLG_INTERNALGLOBS, FLG_INTERNALGLOBSNOGLOBS,
1191           FLG_USEALLGLOBS, FLG_RETALIAS, 
1192           FLG_MODGLOBS, FLG_MODGLOBSUNSPEC, FLG_MODGLOBSUNCHECKED,
1193           FLG_RETEXPOSE, FLG_ASSIGNEXPOSE, FLG_CASTEXPOSE,
1194           FLG_NOEFFECTUNCON, FLG_EVALORDERUNCON,
1195           FLG_FUNCUNUSED,
1196           FLG_EXPORTITER, FLG_EXPORTCONST,
1197           FLG_TYPEUNUSED, FLG_FIELDUNUSED, FLG_PARAMUNUSED, FLG_TOPUNUSED,
1198           FLG_CONSTUNUSED, FLG_ENUMMEMUNUSED,
1199           FLG_VARUNUSED, 
1200           FLG_NULLPOINTERARITH, FLG_POINTERARITH, 
1201           FLG_PTRNUMCOMPARE, 
1202           FLG_BOOLCOMPARE, FLG_UNSIGNEDCOMPARE,
1203           FLG_NOEFFECT, FLG_RETVALINT, FLG_RETVALBOOL, FLG_RETVALOTHER, 
1204           FLG_ANSIRESERVED, FLG_ANSIRESERVEDLOCAL, FLG_CPPNAMES,
1205           FLG_RETVALBOOL, FLG_RETVALINT, FLG_SPECUNDEF, 
1206           FLG_DECLUNDEF, FLG_STRICTOPS, FLG_INCONDEFS, 
1207           FLG_MISPLACEDSHAREQUAL, FLG_REDUNDANTSHAREQUAL,
1208           FLG_INCONDEFSLIB, FLG_MATCHFIELDS, FLG_EXPORTMACRO, FLG_EXPORTVAR, 
1209           FLG_EXPORTFCN, FLG_EXPORTTYPE, FLG_EXPORTLOCAL, FLG_MACROPARAMS, 
1210           FLG_MACROASSIGN,
1211           FLG_SEFPARAMS, FLG_SEFUNSPEC, FLG_MACROSTMT, FLG_MACROPARENS, 
1212           FLG_MACROFCNDECL,
1213           FLG_MACROCONSTDECL,
1214           FLG_MACROREDEF, FLG_MACROEMPTY,
1215           FLG_INFLOOPS, FLG_INFLOOPSUNCON,
1216           FLG_UNREACHABLE, 
1217           FLG_NORETURN, FLG_CASEBREAK, FLG_MISSCASE, FLG_USEDEF,
1218           FLG_EVALORDER,
1219           FLG_MODUNCON, FLG_MODUNCONNOMODS, FLG_MODINTERNALSTRICT,
1220           FLG_MODOBSERVERUNCON,
1221
1222           FLG_NESTEDEXTERN, 
1223           FLG_FIRSTCASE,
1224
1225           /* warn use flags */
1226           FLG_MULTITHREADED, FLG_PORTABILITY, FLG_SUPERUSER, FLG_IMPLEMENTATIONOPTIONAL,
1227           FLG_BUFFEROVERFLOWHIGH,
1228           FLG_BUFFEROVERFLOW, FLG_TOCTOU,
1229
1230           /* memchecks flags */
1231           FLG_NULLSTATE, FLG_NULLDEREF, FLG_NULLASSIGN,
1232           FLG_NULLPASS, FLG_NULLRET,
1233
1234           FLG_COMPDEF, FLG_COMPMEMPASS, FLG_UNIONDEF,
1235
1236           /* memtrans flags */
1237           FLG_EXPOSETRANS,
1238           FLG_OBSERVERTRANS,
1239           FLG_DEPENDENTTRANS,
1240           FLG_NEWREFTRANS,
1241           FLG_ONLYTRANS,
1242           FLG_OWNEDTRANS,
1243           FLG_FRESHTRANS,
1244           FLG_SHAREDTRANS,
1245           FLG_TEMPTRANS,
1246           FLG_KEPTTRANS,
1247           FLG_REFCOUNTTRANS,
1248           FLG_STATICTRANS,
1249           FLG_UNKNOWNTRANS,
1250           FLG_KEEPTRANS,
1251           FLG_IMMEDIATETRANS,
1252           FLG_STATICINITTRANS,
1253           FLG_UNKNOWNINITTRANS,
1254
1255           FLG_USERELEASED, FLG_ALIASUNIQUE, FLG_MAYALIASUNIQUE,
1256           FLG_MUSTFREE, FLG_MUSTDEFINE, FLG_GLOBSTATE, 
1257           FLG_COMPDESTROY, FLG_MUSTNOTALIAS,
1258           FLG_MEMIMPLICIT,
1259           FLG_BRANCHSTATE, 
1260
1261           FLG_DECLPARAMNAME, FLG_DECLPARAMMATCH,
1262
1263           FLG_SHADOW, FLG_DEPARRAYS, 
1264           FLG_STRICTDESTROY, FLG_STRICTUSERELEASED, FLG_STRICTBRANCHSTATE,
1265           FLG_REDECL, FLG_READONLYSTRINGS, FLG_READONLYTRANS,
1266           FLG_LOOPLOOPBREAK, FLG_LOOPSWITCHBREAK, FLG_SWITCHLOOPBREAK,
1267           FLG_SWITCHSWITCHBREAK, FLG_LOOPLOOPCONTINUE,
1268           FLG_CHECKSTRICTGLOBALS, FLG_IMPCHECKEDSPECGLOBALS,
1269           FLG_ALLGLOBALS, FLG_IMPCHECKEDSTRICTGLOBALS,
1270           FLG_IMPCHECKEDSTRICTSTATICS,
1271           FLG_IMPCHECKEDSTRICTSPECGLOBALS,
1272           FLG_IMPCHECKMODINTERNALS,
1273           FLG_WARNMISSINGGLOBALS, FLG_WARNMISSINGGLOBALSNOGLOBS,
1274           FLG_WARNLINTCOMMENTS, FLG_ANSIRESERVEDLOCAL,
1275           FLG_INCLUDENEST, FLG_STRINGLITERALLEN,
1276           FLG_NUMSTRUCTFIELDS, FLG_NUMENUMMEMBERS,
1277           FLG_CONTROLNESTDEPTH,
1278           FLG_FORBLOCK, FLG_WHILEBLOCK,
1279           FLG_FOREMPTY, FLG_WHILEEMPTY,
1280           FLG_IFEMPTY, FLG_IFBLOCK,
1281           FLG_ELSEIFCOMPLETE,
1282           FLG_REALCOMPARE, FLG_BOOLOPS,
1283           FLG_SYSTEMDIRERRORS, FLG_UNUSEDSPECIAL,
1284
1285           FLG_SHIFTSIGNED, FLG_BITWISEOPS,
1286           FLG_BUFFEROVERFLOWHIGH, FLG_BUFFEROVERFLOW,
1287           INVALID_FLAG
1288         } ;
1289
1290       SETFLAGS ();
1291     }
1292   else
1293     {
1294       llcontbug (message ("context_setMode: bad mode: %s", s));
1295      }
1296 }
1297
1298 bool
1299 context_isSpecialFile (cstring fname)
1300 {
1301   cstring ext = fileLib_getExtension (fname);
1302   
1303   return (cstring_equalLit (ext, ".y") 
1304           || cstring_equalLit (ext, ".l")
1305           || cstring_equalLit (fname, "lex.yy.c"));
1306 }
1307
1308 bool
1309 context_isSystemDir (cstring dir)
1310 {
1311   cstring sysDirs = context_exposeString (FLG_SYSTEMDIRS);
1312   char *thisdir = cstring_toCharsSafe (sysDirs);
1313   char *nextdir = strchr (thisdir, PATH_SEPARATOR);
1314
1315   if (nextdir != NULL)
1316     {
1317       *nextdir = '\0';
1318       nextdir += 1;
1319     }
1320
1321   while (thisdir != NULL)
1322     {
1323       DPRINTF (("Test: %s / %s", dir, thisdir));
1324
1325       if (cstring_equalCanonicalPrefix (dir, thisdir))
1326         {
1327           if (nextdir != NULL)
1328             {
1329               *(nextdir - 1) = PATH_SEPARATOR;
1330             }
1331           
1332           return TRUE;
1333         }
1334
1335       if (nextdir != NULL)
1336         {
1337           *(nextdir - 1) = PATH_SEPARATOR;
1338         }
1339
1340       if (nextdir != NULL)
1341         {
1342           thisdir = nextdir;
1343           nextdir = strchr (thisdir, PATH_SEPARATOR);
1344           
1345           if (nextdir != NULL)
1346             {
1347               *nextdir = '\0';
1348               nextdir += 1;
1349             } 
1350         }
1351       else
1352         {
1353           break;
1354         }
1355     } 
1356
1357   DPRINTF (("Returns FALSE"));
1358   return FALSE;
1359 }
1360
1361 void
1362 context_addFileAccessType (typeId t)
1363 {
1364   cstring base;
1365
1366   if (context_inFunctionLike ())
1367     {
1368       gc.acct = typeIdSet_insert (gc.acct, t);
1369     }
1370   
1371   gc.facct = typeIdSet_insert (gc.facct, t);
1372   
1373   base = fileloc_getBase (g_currentloc);
1374   insertModuleAccess (base, t);
1375   DPRINTF (("Add file access: %s / %s", typeIdSet_unparse (gc.facct),
1376             typeIdSet_unparse (gc.acct)));
1377 }
1378
1379 void
1380 context_removeFileAccessType (typeId t)
1381 {
1382   if (gc.kind == CX_FUNCTION || gc.kind == CX_MACROFCN 
1383       || gc.kind == CX_UNKNOWNMACRO)
1384     {
1385       gc.acct = typeIdSet_removeFresh (gc.acct, t);
1386     }
1387   
1388   gc.facct = typeIdSet_removeFresh (gc.facct, t);
1389   gc.nacct = typeIdSet_insert (gc.nacct, t);
1390 }
1391
1392 void context_enterFunctionHeader (void)
1393 {
1394   if (context_getFlag (FLG_GRAMMAR))
1395     {
1396       lldiagmsg (message ("Enter function header: %q", context_unparse ()));
1397     }
1398
1399   if (gc.kind != CX_GLOBAL)
1400     {
1401       llparseerror (cstring_makeLiteral
1402                     ("Likely parse error.  Function header outside global context."));
1403     }
1404   else
1405     {
1406       llassert (gc.kind == CX_GLOBAL);
1407       DPRINTF (("Enter function header!"));
1408       gc.inFunctionHeader = TRUE;
1409     }
1410 }
1411
1412 void context_exitFunctionHeader (void)
1413 {
1414   if (context_getFlag (FLG_GRAMMAR))
1415     {
1416       lldiagmsg (message ("Exit function header: %q", context_unparse ()));
1417     }
1418
1419   DPRINTF (("Exit function header!"));
1420   gc.inFunctionHeader = FALSE;
1421 }
1422
1423 bool context_inFunctionHeader (void)
1424 {
1425   return (gc.inFunctionHeader);
1426 }
1427
1428 void context_enterFunctionDeclaration (uentry e)
1429 {
1430   if (context_getFlag (FLG_GRAMMAR))
1431     {
1432       lldiagmsg (message ("Enter function declaration: %q", context_unparse ()));
1433     }
1434
1435   DPRINTF (("Enter function decl"));
1436   llassert (gc.savekind == CX_ERROR);
1437   gc.savekind = gc.kind;
1438   gc.savecont = gc.cont;
1439   gc.kind = CX_FCNDECLARATION;
1440   gc.cont.fcn = e;
1441 }
1442
1443 void context_exitFunctionDeclaration (void)
1444 {
1445   if (context_getFlag (FLG_GRAMMAR))
1446     {
1447       lldiagmsg (message ("Exit function declaration: %q", context_unparse ()));
1448     }
1449
1450   DPRINTF (("Exit function decl"));
1451   llassert (gc.savekind != CX_ERROR);
1452   llassert (gc.kind == CX_FCNDECLARATION);
1453   gc.kind = gc.savekind;
1454   gc.cont = gc.savecont;
1455
1456   gc.savekind = CX_ERROR;
1457
1458   if (context_getFlag (FLG_GRAMMAR))
1459     {
1460       lldiagmsg (message ("After exit function declaration: %q", context_unparse ()));
1461     }
1462 }
1463
1464 bool context_inFunctionDeclaration (void)
1465 {
1466   return (gc.kind == CX_FCNDECLARATION);
1467 }
1468
1469
1470 void
1471 context_enterMacro (/*@observer@*/ uentry e)
1472 {
1473   context_enterFunction (e);
1474   gc.kind = CX_MACROFCN;
1475 }
1476
1477 void
1478 context_enterUnknownMacro (/*@dependent@*/ uentry e)
1479 {
1480   llassert (uentry_isFunction (e));
1481   context_enterFunction (e);
1482   gc.kind = CX_UNKNOWNMACRO;
1483 }
1484
1485 void context_enterAndClause (exprNode e)
1486 {
1487   
1488   usymtab_trueBranch (guardSet_copy (exprNode_getGuards (e)));
1489   pushClause (ANDCLAUSE);
1490 }
1491
1492 void context_enterOrClause (exprNode e)
1493 {
1494   usymtab_trueBranch (guardSet_invert (exprNode_getGuards (e)));
1495   pushClause (ORCLAUSE);
1496 }
1497
1498 bool context_inDeepLoop (void)
1499 {
1500   bool inLoop = FALSE;
1501
1502   clauseStack_elements (gc.clauses, el)
1503     {
1504       if (clause_isLoop (el))
1505         {
1506           if (inLoop)
1507             {
1508               return TRUE;
1509             }
1510
1511           inLoop = TRUE;
1512         }
1513     } end_clauseStack_elements;
1514
1515   return FALSE;
1516 }
1517
1518 bool context_inDeepSwitch (void)
1519 {
1520   bool inLoop = FALSE;
1521
1522   clauseStack_elements (gc.clauses, el)
1523     {
1524       if (clause_isSwitch (el))
1525         {
1526           if (inLoop)
1527             {
1528               return TRUE;
1529             }
1530
1531           inLoop = TRUE;
1532         }
1533     } end_clauseStack_elements;
1534
1535   return FALSE;
1536 }
1537
1538 bool context_inDeepLoopSwitch (void)
1539 {
1540   bool inLoop = FALSE;
1541
1542   clauseStack_elements (gc.clauses, el)
1543     {
1544       if (clause_isBreakable (el))
1545         {
1546           if (inLoop)
1547             {
1548               return TRUE;
1549             }
1550
1551           inLoop = TRUE;
1552         }
1553     } end_clauseStack_elements;
1554
1555   return FALSE;
1556 }
1557
1558 clause context_breakClause (void)
1559 {
1560   clauseStack_elements (gc.clauses, el)
1561     {
1562       if (clause_isSwitch (el))
1563         {
1564           return el;
1565         }
1566       else if (clause_isLoop (el))
1567         {
1568           return el;
1569         }
1570       else
1571         {
1572           ;
1573         }
1574     } end_clauseStack_elements;
1575
1576   return NOCLAUSE;
1577 }
1578
1579 clause context_nextBreakClause (void)
1580 {
1581   bool hasOne = FALSE;
1582
1583   clauseStack_elements (gc.clauses, el)
1584     {
1585       if (clause_isBreakable (el))
1586         {
1587           if (hasOne)
1588             {
1589               return el;
1590             }
1591           else
1592             {
1593               hasOne = TRUE;
1594             }
1595         }
1596     } end_clauseStack_elements;
1597
1598   return NOCLAUSE;
1599 }
1600   
1601 bool context_inConditional (void)
1602 {
1603   clauseStack_elements (gc.clauses, el)
1604     {
1605       /*
1606       ** Should also include TRUECLAUSE and FALSECLAUSE, but need
1607       ** to distinguish if from ? for this
1608       */
1609
1610       if (clause_isBreakable (el) && (el != DOWHILECLAUSE))
1611         {
1612           return TRUE;
1613         }
1614     } end_clauseStack_elements;
1615
1616   return FALSE;
1617 }
1618
1619 void context_exitAndClause (exprNode pred, exprNode tbranch)
1620 {
1621   context_setJustPopped ();
1622   
1623   llassert (gc.inclause == ANDCLAUSE);
1624   
1625   usymtab_popAndBranch (pred, tbranch);
1626   clauseStack_pop (gc.clauses);
1627   gc.inclause = topClause (gc.clauses);
1628 }
1629
1630 void context_exitOrClause (exprNode pred, exprNode tbranch)
1631 {
1632   context_setJustPopped ();
1633   
1634   llassert (gc.inclause == ORCLAUSE);
1635   
1636   usymtab_popOrBranch (pred, tbranch);
1637   clauseStack_pop (gc.clauses);
1638   gc.inclause = topClause (gc.clauses);
1639 }
1640
1641 static void context_enterCondClauseAux (clause cl)
1642      /*@modifies gc@*/
1643 {
1644   pushClause (cl);
1645 }
1646
1647 static void context_enterTrueAux (exprNode e, clause cl)
1648    /*@modifies gc@*/
1649 {
1650   usymtab_trueBranch (guardSet_copy (exprNode_getGuards (e)));
1651   pushClause (cl);
1652 }
1653
1654 void context_enterIterClause (void)
1655 {
1656   context_enterTrueAux (exprNode_undefined, ITERCLAUSE);
1657 }
1658
1659 void context_enterDoWhileClause (void)
1660 {
1661   pushClause (DOWHILECLAUSE);
1662 }
1663
1664 void context_enterWhileClause (exprNode e)
1665 {
1666   context_enterTrueAux (e, WHILECLAUSE);
1667 }
1668
1669 void context_enterForClause (exprNode e)
1670 {
1671   context_enterTrueAux (e, FORCLAUSE);
1672 }
1673
1674 void context_enterTrueClause (exprNode e)
1675 {
1676   context_enterTrueAux (e, TRUECLAUSE);
1677 }
1678
1679 void context_enterSwitch (exprNode e)
1680 {
1681   
1682   usymtab_switchBranch (e);
1683   context_enterCondClauseAux (SWITCHCLAUSE);
1684 }
1685
1686 void context_exitSwitch (exprNode e, bool allpaths)
1687 {
1688     usymtab_exitSwitch (e, allpaths);
1689   
1690   while (clause_isCase (clauseStack_top (gc.clauses)))
1691     {
1692       clauseStack_pop (gc.clauses);
1693     }
1694   
1695   context_exitClauseSimp ();
1696 }
1697
1698 void context_enterCaseClause (exprNode e)
1699 {
1700   bool branch = FALSE;
1701  
1702   DPRINTF (("Enter case clause!"));
1703
1704   branch = usymtab_newCase (exprNode_undefined, e);
1705   
1706   if (branch)
1707     {
1708       context_enterCondClauseAux (CASECLAUSE);
1709     }
1710 }
1711
1712 static void context_enterFalseClauseAux (exprNode e, clause cl)
1713      /*@modifies gc@*/
1714 {
1715   usymtab_altBranch (guardSet_invert (exprNode_getGuards (e)));
1716   gc.inclause = cl;
1717   clauseStack_switchTop (gc.clauses, cl);
1718 }
1719
1720 void context_enterFalseClause (exprNode e)
1721 {
1722   
1723   context_enterFalseClauseAux (e, FALSECLAUSE);
1724 }
1725
1726 void
1727 context_enterConstantMacro (/*@exposed@*/ /*@dependent@*/ uentry e)
1728 {
1729   gc.kind = CX_MACROCONST;
1730   gc.cont.fcn = e;
1731   gc.showfunction = context_getFlag (FLG_SHOWFUNC);
1732
1733   gc.acct = typeIdSet_subtract (typeIdSet_union (gc.facct, uentry_accessType (e)), 
1734                                 gc.nacct);
1735
1736   
1737   usymtab_enterScope ();
1738   sRef_enterFunctionScope ();
1739
1740   gc.globs = globSet_undefined;
1741   globSet_clear (gc.globs_used);
1742   gc.mods = sRefSet_undefined;
1743 }
1744
1745 uentry context_getHeader (void)
1746 {
1747   if (!(context_inFunctionLike () || (gc.kind == CX_MACROCONST)))
1748     {
1749       llfatalbug (message ("context_getHeader: bad call: %q",
1750                            context_unparse ()));
1751     }
1752
1753   return (gc.cont.fcn);
1754 }
1755
1756 void
1757 context_setFunctionDefined (fileloc loc)
1758 {
1759   switch (gc.kind)
1760     {
1761     case CX_UNKNOWNMACRO:
1762     case CX_FUNCTION:
1763     case CX_MACROFCN:
1764       uentry_setFunctionDefined (gc.cont.fcn, loc);
1765       break;
1766     default:
1767       /* (not a bug because of parse errors) */
1768       break;
1769     }
1770 }
1771
1772 void
1773 context_enterFunction (/*@exposed@*/ uentry e)
1774 {
1775   gc.kind = CX_FUNCTION;
1776   gc.cont.fcn = e;
1777
1778   DPRINTF (("Enter function: %s", uentry_unparse (e)));
1779
1780   if (uentry_hasAccessType (e))
1781     {
1782       gc.acct = typeIdSet_subtract (typeIdSet_union (gc.facct, uentry_accessType (e)), 
1783                                     gc.nacct);
1784     }
1785   else
1786     {
1787       gc.acct = gc.facct;
1788     }
1789
1790   DPRINTF (("Enter function: %s / %s", uentry_unparse (e), 
1791             typeIdSet_unparse (gc.acct)));
1792
1793   gc.showfunction = context_getFlag (FLG_SHOWFUNC);
1794   
1795   gc.globs = uentry_getGlobs (e);
1796   globSet_clear (gc.globs_used);
1797   gc.mods = uentry_getMods (e);
1798
1799   usymtab_enterFunctionScope (e);
1800   sRef_enterFunctionScope ();
1801 }
1802
1803 void
1804 context_enterOldStyleScope (void)
1805 {
1806   gc.kind = CX_OLDSTYLESCOPE;
1807   DPRINTF (("Enter old style scope!"));
1808   usymtab_enterFunctionScope (uentry_undefined);
1809 }
1810
1811 void 
1812 context_completeOldStyleFunction (uentry e)
1813 {
1814   llassert (gc.kind == CX_OLDSTYLESCOPE);
1815
1816   gc.kind = CX_FUNCTION;
1817   gc.cont.fcn = e;
1818   
1819   DPRINTF (("Enter function: %s", uentry_unparse (e)));
1820   
1821   if (uentry_hasAccessType (e))
1822     {
1823       gc.acct = typeIdSet_subtract (typeIdSet_union (gc.facct, uentry_accessType (e)), 
1824                                     gc.nacct);
1825     }
1826   else
1827     {
1828       gc.acct = gc.facct;
1829     }
1830   
1831   DPRINTF (("Enter function: %s / %s", uentry_unparse (e), 
1832             typeIdSet_unparse (gc.acct)));
1833   
1834   gc.showfunction = context_getFlag (FLG_SHOWFUNC);
1835   
1836   if (!globSet_isEmpty (uentry_getGlobs (e))) 
1837     {
1838       llfatalerror (message ("%q: Old-style function declaration uses a clause (rewrite with function parameters): %q",
1839                              fileloc_unparse (g_currentloc), uentry_unparse (e)));
1840     }
1841
1842   gc.showfunction = context_getFlag (FLG_SHOWFUNC);
1843   
1844   gc.globs = uentry_getGlobs (e);
1845   globSet_clear (gc.globs_used);
1846
1847   gc.mods = uentry_getMods (e);
1848
1849   if (!sRefSet_isEmpty (gc.mods))
1850     {
1851       llfatalerror (message ("%q: Old-style function declaration uses a clause (rewrite with function parameters): %q",
1852                              fileloc_unparse (g_currentloc), uentry_unparse (e)));
1853     }
1854
1855   sRef_enterFunctionScope ();
1856 }
1857
1858 static bool context_checkStrictGlobals (void)
1859 {
1860   return (context_getFlag (FLG_CHECKSTRICTGLOBALS));
1861 }
1862
1863 static bool context_hasGlobs (void)
1864 {
1865   if (context_inFunctionLike ())
1866     {
1867       return (uentry_hasGlobs (gc.cont.fcn));
1868     }
1869   else
1870     {
1871       return (FALSE);
1872     }
1873 }
1874
1875 static bool context_checkCheckedGlobals (void)
1876 {
1877   return (context_getFlag (FLG_GLOBALS)
1878           && (context_getFlag (FLG_GLOBUNSPEC)
1879               || context_hasGlobs ()));
1880 }
1881
1882 static bool context_checkUnknownGlobals (void)
1883 {
1884   /* should be uentry_hasGlobs ? */
1885
1886   return (context_getFlag (FLG_ALLGLOBALS)
1887           && (context_getFlag (FLG_GLOBUNSPEC)
1888               || context_hasGlobs ()));
1889 }      
1890
1891 static bool context_checkUncheckedGlobals (void)
1892 {
1893   return (FALSE);
1894 }      
1895
1896 bool 
1897 context_checkExport (uentry e)
1898 {
1899   if (!gc.anyExports) return FALSE;
1900
1901   if (uentry_isFunction (e)
1902       || (uentry_isVariable (e) && ctype_isFunction (uentry_getType (e))))
1903     {
1904       return context_maybeSet (FLG_EXPORTFCN);
1905     }
1906   else if (uentry_isExpandedMacro (e))
1907     {
1908       return context_maybeSet (FLG_EXPORTMACRO);
1909     }
1910   else if (uentry_isVariable (e))
1911     {
1912       return context_maybeSet (FLG_EXPORTVAR);
1913     }
1914   else if (uentry_isEitherConstant (e))
1915     {
1916       return context_maybeSet (FLG_EXPORTCONST);
1917     }
1918   else if (uentry_isIter (e) || uentry_isEndIter (e))
1919     {
1920       return context_maybeSet (FLG_EXPORTITER);
1921     }
1922   else if (uentry_isDatatype (e))
1923     {
1924       return context_maybeSet (FLG_EXPORTTYPE);
1925     }
1926   else
1927     {
1928       BADEXIT;
1929     }
1930 }
1931               
1932 bool
1933 context_checkGlobUse (uentry glob)
1934 {
1935   
1936   if (uentry_isCheckedStrict (glob))
1937     {
1938       return context_checkStrictGlobals ();
1939     }
1940   else if (uentry_isChecked (glob))
1941     {
1942       return context_checkCheckedGlobals ();
1943     }
1944   else if (uentry_isCheckedUnknown (glob) || uentry_isCheckMod (glob))
1945     {
1946       return context_checkUnknownGlobals ();
1947     }
1948   else 
1949     {
1950       llassert (uentry_isUnchecked (glob));
1951
1952       return context_checkUncheckedGlobals ();
1953     }
1954 }
1955
1956 bool
1957 context_checkAliasGlob (uentry glob)
1958 {
1959   if (uentry_isCheckedStrict (glob))
1960     {
1961       return gc.flags[FLG_CHECKSTRICTGLOBALIAS];
1962     }
1963   else if (uentry_isChecked (glob))
1964     {
1965       return gc.flags[FLG_CHECKEDGLOBALIAS];
1966     }
1967   else if (uentry_isCheckMod (glob))
1968     {
1969       return gc.flags[FLG_CHECKMODGLOBALIAS];
1970     }
1971   else 
1972     {
1973       llassert (uentry_isUnchecked (glob) || uentry_isCheckedUnknown (glob));
1974
1975       return gc.flags[FLG_UNCHECKEDGLOBALIAS];
1976     }
1977 }
1978
1979 bool context_checkInternalUse (void)
1980 {
1981   if (context_hasGlobs ())
1982     {
1983       return (gc.flags[FLG_INTERNALGLOBS]);
1984     }
1985   else
1986     {
1987       return (gc.flags[FLG_INTERNALGLOBSNOGLOBS]);
1988     }
1989 }
1990
1991 bool
1992 context_checkGlobMod (sRef el)
1993 {
1994   uentry ue = sRef_getUentry (el);
1995
1996   /* no: llassert (sRef_isFileOrGlobalScope (el)); also check local statics */
1997
1998   if (uentry_isCheckedModify (ue)
1999       || (!uentry_isUnchecked (ue) && (gc.flags[FLG_ALLGLOBALS])))
2000     {
2001       if (context_hasMods ())
2002         {
2003           return (gc.flags[FLG_MODGLOBS]);
2004         }
2005       else
2006         {
2007           if (uentry_isCheckedStrict (ue))
2008             {
2009               return (gc.flags[FLG_MODGLOBSUNSPEC]);
2010             }
2011           else
2012             {
2013               return (gc.flags[FLG_MODSTRICTGLOBSUNSPEC]);
2014             }
2015         }
2016     }
2017   else
2018     {
2019       if (context_hasMods ())
2020         {
2021           return (gc.flags[FLG_MODGLOBSUNCHECKED]);
2022         }
2023       else
2024         {
2025           return FALSE;
2026         }
2027     }
2028 }
2029
2030 void
2031 context_usedGlobal (/*@exposed@*/ sRef el)
2032 {
2033   if (!globSet_member (gc.globs_used, el))
2034     {
2035       /* 
2036       ** The first time a global is used in a function, we need
2037       ** to clear the derived sRefs, since they were set for the
2038       ** previous function.
2039       */
2040
2041       sRef_clearDerived (el);
2042       gc.globs_used = globSet_insert (gc.globs_used, el);
2043     }
2044 }
2045
2046 /*@observer@*/ sRefSet
2047 context_modList (void)
2048 {
2049   return gc.mods;
2050 }
2051
2052 bool
2053 context_globAccess (sRef s)
2054 {
2055   llassert (sRef_isFileOrGlobalScope (s) || sRef_isKindSpecial (s));
2056   return (globSet_member (gc.globs, s));
2057 }
2058
2059 bool
2060 context_hasAccess (typeId t)
2061 {
2062   if (context_inFunctionLike ())
2063     {
2064       DPRINTF (("Access %d / %s",
2065                 t, typeIdSet_unparse (gc.acct)));
2066       return (typeIdSet_member (gc.acct, t));
2067     }
2068   else
2069     {
2070       return (context_hasFileAccess (t));
2071     }
2072 }
2073
2074 bool
2075 context_hasFileAccess (typeId t)
2076 {
2077   return (typeIdSet_member (gc.facct, t));
2078 }
2079
2080 /*@only@*/ cstring
2081 context_unparseAccess (void)
2082 {
2083   return (message ("%q / %q", typeIdSet_unparse (gc.acct),
2084                    typeIdSet_unparse (gc.facct)));
2085 }
2086
2087 /*@only@*/ cstring
2088 context_unparseClauses (void)
2089 {
2090   return (clauseStack_unparse (gc.clauses));
2091 }
2092
2093 bool
2094 context_couldHaveAccess (typeId t)
2095 {
2096   if (gc.kind == CX_FUNCTION || gc.kind == CX_MACROFCN || gc.kind == CX_UNKNOWNMACRO)
2097     {
2098       return (typeIdSet_member (gc.acct, t));
2099     }
2100   else
2101     {
2102       return (typeIdSet_member (gc.facct, t)); 
2103     }
2104 }
2105
2106 ctype
2107 context_getRetType (void)
2108 {
2109   ctype f = ctype_undefined;
2110
2111   if (gc.kind == CX_FUNCTION || gc.kind == CX_MACROFCN)
2112     {
2113       f = uentry_getType (gc.cont.fcn);
2114     }
2115   else if (gc.kind == CX_UNKNOWNMACRO)
2116     {
2117       return ctype_unknown;
2118     }
2119   else
2120     {
2121       llcontbuglit ("context_getRetType: not in a function context");
2122       return ctype_unknown;
2123     }
2124
2125   if (!ctype_isFunction (f))
2126     {
2127       if (ctype_isKnown (f))
2128         {
2129           llbuglit ("context_getRetType: not a function");
2130         }
2131
2132       return ctype_unknown;
2133     }
2134   return (ctype_getReturnType (f));
2135 }    
2136
2137 bool
2138 context_hasMods (void)
2139 {
2140   if (context_inFunctionLike ())
2141     {
2142       return (uentry_hasMods (gc.cont.fcn));
2143     }
2144   else
2145     {
2146       return FALSE;
2147     }
2148 }
2149
2150 void
2151 context_exitAllClauses (void)
2152 {  
2153   while (!clauseStack_isEmpty (gc.clauses))
2154     {
2155       clause el = clauseStack_top (gc.clauses);
2156       gc.inclause = el;
2157
2158       if (clause_isNone (el))
2159         {
2160           usymtab_quietExitScope (g_currentloc);
2161           clauseStack_pop (gc.clauses);
2162         }
2163       else
2164         {
2165           context_exitClausePlain ();
2166         }
2167     }
2168
2169   clauseStack_clear (gc.clauses);  
2170   gc.inclause = NOCLAUSE;
2171 }
2172
2173 void
2174 context_exitAllClausesQuiet (void)
2175 {  
2176   while (!clauseStack_isEmpty (gc.clauses))
2177     {
2178       clause el = clauseStack_top (gc.clauses);
2179       gc.inclause = el;
2180
2181       usymtab_quietExitScope (g_currentloc);
2182       clauseStack_pop (gc.clauses);
2183     }
2184
2185   clauseStack_clear (gc.clauses);  
2186   gc.inclause = NOCLAUSE;
2187 }
2188
2189 static
2190 void context_exitClauseSimp (void)
2191 {
2192   
2193   context_setJustPopped ();
2194   clauseStack_pop (gc.clauses);
2195   gc.inclause = topClause (gc.clauses);
2196 }
2197
2198 static
2199 void context_exitCaseClause (void)
2200 {
2201   context_setJustPopped ();
2202   usymtab_popCaseBranch ();
2203   clauseStack_pop (gc.clauses);
2204   gc.inclause = topClause (gc.clauses);
2205 }
2206
2207 static
2208 void context_exitClauseAux (exprNode pred, exprNode tbranch)
2209 {
2210   context_setJustPopped ();
2211   /*@i32 was makeAlt */
2212   usymtab_popTrueBranch (pred, tbranch, gc.inclause);
2213   clauseStack_pop (gc.clauses);
2214   gc.inclause = topClause (gc.clauses);
2215 }
2216
2217 void context_exitTrueClause (exprNode pred, exprNode tbranch)
2218 {
2219   DPRINTF (("Exit true clause: %s", exprNode_unparse (tbranch)));
2220
2221   if (gc.inclause != TRUECLAUSE)
2222     {
2223       llparseerror (cstring_makeLiteral
2224                     ("Likely parse error.  Conditional clauses are inconsistent."));
2225       return;
2226     }
2227   
2228   context_setJustPopped ();  
2229   usymtab_popTrueBranch (pred, tbranch, TRUECLAUSE);
2230   clauseStack_pop (gc.clauses);
2231   gc.inclause = topClause (gc.clauses);  
2232 }
2233
2234 void context_exitIterClause (exprNode body)
2235 {
2236   llassert (gc.inclause == ITERCLAUSE);
2237
2238   context_setJustPopped ();
2239
2240   if (context_getFlag (FLG_LOOPEXEC))
2241     {
2242       usymtab_popTrueExecBranch (exprNode_undefined, body, ITERCLAUSE);
2243     }
2244   else
2245     {
2246       usymtab_popTrueBranch (exprNode_undefined, body, ITERCLAUSE);
2247     }
2248
2249   clauseStack_pop (gc.clauses);
2250   gc.inclause = topClause (gc.clauses);
2251 }
2252
2253 static void context_popCase (void) {
2254   /*
2255   ** If we are exiting an outer clause, sometimes still in a switch case.
2256   **
2257   ** e.g.: 
2258   **
2259   ** switch(a)
2260   ** {
2261   **   case 1:
2262   **     while (c>3)
2263   **       {
2264   **         case 3: ++c;
2265   **       }
2266   ** }     
2267   */
2268
2269   DPRINTF (("Popping case clause: %s",
2270             clauseStack_unparse (gc.clauses)));
2271   
2272   if (gc.inclause == CASECLAUSE) {
2273     context_exitCaseClause ();
2274   }
2275 }
2276
2277 void context_exitWhileClause (exprNode pred, exprNode body)
2278 {
2279   guardSet invGuards = guardSet_invert (exprNode_getGuards (pred));
2280
2281   context_popCase (); 
2282
2283   if (gc.inclause != WHILECLAUSE) {
2284     DPRINTF (("Clause: %s / %s", clause_unparse (gc.inclause),
2285               clauseStack_unparse (gc.clauses)));
2286   }
2287
2288   llassert (gc.inclause == WHILECLAUSE);
2289
2290   context_setJustPopped ();
2291
2292   
2293   /*
2294   ** predicate must be false after while loop (unless there are breaks)
2295   */
2296
2297   if (context_getFlag (FLG_LOOPEXEC))
2298     {
2299       usymtab_popTrueExecBranch (pred, body, WHILECLAUSE);
2300     }
2301   else
2302     {
2303       usymtab_popTrueBranch (pred, body, WHILECLAUSE);
2304     }
2305
2306   
2307   usymtab_addGuards (invGuards);
2308   guardSet_free (invGuards);
2309
2310   clauseStack_pop (gc.clauses);
2311   gc.inclause = topClause (gc.clauses);  
2312 }
2313
2314 void context_exitDoWhileClause (exprNode pred)
2315 {
2316   guardSet invGuards = guardSet_invert (exprNode_getGuards (pred));
2317
2318   if (gc.inclause == CASECLAUSE) {
2319     /* handle Duff's device */
2320     clauseStack_pop (gc.clauses);
2321     gc.inclause = topClause (gc.clauses);
2322   }
2323
2324   llassert (gc.inclause == DOWHILECLAUSE);
2325
2326   context_setJustPopped ();
2327
2328     
2329   usymtab_addGuards (invGuards);
2330   guardSet_free (invGuards);
2331
2332   clauseStack_pop (gc.clauses);
2333   gc.inclause = topClause (gc.clauses);  
2334 }
2335
2336 void context_exitForClause (exprNode forPred, exprNode body)
2337 {
2338   guardSet invGuards = guardSet_invert (exprNode_getForGuards (forPred));
2339
2340   llassert (gc.inclause == FORCLAUSE);
2341   context_setJustPopped ();
2342
2343   /*
2344   ** predicate must be false after while loop (unless there are breaks)
2345   */
2346
2347   if (context_getFlag (FLG_LOOPEXEC))
2348     {
2349       usymtab_popTrueExecBranch (forPred, body, FORCLAUSE);
2350     }
2351   else
2352     {
2353       usymtab_popTrueBranch (forPred, body, FORCLAUSE);
2354     }
2355
2356   usymtab_addGuards (invGuards);
2357   guardSet_free (invGuards);
2358   clauseStack_pop (gc.clauses);
2359   gc.inclause = topClause (gc.clauses);
2360 }
2361
2362 static void context_exitClausePlain (void)
2363 {
2364   llassert (gc.inclause != NOCLAUSE);
2365   
2366   if (gc.inclause == FALSECLAUSE)
2367     {
2368       context_exitClause (exprNode_undefined, exprNode_undefined, exprNode_undefined);
2369     }
2370   else
2371     {
2372       context_exitClauseAux (exprNode_undefined, exprNode_undefined);
2373     }
2374 }
2375
2376 void context_exitClause (exprNode pred, exprNode tbranch, exprNode fbranch)
2377 {
2378   context_setJustPopped ();
2379   
2380   if (gc.inclause == FALSECLAUSE)
2381     {
2382       usymtab_popBranches (pred, tbranch, fbranch, FALSE, FALSECLAUSE);
2383       
2384       llassert (clauseStack_top (gc.clauses) == FALSECLAUSE);
2385
2386       clauseStack_pop (gc.clauses);
2387       gc.inclause = topClause (gc.clauses);
2388     }
2389   else
2390     {
2391       context_exitTrueClause (pred, tbranch);
2392     }
2393 }
2394
2395 void
2396 context_returnFunction (void)
2397 {
2398   usymtab_checkFinalScope (TRUE);
2399 }
2400
2401 void
2402 context_exitFunction (void)
2403 {    
2404   DPRINTF (("Exit function: %s", context_unparse ()));
2405
2406   if (!context_inFunction () && !context_inMacroConstant () 
2407       && !context_inUnknownMacro () 
2408       && !context_inIterDef () && !context_inIterEnd ())
2409     {
2410       /*
2411       ** not a bug because of parse errors
2412       */
2413
2414       BADBRANCH;
2415     }
2416   else
2417     {
2418       if (context_inMacro () && usymtab_inFunctionScope ())
2419         {
2420           usymtab_exitScope (exprNode_undefined);
2421         }
2422       
2423       if (uentry_hasGlobs (gc.cont.fcn))
2424         {
2425           exprChecks_checkUsedGlobs (gc.globs, gc.globs_used);
2426         }
2427       
2428             
2429       if (uentry_hasMods (gc.cont.fcn))
2430         {
2431           if (context_getFlag (FLG_MUSTMOD))
2432             {
2433               exprNode_checkAllMods (gc.mods, gc.cont.fcn);
2434             }
2435         }
2436
2437       DPRINTF (("Exit function: %s", uentry_unparse (gc.cont.fcn)));
2438
2439       /*
2440       ** clear file static modifies
2441       */
2442       
2443       /* do this first to get unused error messages */
2444
2445       usymtab_exitScope (exprNode_undefined);
2446       sRef_exitFunctionScope ();
2447       
2448       gc.showfunction = FALSE;
2449       gc.kind = CX_GLOBAL;
2450       gc.cont.glob = TRUE;
2451       gc.acct = gc.facct; 
2452       gc.globs = globSet_new ();
2453       globSet_clear (gc.globs_used);
2454       gc.mods = sRefSet_new ();
2455     }
2456
2457   llassert (clauseStack_isEmpty (gc.clauses));
2458   llassert (gc.inclause == NOCLAUSE);
2459   DPRINTF (("After exit function: %s", context_unparse ()));
2460 }
2461
2462 void
2463 context_quietExitFunction (void)
2464 {
2465   while (gc.kind == CX_INNER)
2466     { 
2467       context_exitInnerPlain ();
2468     }
2469
2470   if (!context_inFunction () && !context_inMacroConstant () && !context_inUnknownMacro () 
2471       && !context_inIterDef () && !context_inIterEnd ())
2472     {
2473     }
2474   else
2475     {
2476       usymtab_quietExitScope (g_currentloc);
2477
2478       gc.showfunction = FALSE;
2479       gc.kind = CX_GLOBAL;
2480       gc.cont.glob = TRUE;
2481       gc.acct = gc.facct; 
2482       gc.globs = globSet_new ();
2483       globSet_clear (gc.globs_used);
2484       gc.mods = sRefSet_new ();
2485
2486       sRef_exitFunctionScope ();
2487     }
2488 }
2489
2490 /*@observer@*/ uentryList
2491 context_getParams (void)
2492 {
2493   if (context_inFunctionLike ())
2494     {
2495       return (uentry_getParams (gc.cont.fcn));
2496     }
2497   else
2498     {
2499       llcontbug (message ("context_getParams: not in function: %q", context_unparse ()));
2500       return uentryList_undefined;
2501     }
2502 }
2503
2504 /*@observer@*/ globSet
2505 context_getUsedGlobs (void)
2506 {
2507   llassert (gc.kind == CX_FUNCTION || gc.kind == CX_MACROFCN 
2508             || gc.kind == CX_UNKNOWNMACRO || gc.kind == CX_ITERDEF);
2509
2510   return (gc.globs_used);
2511 }
2512
2513 cstring
2514 context_moduleName ()
2515 {
2516   return (fileloc_getBase (g_currentloc));
2517 }
2518
2519 /*@observer@*/ globSet
2520 context_getGlobs (void)
2521 {
2522   llassert (gc.kind == CX_FUNCTION || gc.kind == CX_MACROFCN 
2523             || gc.kind == CX_UNKNOWNMACRO || gc.kind == CX_ITERDEF);
2524
2525   return (gc.globs);
2526 }
2527
2528 void
2529 context_addBoolAccess (void)
2530 {
2531   cstring bname = context_getString (FLG_BOOLTYPE);
2532   typeIdSet boolt = typeIdSet_single (usymtab_getTypeId (bname));
2533   
2534   addModuleAccess (cstring_copy (bname), boolt);
2535
2536   /* for sys/types (perhaps, this is bogus!) */ 
2537   addModuleAccess (cstring_makeLiteral ("types"), boolt); 
2538 }
2539
2540 # if 0
2541 bool
2542 context_canAccessBool (void)
2543 {
2544   return TRUE;
2545 }
2546 # endif
2547
2548 /*
2549   static typeId boolType = typeId_invalid;
2550
2551   if (typeId_isInvalid (boolType))
2552     { 
2553       boolType = usymtab_getTypeId (context_getBoolName ());
2554     }
2555
2556   if (typeId_isInvalid (boolType)) {
2557     return FALSE;
2558   } else {
2559     return (typeIdSet_member (gc.acct, boolType));
2560   }
2561 }
2562 */
2563
2564 /* evs 2000-07-25: old version - replaced */
2565
2566 ctype
2567 context_boolImplementationType () {
2568   /* For now, this is bogus! */
2569   return ctype_int;
2570 }
2571
2572 bool
2573 context_canAccessBool (void)
2574 {
2575   static typeId boolType = typeId_invalid;
2576
2577   if (typeId_isInvalid (boolType))
2578     { 
2579       boolType = usymtab_getTypeId (context_getBoolName ());
2580     }
2581
2582   if (!typeId_isInvalid (boolType))
2583     { 
2584       return context_hasAccess (boolType);
2585     }
2586   else 
2587     {
2588       ;
2589     }
2590
2591   return FALSE; 
2592 }
2593
2594 void
2595 context_setMessageAnnote (/*@only@*/ cstring s)
2596 {
2597   llassert (cstring_isUndefined (gc.msgAnnote));
2598     gc.msgAnnote = s;
2599 }
2600
2601 bool
2602 context_hasMessageAnnote (void)
2603 {
2604   return (cstring_isDefined (gc.msgAnnote));
2605 }
2606
2607 void
2608 context_clearMessageAnnote (void)
2609 {
2610   if (cstring_isDefined (gc.msgAnnote))
2611     {
2612       cstring_free (gc.msgAnnote);
2613       gc.msgAnnote = cstring_undefined;
2614     }
2615 }
2616
2617 /*@only@*/ cstring
2618 context_getMessageAnnote (void)
2619 {
2620   cstring st = gc.msgAnnote;
2621
2622     gc.msgAnnote = cstring_undefined;
2623   return st;
2624 }
2625
2626 void
2627 context_setAliasAnnote (/*@observer@*/ sRef s, /*@observer@*/ sRef t)
2628 {
2629     llassert (sRef_isInvalid (gc.aliasAnnote));
2630   llassert (!sRef_sameName (s, t));
2631   gc.aliasAnnote = s;
2632   gc.aliasAnnoteAls = t;
2633 }
2634
2635 bool
2636 context_hasAliasAnnote (void)
2637 {
2638   return (sRef_isValid (gc.aliasAnnote));
2639 }
2640
2641 void
2642 context_clearAliasAnnote (void)
2643 {
2644   gc.aliasAnnote = sRef_undefined;
2645 }
2646
2647 cstring
2648 context_getAliasAnnote (void)
2649 {
2650   sRef ret = gc.aliasAnnote;
2651   sRef als = gc.aliasAnnoteAls;
2652
2653   llassert (sRef_isValid (ret) && sRef_isValid (als));
2654
2655   gc.aliasAnnote = sRef_undefined;
2656   return (message ("%q aliases %q", sRef_unparse (als), sRef_unparse (ret)));
2657 }
2658
2659 void
2660 context_recordFileModifies (sRefSet mods)
2661 {
2662   gc.modrecs = sRefSetList_add (gc.modrecs, mods);
2663 }
2664
2665 void
2666 context_recordFileGlobals (globSet mods)
2667 {
2668   DPRINTF (("Recording file globals: %s", globSet_unparse (mods)));
2669   /*@access globSet@*/ context_recordFileModifies (mods); /*@noaccess globSet@*/
2670 }
2671
2672 void
2673 context_setCommentMarkerChar (char c)
2674 {
2675   llassert (c != '\0');
2676
2677   context_setValue (FLG_COMMENTCHAR, (int) c);
2678 }
2679
2680 char
2681 context_getCommentMarkerChar (void)
2682 {
2683   return ((char) context_getValue (FLG_COMMENTCHAR));
2684 }
2685
2686 static void
2687 context_setValue (flagcode flag, int val)
2688 {
2689   int index = flagcode_valueIndex (flag);
2690
2691   llassert (index >= 0 && index <= NUMVALUEFLAGS);
2692
2693   switch (flag)
2694     {
2695     case FLG_LINELEN:
2696       if (val <= 0)
2697         {
2698           
2699           llerror_flagWarning (message ("Value for %s must be a positive "
2700                                     "number (given %d)",
2701                                     flagcode_unparse (flag), val));
2702           return;
2703         }
2704       if (flag == FLG_LINELEN && val < MINLINELEN)
2705         {
2706           llerror_flagWarning (message ("Value for %s must be at least %d (given %d)",
2707                                     flagcode_unparse (flag), 
2708                                     MINLINELEN, val));
2709           val = MINLINELEN;
2710         }
2711       break;
2712
2713     case FLG_INCLUDENEST:
2714     case FLG_CONTROLNESTDEPTH:
2715     case FLG_STRINGLITERALLEN:
2716     case FLG_NUMSTRUCTFIELDS:
2717     case FLG_NUMENUMMEMBERS:      
2718     case FLG_INDENTSPACES:
2719       if (val < 0)
2720         {
2721           
2722           llerror_flagWarning (message ("Value for %s must be a non-negative "
2723                                     "number (given %d)",
2724                                     flagcode_unparse (flag), val));
2725           return;
2726         }
2727
2728       break;
2729     default:
2730       break;
2731     }
2732     
2733   gc.values[index] = val;
2734 }
2735
2736 void
2737 context_setValueAndFlag (flagcode flag, int val)
2738 {
2739   gc.flags[flag] = TRUE;
2740   context_setValue (flag, val);
2741 }
2742
2743 int
2744 context_getValue (flagcode flag)
2745 {
2746   int index = flagcode_valueIndex (flag);
2747
2748   llassert (index >= 0 && index <= NUMVALUEFLAGS);
2749   return (gc.values[index]);
2750 }
2751
2752 int
2753 context_getCounter (flagcode flag)
2754 {
2755   int index = flagcode_valueIndex (flag);
2756
2757   llassert (index >= 0 && index <= NUMVALUEFLAGS);
2758   return (gc.counters[index]);
2759 }
2760
2761 void
2762 context_incCounter (flagcode flag)
2763 {
2764   int index = flagcode_valueIndex (flag);
2765
2766   llassert (index >= 0 && index <= NUMVALUEFLAGS);
2767   /* check limit */
2768   gc.counters[index]++;
2769 }
2770
2771 void
2772 context_decCounter (flagcode flag)
2773 {
2774   int index = flagcode_valueIndex (flag);
2775
2776   llassert (index >= 0 && index <= NUMVALUEFLAGS);
2777   gc.counters[index]--;
2778 }
2779
2780 bool context_showFunction (void)
2781 {
2782   return (gc.showfunction);
2783 }
2784
2785 void
2786 context_setString (flagcode flag, cstring val)
2787 {
2788   int index = flagcode_stringIndex (flag);
2789
2790   llassert (index >= 0 && index <= NUMSTRINGFLAGS);
2791
2792   if (flag == FLG_SYSTEMDIRS)
2793     {
2794       llassert (cstring_isDefined (val));
2795
2796       if (cstring_firstChar (val) == '\"')
2797         {
2798           cstring oval = val;
2799           cstring tval = cstring_copy (cstring_suffix (val, 1));
2800         
2801           if (cstring_lastChar (tval) != '\"')
2802             {
2803               int n = cstring_length (tval) - 1;
2804
2805               while (isspace ((int) cstring_getChar (tval, n)))
2806                 {
2807                   n--;
2808                 }
2809
2810               if (cstring_getChar (tval, n) != '\"')
2811                 {
2812                   llerror_flagWarning (message ("Setting -systemdirs to string with unmatching quotes: %s", val));
2813                 }
2814               else
2815                 {
2816                   cstring otval = tval;
2817                   tval = cstring_prefix (tval, n);
2818                   cstring_free (otval);
2819                 }
2820             }
2821           
2822           val = cstring_copy (cstring_clip (tval, cstring_length (tval) - 1));
2823           DPRINTF (("val = %s", val));
2824           cstring_free (tval);
2825           cstring_free (oval);
2826         }
2827     }
2828
2829   if (flag == FLG_TMPDIR)
2830     {
2831       llassert (cstring_isDefined (val));
2832       
2833       if (cstring_length (val) == 0)
2834         {
2835           cstring_free (val);
2836           val = message (".%s", cstring_makeLiteralTemp (CONNECTSTR));
2837         }
2838       else if (cstring_lastChar (val) != CONNECTCHAR)
2839         {
2840           val = cstring_appendChar (val, CONNECTCHAR);
2841         }
2842       else
2843         {
2844           ;
2845         }
2846     }
2847
2848   if (cstring_length (val) >= 1
2849       && cstring_firstChar (val) == '"')
2850     {
2851       llerror_flagWarning (message
2852                        ("setting %s to string beginning with \".  You probably "
2853                         "don't meant to have the \"'s.",
2854                         flagcode_unparse (flag)));
2855     }
2856
2857   if (flag == FLG_BOOLTYPE)
2858     {
2859
2860     }
2861
2862   gc.strings[index] = val;
2863 }
2864
2865 static /*@exposed@*/ cstring
2866 context_exposeString (flagcode flag)
2867 {
2868   int index = flagcode_stringIndex (flag);
2869
2870   llassert (index >= 0 && index <= NUMSTRINGFLAGS);
2871   return (gc.strings[index]);
2872 }
2873
2874 cstring
2875 context_getString (flagcode flag)
2876 {
2877   return (context_exposeString (flag));
2878 }
2879
2880 void
2881 context_resetErrors (void)
2882 {
2883   gc.numerrors = 0;
2884 }
2885
2886 void context_initMod (void)
2887    /*@globals undef gc; @*/
2888 {
2889   gc.kind = CX_GLOBAL;
2890
2891   gc.savekind = CX_ERROR;
2892   gc.savecont.glob = FALSE;
2893
2894   gc.instandardlib = FALSE;
2895   gc.numerrors = 0;
2896   gc.neednl = FALSE;
2897   gc.linesprocessed = 0;
2898   gc.speclinesprocessed = 0;
2899   gc.insuppressregion = FALSE;
2900   gc.macroMissingParams = FALSE;
2901   gc.preprocessing = FALSE;
2902   gc.incommandline = FALSE;
2903   gc.mc = macrocache_create ();
2904   gc.nmods = 0;
2905   gc.maxmods = DEFAULTMAXMODS;
2906   gc.moduleaccess = (maccesst *) dmalloc (sizeof (*gc.moduleaccess) * (gc.maxmods));
2907   
2908   gc.library = FLG_ANSILIB;
2909
2910   gc.locstack = filelocStack_new ();
2911   gc.modrecs = sRefSetList_undefined;
2912   gc.anyExports = FALSE;
2913
2914   gc.ftab = fileTable_create ();
2915   gc.msgLog = messageLog_new ();
2916   gc.inimport = FALSE;
2917   gc.inDerivedFile = FALSE;
2918   gc.inheader = FALSE;
2919   gc.markers = flagMarkerList_new ();
2920   gc.cont.glob = TRUE;
2921   gc.showfunction = FALSE;
2922   gc.msgAnnote = cstring_undefined;
2923   gc.aliasAnnote = sRef_undefined;
2924   gc.aliasAnnoteAls = sRef_undefined;
2925   gc.boolType = ctype_bool;
2926   gc.mods = sRefSet_new ();
2927
2928   gc.saveloc = fileloc_undefined;
2929
2930   gc.inmacrocache = FALSE;
2931   gc.inclause = NOCLAUSE;
2932   gc.clauses = clauseStack_new ();
2933   gc.globs = globSet_new ();
2934   gc.nacct = typeIdSet_emptySet ();
2935   gc.acct = typeIdSet_emptySet ();
2936   gc.facct = typeIdSet_emptySet ();
2937   gc.savedFlags = FALSE;
2938   gc.pushloc = fileloc_undefined;
2939   gc.protectVars = FALSE;
2940   gc.justpopped = FALSE;
2941   gc.isNullGuarded = NO;
2942   gc.globs_used = globSet_undefined;
2943   
2944   allFlagCodes (code)
2945     {
2946       gc.setGlobally[code] = FALSE;
2947       gc.setLocally[code] = FALSE;
2948     } end_allFlagCodes ;
2949
2950   usymtab_initMod ();
2951
2952   context_resetAllFlags ();
2953   assertSet (gc.flags); /* Can't use global in defines */
2954   assertSet (gc.saveflags);
2955   assertSet (gc.values);
2956   assertSet (gc.strings);
2957
2958   conext_resetAllCounters ();
2959   assertSet (gc.counters);
2960
2961   context_setMode (DEFAULT_MODE);
2962
2963   gc.stateTable = metaStateTable_create ();
2964   gc.annotTable = annotationTable_create ();
2965
2966   gc.inFunctionHeader = FALSE;
2967
2968   DPRINTF (("Annotations: \n%s",
2969             cstring_toCharsSafe (annotationTable_unparse (gc.annotTable))));
2970   DPRINTF (("State: \n%s",
2971             cstring_toCharsSafe (metaStateTable_unparse (gc.stateTable))));
2972
2973 }
2974
2975 ctype
2976 context_typeofZero (void)
2977 {
2978   ctype ct = ctype_int;
2979
2980   if (context_getFlag (FLG_ZEROPTR))
2981     {
2982       ct = ctype_makeConj (ct, ctype_voidPointer);
2983     }
2984   
2985   if (context_getFlag (FLG_ZEROBOOL)) {
2986     ct = ctype_makeConj (ct, ctype_bool); 
2987   }
2988
2989   return ct;
2990 }
2991
2992 ctype
2993 context_typeofOne (void)
2994 {
2995   ctype ct = ctype_int;
2996
2997   /* 1 is on longer a bool (was before 2.4)
2998      if (!context_getFlag (FLG_ABSTRACTBOOL))
2999      {
3000      ct = ctype_makeConj (ct, ctype_bool);
3001      }
3002      */
3003
3004   return (ct);
3005 }
3006
3007 /*@only@*/ cstring
3008 context_unparse (void)
3009 {
3010   cstring s;
3011
3012   switch (gc.kind)
3013     {
3014     case CX_LCL:
3015       s = message ("LCL File: %q", fileloc_unparse (g_currentloc));
3016       break;
3017     case CX_LCLLIB:
3018       s = message ("LCL Lib File: %q", fileloc_unparse (g_currentloc));
3019       break;
3020     case CX_GLOBAL:
3021       s = message ("Global Context:%q", fileloc_unparse (g_currentloc));
3022       break;
3023     case CX_INNER:
3024       s = message ("Inner Context [%d] : %q", 
3025                    gc.cont.cdepth,
3026                    fileloc_unparse (g_currentloc));
3027       break;
3028     case CX_FUNCTION:
3029       s = message ("Function %q :%q \n\taccess %q\n\tmodifies %q",
3030                    uentry_unparse (gc.cont.fcn),
3031                    fileloc_unparse (g_currentloc),
3032                    typeIdSet_unparse (gc.acct),
3033                    sRefSet_unparse (gc.mods));
3034       break;
3035     case CX_MACROFCN:
3036       s = message ("Function Macro %q", uentry_unparse (gc.cont.fcn));
3037       break;
3038     case CX_UNKNOWNMACRO:
3039       s = message ("Forward Specified Macro %q", uentry_unparse (gc.cont.fcn));
3040       break;
3041     case CX_MACROCONST:
3042       s = message ("Constant Macro %q", uentry_unparse (gc.cont.fcn));
3043       break;
3044     case CX_ITERDEF:
3045       s = message ("Iter definition %q", uentry_unparse (gc.cont.fcn));
3046       break;
3047     case CX_ITEREND:
3048       s = message ("Iter end %q", uentry_unparse (gc.cont.fcn));
3049       break;
3050     case CX_FCNDECLARATION:
3051       s = message ("Function declaration %q", uentry_unparse (gc.cont.fcn));
3052       break;
3053     default:
3054       s = message ("Un-unparseable context: %d", (int) gc.kind);
3055       break;
3056     }
3057   
3058   s = message ("%q\naccess: %q", s, context_unparseAccess ());
3059   return (s);
3060 }
3061
3062 extern ctype
3063 context_currentFunctionType (void)
3064 {
3065   if (gc.kind == CX_FUNCTION || gc.kind == CX_MACROFCN)
3066     {
3067             return (uentry_getType (gc.cont.fcn));
3068     }
3069   else if (gc.kind == CX_INNER)
3070     {
3071       llcontbuglit ("context_currentFunctionType: inner context");
3072       do { context_exitInnerPlain (); } while (gc.kind == CX_INNER);
3073       return (context_currentFunctionType ());
3074     }
3075   else
3076     {
3077       llcontbuglit ("context_currentFunctionType: not in function");
3078       return (ctype_undefined);
3079     }
3080 }
3081
3082 void
3083 context_enterInnerContext (void)
3084 {
3085   if (context_getFlag (FLG_GRAMMAR))
3086     {
3087       lldiagmsg (message ("Enter inner context: %q", context_unparse ()));
3088     }
3089
3090   if (gc.kind == CX_GLOBAL)
3091     {
3092       gc.kind = CX_INNER;
3093       gc.cont.cdepth = 1;
3094     }
3095   else if (gc.kind == CX_INNER)
3096     {
3097       gc.cont.cdepth++;
3098     }
3099   else
3100     {
3101       ;
3102     }
3103
3104   usymtab_enterScope ();
3105   pushClause (NOCLAUSE);
3106 }
3107
3108 void
3109 context_exitInnerPlain (void) /*@modifies gc;@*/
3110 {
3111   context_exitInner (exprNode_undefined);
3112 }
3113
3114 void
3115 context_exitInner (exprNode exp)
3116 {
3117    if (context_getFlag (FLG_GRAMMAR))
3118     {
3119       lldiagmsg (message ("Enter inner context: %q", context_unparse ()));
3120     }
3121  
3122   llassertprint (gc.inclause == NOCLAUSE || gc.inclause == CASECLAUSE,
3123                  ("inclause = %s", clause_nameTaken (gc.inclause)));
3124
3125   clauseStack_removeFirst (gc.clauses, NOCLAUSE);
3126   gc.inclause = topClause (gc.clauses);
3127
3128   if (gc.kind == CX_INNER)
3129     {
3130       if (--gc.cont.cdepth == 0)
3131         {
3132           gc.kind = CX_GLOBAL;
3133           gc.cont.glob = TRUE;
3134         }
3135     }
3136   else 
3137     {
3138       if (gc.kind == CX_GLOBAL)
3139         {
3140           llcontbuglit ("Attempt to exit global context");
3141           return;
3142         }
3143     }
3144
3145     usymtab_exitScope (exp);
3146 }
3147
3148
3149 void
3150 context_enterStructInnerContext (void)
3151 {
3152   if (context_getFlag (FLG_GRAMMAR))
3153     {
3154       lldiagmsg (message ("Enter struct inner context: %q", context_unparse ()));
3155     }
3156
3157   if (gc.kind == CX_GLOBAL)
3158     {
3159       gc.kind = CX_INNER;
3160       gc.cont.cdepth = 1;
3161     }
3162   else if (gc.kind == CX_INNER)
3163     {
3164       gc.cont.cdepth++;
3165     }
3166   else
3167     {
3168       ;
3169     }
3170
3171   usymtab_enterScope ();
3172
3173   if (context_getFlag (FLG_GRAMMAR))
3174     {
3175       lldiagmsg (message ("Enter struct inner context: %q", context_unparse ()));
3176     }
3177 }
3178
3179 void
3180 context_exitStructInnerContext (void)
3181 {
3182   if (context_getFlag (FLG_GRAMMAR))
3183     {
3184       lldiagmsg (message ("Exit struct inner context: %q [%d]", context_unparse (), gc.cont.cdepth));
3185     }
3186
3187   if (gc.kind == CX_INNER)
3188     {
3189       if (gc.cont.cdepth <= 0)
3190         {
3191           llcontbuglit ("Attempt to exit inner context with no depth");
3192           gc.kind = CX_GLOBAL;
3193           gc.cont.glob = TRUE;
3194           gc.cont.cdepth = 0;
3195         }
3196       else {
3197         gc.cont.cdepth--;
3198
3199         if (gc.cont.cdepth == 0)
3200           {
3201             gc.kind = CX_GLOBAL;
3202             gc.cont.glob = TRUE;
3203           }
3204       }
3205     }
3206   else 
3207     {
3208       if (gc.kind == CX_GLOBAL)
3209         {
3210           llcontbuglit ("Attempt to exit global context");
3211           return;
3212         }
3213     }
3214
3215   usymtab_exitScope (exprNode_undefined);
3216
3217   if (context_getFlag (FLG_GRAMMAR))
3218     {
3219       lldiagmsg (message ("After exit struct inner context: %q [%d]", context_unparse (), gc.cont.cdepth));
3220     }
3221 }
3222
3223 void
3224 context_exitInnerSafe (void)
3225 {
3226   if (context_getFlag (FLG_GRAMMAR))
3227     {
3228       lldiagmsg (message ("Exit inner safe: %q", context_unparse ()));
3229     }
3230
3231   if (gc.kind == CX_INNER)
3232     {
3233       if (--gc.cont.cdepth <= 0)
3234         {
3235           gc.cont.cdepth = 0;
3236         }
3237     }
3238   else if (gc.kind == CX_GLOBAL)
3239     {
3240       llcontbuglit ("Attempt to exit global context");
3241       return;
3242     }
3243   else
3244     {
3245       if (usymtab_inDeepScope ())
3246         {
3247           usymtab_exitScope (exprNode_undefined);
3248         }
3249     }
3250 }
3251
3252 static
3253 void setModuleAccess (void)
3254 {
3255   gc.facct = typeIdSet_emptySet ();
3256
3257   if (fileId_isValid (currentFile ()))
3258     {
3259       cstring baseName = fileloc_getBase (g_currentloc);
3260       
3261       if (context_getFlag (FLG_ACCESSFILE))
3262         {
3263           if (usymtab_existsType (baseName))
3264             {
3265               gc.facct = typeIdSet_insert (gc.facct, 
3266                                            usymtab_getTypeId (baseName));
3267             }
3268           else 
3269             {
3270               ;
3271             }
3272         }
3273       
3274       if (context_getFlag (FLG_ACCESSMODULE))
3275         {
3276           int i;
3277           bool hasaccess = FALSE;
3278           
3279           for (i = 0; i < gc.nmods; i++)
3280             {
3281               if (cstring_equal (baseName, gc.moduleaccess[i].file))
3282                 {
3283                   gc.facct = typeIdSet_union (gc.facct, gc.moduleaccess[i].daccess);
3284
3285                   hasaccess = TRUE;
3286                   break;
3287                 }
3288             }
3289         }
3290       
3291       gc.acct = gc.facct;
3292       gc.inheader = fileId_isHeader (currentFile ());
3293     }
3294   else
3295     {
3296       llcontbuglit ("Current file not defined\n");
3297       gc.facct = typeIdSet_emptySet ();
3298       gc.acct = gc.facct;
3299       gc.inheader = FALSE;
3300     }
3301   
3302   /* 17 Jan 1995: forgot to clear nacct */
3303   
3304   gc.nacct = typeIdSet_emptySet ();
3305 }
3306
3307 static void
3308 context_enterFileAux (void)
3309 {
3310   setModuleAccess ();
3311 }
3312
3313 void
3314 context_enterFile (void)
3315 {
3316   context_enterFileAux ();
3317   usymtab_enterFile ();
3318 }
3319
3320 void
3321 context_enterMacroFile (void)
3322 {
3323   context_enterFileAux ();
3324 }
3325
3326 bool 
3327 context_inFunction (void)
3328 {
3329   kcontext ck = gc.kind;
3330   
3331   return ((ck == CX_FUNCTION) || (ck == CX_MACROFCN) || (ck == CX_INNER));
3332 }
3333
3334 bool 
3335 context_inFunctionLike (void)
3336 {
3337   return (gc.kind == CX_FUNCTION || gc.kind == CX_MACROFCN 
3338           || gc.kind == CX_FCNDECLARATION
3339           || gc.kind == CX_UNKNOWNMACRO || gc.kind == CX_ITERDEF);
3340 }
3341
3342 bool 
3343 context_inRealFunction (void)
3344 {
3345   kcontext ck = gc.kind;
3346   
3347   return ((ck == CX_FUNCTION) || (ck == CX_MACROFCN));
3348 }
3349   
3350 void
3351 context_processAllMacros (void)
3352 {
3353   usymtab_enterFile ();
3354
3355   gc.inmacrocache = TRUE; 
3356   macrocache_processUndefinedElements (gc.mc);
3357   cleanupMessages ();  
3358   usymtab_exitFile ();
3359
3360   gc.inmacrocache = FALSE;
3361   macrocache_finalize ();
3362 }
3363
3364 /*
3365 ** this happens once at the end of each C file
3366 **
3367 ** check each Macro that was defined in current file.c or current file.h
3368 **
3369 */
3370
3371 static void
3372 context_processMacros (void)
3373 {
3374   if (fileId_isValid (currentFile ()))
3375     {
3376       fileloc lastfl;
3377       cstring cbase = fileLib_removePathFree (fileLib_removeAnyExtension (fileName (currentFile ())));
3378       
3379       gc.inmacrocache = TRUE;
3380
3381       DPRINTF (("Processing macros: %s", cbase));
3382       lastfl = macrocache_processFileElements (gc.mc, cbase);
3383       DPRINTF (("Processing macros: %s", fileloc_unparse (lastfl)));
3384
3385       cstring_free (cbase);
3386       
3387       if (fileloc_isDefined (lastfl))
3388         {
3389           g_currentloc = fileloc_update (g_currentloc, lastfl);
3390           cleanupMessages ();
3391         }
3392
3393       gc.inmacrocache = FALSE;
3394     }
3395 }
3396
3397 bool
3398 context_processingMacros (void)
3399 {
3400   return (gc.inmacrocache);
3401 }
3402
3403 void
3404 context_exitCFile (void)
3405 {
3406   if (gc.kind != CX_GLOBAL)
3407     {
3408       llfatalerrorLoc
3409         (cstring_makeLiteral ("File ended outside global scope"));
3410     }
3411
3412   if (gc.insuppressregion)
3413     {
3414      /* gack...don't reverse the order of these lines! ;-> */
3415       gc.insuppressregion = FALSE;
3416       llerrorlit (FLG_SYNTAX, 
3417                   "File ended in ignore errors region, "
3418                   "possible missing /*@end*/");
3419     }
3420
3421   /* fix up parse errors */
3422
3423   while (!usymtab_inFileScope ())
3424     {
3425       usymtab_quietExitScope (g_currentloc);
3426     }
3427
3428   /*
3429   ** Clear the file-specific modifies information.
3430   */
3431   
3432   sRefSetList_elements (gc.modrecs, mods)
3433     {
3434       sRefSet_clearStatics (mods);
3435     } end_sRefSetList_elements ;
3436   
3437   sRefSetList_clear (gc.modrecs);
3438   
3439   context_processMacros ();
3440   cleanupMessages (); 
3441   
3442   usymtab_exitFile ();
3443   
3444   gc.inDerivedFile = FALSE;
3445   filelocStack_clear (gc.locstack);
3446   
3447   gc.nacct = typeIdSet_emptySet (); /* empty noaccess */
3448   
3449   gc.cont.glob = TRUE;
3450   
3451   if (gc.savedFlags)
3452     {
3453       context_restoreFlagSettings ();
3454       gc.savedFlags = FALSE;
3455     }
3456   
3457   /*
3458     DPRINTF (("After exiting file: "));
3459     usymtab_printAll ();
3460   */
3461 }
3462
3463 void
3464 context_exitMacroCache (void)
3465 {
3466   if (gc.kind != CX_GLOBAL)
3467     {
3468       if (context_inMacro ()) 
3469         /* this is okay, file could end without newline in macro */
3470         {
3471           DPRINTF (("Still in macro: %s",
3472                     context_unparse ()));
3473           context_exitFunction ();
3474         }
3475       else
3476         {
3477           llcontbug (message ("context_exitMacroCache: outside global scope: %q", 
3478                               context_unparse ()));
3479           gc.kind = CX_GLOBAL; 
3480         }
3481     }
3482
3483   /*
3484   ** no longer valid here
3485   ** if (gc.insuppressregion)
3486   **   {
3487   **     gc.insuppressregion = FALSE;
3488   **     llerror ("File ended in ignore errors region, possible missing @");
3489   **   }
3490   */
3491
3492   gc.cont.glob = TRUE;
3493 }
3494
3495 void
3496 context_saveLocation (void)
3497 {
3498   /* was llassert (fileloc_isUndefined (gc.saveloc)) */
3499   fileloc_free (gc.saveloc);
3500   gc.saveloc = fileloc_copy (g_currentloc);
3501 }
3502
3503 fileloc
3504 context_getSaveLocation (void)
3505 {
3506   fileloc fl = gc.saveloc;
3507   gc.saveloc = fileloc_undefined;
3508   return fl;
3509 }
3510
3511 /*@observer@*/ cstring
3512 context_inFunctionName (void)
3513 {
3514   if (gc.kind == CX_FUNCTION
3515       || gc.kind == CX_MACROFCN || gc.kind == CX_UNKNOWNMACRO 
3516       || gc.kind == CX_MACROCONST 
3517       || gc.kind == CX_ITERDEF || gc.kind == CX_ITEREND)
3518     {
3519       return (uentry_rawName (gc.cont.fcn));
3520     }
3521   else
3522     {
3523       llcontbuglit ("context_inFunctionName: not in function");
3524       return (cstring_undefined);
3525     }
3526 }
3527
3528 void
3529 context_userSetFlag (flagcode f, bool b)
3530 {
3531   DPRINTF (("set flag: %s", flagcode_unparse (f)));
3532
3533   if (f == FLG_NEVERINCLUDE && b)
3534     {
3535       if (gc.flags[FLG_EXPORTHEADER])
3536         {
3537           llerror_flagWarning (cstring_makeLiteral
3538                            ("setting +neverinclude after +exportheader.  "
3539                             "Turning off exportheader, since headers are not checked "
3540                             "when +neverinclude is used."));
3541
3542           gc.flags[FLG_EXPORTHEADER] = FALSE;
3543         }
3544     }
3545   else 
3546     {
3547       if (f == FLG_EXPORTHEADER && b)
3548         {
3549           if (gc.flags[FLG_NEVERINCLUDE])
3550             {
3551               llerror_flagWarning (cstring_makeLiteral
3552                                ("setting +exportheader after +neverinclude.  "
3553                                 "Not setting exportheader, since headers are not checked "
3554                                 "when +neverinclude is used."));
3555               gc.flags[FLG_EXPORTHEADER] = FALSE;
3556               return;
3557             }
3558         }
3559     }
3560   
3561   if (context_getFlag (FLG_WARNFLAGS) && f != FLG_NOF && f != FLG_OPTF)
3562     {
3563       bool lastsetting = context_getFlag (f);
3564       
3565       if (bool_equal (lastsetting, b)
3566           && !flagcode_isSpecialFlag (f) 
3567           && !flagcode_isIdemFlag (f)
3568           && !flagcode_hasArgument (f))
3569         {
3570           llerror_flagWarning (message ("setting %s%s redundant with current value", 
3571                                     cstring_makeLiteralTemp (b ? "+" : "-"),
3572                                     flagcode_unparse (f)));
3573         }
3574     }
3575
3576   if (flagcode_isWarnUseFlag (f) && b)
3577     {
3578       if (!context_getFlag (FLG_WARNUSE))
3579         {
3580           llerror_flagWarning (message ("flag +%s is canceled by -warnuse",
3581                                     flagcode_unparse (f)));
3582           
3583         }
3584     }
3585
3586
3587   if (flagcode_isLibraryFlag (f)) 
3588     {
3589       if (gc.library != FLG_ANSILIB
3590           && gc.library != f)
3591         {
3592           llerror_flagWarning (message ("selecting library %s after library %s was "
3593                                         "selected (only one library may be used)",
3594                                         flagcode_unparse (f),
3595                                         flagcode_unparse (gc.library)));
3596         }
3597
3598       if (f == FLG_UNIXLIB)
3599         {
3600           if (context_getFlag (FLG_WARNUNIXLIB))
3601             {
3602               llerror_flagWarning (cstring_makeLiteral
3603                                    ("selecting unix library.  Unix library is "
3604                                     "ad hoc addition to POSIX library.  Recommend "
3605                                     "use +posixlib to select POSIX library instead. "
3606                                     "Use -warnunixlib to suppress this message."));
3607             }
3608         }
3609       
3610       gc.library = f;
3611     }
3612
3613   if (flagcode_isNameChecksFlag (f) && b && !context_maybeSet (FLG_NAMECHECKS))
3614     {
3615       llerror_flagWarning (message
3616                            ("setting +%s will not produce warnings with -namechecks. Must set +namechecks also.",
3617                             flagcode_unparse (f)));
3618     }
3619
3620   gc.setGlobally[f] = TRUE;
3621   context_setFlag (f, b);
3622 }
3623
3624 void
3625 context_fileSetFlag (flagcode f, ynm set)
3626 {
3627   if (!gc.savedFlags)
3628     {
3629       context_saveFlagSettings ();
3630     }
3631
3632   if (ynm_isOff (set))
3633     {
3634       context_setFlagAux (f, FALSE, TRUE, FALSE);
3635     }
3636   else if (ynm_isOn (set))
3637     {
3638       context_setFlagAux (f, TRUE, TRUE, FALSE);
3639       gc.setLocally[f] = TRUE;
3640     }
3641   else
3642     {
3643       context_restoreFlag (f);
3644     }
3645 }
3646
3647 static void
3648 context_restoreFlag (flagcode f)
3649 {
3650   
3651   if (!gc.savedFlags)
3652     {
3653       voptgenerror 
3654         (FLG_SYNTAX,
3655          message ("Attempt to restore flag %s when no file scope flags "
3656                   "have been set.",
3657                   flagcode_unparse (f)),
3658          g_currentloc);
3659     }
3660   else
3661     {
3662       context_addFlagMarker (f, MAYBE);
3663       context_setFlagAux (f, gc.saveflags[f], FALSE, TRUE);
3664     }
3665
3666   }
3667
3668 static void
3669 context_setFlag (flagcode f, bool b)
3670 {
3671   context_setFlagAux (f, b, FALSE, FALSE);
3672 }
3673
3674 void
3675 context_setFlagTemp (flagcode f, bool b)
3676 {
3677   DPRINTF (("Set flag temp: %s / %s", flagcode_unparse (f), bool_unparse (b)));
3678   gc.flags[f] = b;
3679 }
3680
3681 /*@notfunction@*/
3682 # define DOSET(ff,b) \
3683    do { if (inFile) { gc.setLocally[ff] = TRUE; \
3684                       context_addFlagMarker (ff, ynm_fromBool (b)); } \
3685         DPRINTF (("set flag: %s / %s", flagcode_unparse (ff), bool_unparse (b))); \
3686         gc.flags[ff] = b; } while (FALSE)
3687
3688 static void
3689   context_setFlagAux (flagcode f, bool b, bool 
3690                       inFile, /*@unused@*/ bool isRestore)
3691 {
3692   DPRINTF (("set flag: %s / %s", flagcode_unparse (f), bool_unparse (b)));
3693
3694   if (f == FLG_USESTDERR) 
3695     {
3696       if (b) {
3697         g_msgstream = stderr;
3698       } else {
3699         g_msgstream = stdout;
3700       }
3701     }
3702
3703   /*
3704   ** Removed test for special flags.
3705   */
3706
3707   if (flagcode_isIdemFlag (f))
3708     {
3709       DOSET (f, TRUE);
3710     }
3711   else
3712     {
3713       DOSET (f, b);
3714     }
3715
3716   if (f >= FLG_ITS4MOSTRISKY && f <= FLG_ITS4LOWRISK)
3717     {
3718       if (b) /* Turing higher level on, turns on all lower levels */
3719         {
3720           switch (f)
3721             {
3722             case FLG_ITS4MOSTRISKY:
3723               DOSET (FLG_ITS4VERYRISKY, b);
3724               /*@fallthrough@*/ 
3725             case FLG_ITS4VERYRISKY:
3726               DOSET (FLG_ITS4RISKY, b);
3727               /*@fallthrough@*/ 
3728             case FLG_ITS4RISKY:
3729               DOSET (FLG_ITS4MODERATERISK, b);
3730               /*@fallthrough@*/ 
3731             case FLG_ITS4MODERATERISK:
3732               DOSET (FLG_ITS4LOWRISK, b);
3733               /*@fallthrough@*/ 
3734             case FLG_ITS4LOWRISK:
3735               break;
3736               BADDEFAULT;
3737             }
3738         }
3739       else /* Turning level off, turns off all higher levels */
3740         {
3741           switch (f)
3742             {
3743             case FLG_ITS4LOWRISK:
3744               DOSET (FLG_ITS4MODERATERISK, b);
3745               /*@fallthrough@*/ 
3746             case FLG_ITS4MODERATERISK:
3747               DOSET (FLG_ITS4RISKY, b);
3748               /*@fallthrough@*/ 
3749             case FLG_ITS4RISKY:
3750               DOSET (FLG_ITS4VERYRISKY, b);
3751               /*@fallthrough@*/ 
3752             case FLG_ITS4VERYRISKY:
3753               DOSET (FLG_ITS4MOSTRISKY, b);
3754               /*@fallthrough@*/ 
3755             case FLG_ITS4MOSTRISKY:
3756               break;
3757               BADDEFAULT;
3758             }
3759         }
3760     }
3761   
3762   switch (f)
3763     {     
3764     case FLG_ALLEMPTY:
3765       DOSET (FLG_ALLEMPTY, b);
3766       DOSET (FLG_IFEMPTY, b);
3767       DOSET (FLG_WHILEEMPTY, b);
3768       DOSET (FLG_FOREMPTY, b);
3769       break;
3770     case FLG_PREDBOOL:
3771       DOSET (FLG_PREDBOOL, b);
3772       DOSET (FLG_PREDBOOLINT, b);
3773       DOSET (FLG_PREDBOOLPTR, b);
3774       DOSET (FLG_PREDBOOLOTHERS, b);
3775       break;
3776     case FLG_GLOBALIAS:
3777       DOSET (FLG_CHECKSTRICTGLOBALIAS, b);
3778       DOSET (FLG_CHECKEDGLOBALIAS, b);
3779       DOSET (FLG_CHECKMODGLOBALIAS, b);
3780       DOSET (FLG_UNCHECKEDGLOBALIAS, b);
3781       break;
3782     case FLG_ALLBLOCK:
3783       DOSET (FLG_ALLBLOCK, b);
3784       DOSET (FLG_IFBLOCK, b);
3785       DOSET (FLG_WHILEBLOCK, b);
3786       DOSET (FLG_FORBLOCK, b);
3787       break;
3788     case FLG_GRAMMAR:
3789       if (b)
3790         {
3791           yydebug = 1;
3792           mtdebug = 1;
3793         }
3794       else
3795         {
3796           yydebug = 0;
3797           mtdebug = 0;
3798         }
3799       
3800       DOSET (FLG_GRAMMAR, b);
3801       break;
3802     case FLG_CODEIMPONLY:
3803       DOSET (FLG_CODEIMPONLY, b);
3804       DOSET (FLG_GLOBIMPONLY, b);
3805       DOSET (FLG_RETIMPONLY, b);
3806       DOSET (FLG_STRUCTIMPONLY, b);
3807       break;
3808     case FLG_SPECALLIMPONLY:
3809       DOSET (FLG_SPECALLIMPONLY, b);
3810       DOSET (FLG_SPECGLOBIMPONLY, b);
3811       DOSET (FLG_SPECRETIMPONLY, b);
3812       DOSET (FLG_SPECSTRUCTIMPONLY, b);
3813       break;
3814     case FLG_ALLIMPONLY:
3815       DOSET (FLG_ALLIMPONLY, b);
3816       DOSET (FLG_GLOBIMPONLY, b);
3817       DOSET (FLG_RETIMPONLY, b);
3818       DOSET (FLG_STRUCTIMPONLY, b);
3819       DOSET (FLG_SPECGLOBIMPONLY, b);
3820       DOSET (FLG_SPECRETIMPONLY, b);
3821       DOSET (FLG_SPECSTRUCTIMPONLY, b);
3822       break;
3823     case FLG_ANSILIMITS: 
3824       DOSET (FLG_ANSILIMITS, b);
3825       DOSET (FLG_CONTROLNESTDEPTH, b);
3826       DOSET (FLG_STRINGLITERALLEN, b);
3827       DOSET (FLG_INCLUDENEST, b);
3828       DOSET (FLG_NUMSTRUCTFIELDS, b);
3829       DOSET (FLG_NUMENUMMEMBERS, b);
3830       
3831       if (b)
3832         {
3833           context_setValue (FLG_CONTROLNESTDEPTH, DEFAULT_CONTROLNESTDEPTH);
3834           context_setValue (FLG_STRINGLITERALLEN, DEFAULT_STRINGLITERALLEN);
3835           context_setValue (FLG_INCLUDENEST, DEFAULT_INCLUDENEST);
3836           context_setValue (FLG_NUMSTRUCTFIELDS, DEFAULT_NUMSTRUCTFIELDS);
3837           context_setValue (FLG_NUMENUMMEMBERS, DEFAULT_NUMENUMMEMBERS);
3838         }
3839       break;
3840     case FLG_EXTERNALNAMELEN:
3841       DOSET (FLG_DISTINCTEXTERNALNAMES, TRUE);
3842       DOSET (FLG_EXTERNALNAMELEN, TRUE);
3843       break;
3844     case FLG_INTERNALNAMELEN:
3845       DOSET (FLG_DISTINCTINTERNALNAMES, TRUE);
3846       DOSET (FLG_INTERNALNAMELEN, TRUE);
3847       break;
3848     case FLG_EXTERNALNAMECASEINSENSITIVE:
3849       DOSET (FLG_EXTERNALNAMECASEINSENSITIVE, b);
3850       
3851       if (b && !gc.flags[FLG_DISTINCTEXTERNALNAMES])
3852         {
3853           DOSET (FLG_DISTINCTEXTERNALNAMES, TRUE);
3854           context_setValue (FLG_EXTERNALNAMELEN, 0);
3855         }
3856       break;
3857     case FLG_INTERNALNAMECASEINSENSITIVE:
3858       DOSET (FLG_INTERNALNAMECASEINSENSITIVE, b);
3859       
3860       if (b && !gc.flags[FLG_DISTINCTINTERNALNAMES])
3861         {
3862           DOSET (FLG_DISTINCTINTERNALNAMES, TRUE);
3863           context_setValue (FLG_INTERNALNAMELEN, 0);
3864         }
3865       break;
3866     case FLG_INTERNALNAMELOOKALIKE:
3867       DOSET (FLG_INTERNALNAMELOOKALIKE, b);
3868       
3869       if (b && !gc.flags[FLG_DISTINCTINTERNALNAMES])
3870         {
3871           DOSET (FLG_DISTINCTINTERNALNAMES, TRUE);
3872           context_setValue (FLG_INTERNALNAMELEN, 0);
3873         }
3874       break;
3875     case FLG_MODUNSPEC:
3876       DOSET (FLG_MODNOMODS, b);
3877       DOSET (FLG_MODGLOBSUNSPEC, b);
3878       DOSET (FLG_MODSTRICTGLOBSUNSPEC, b);
3879       break;
3880     case FLG_EXPORTANY: 
3881       DOSET (FLG_EXPORTVAR, b);
3882       DOSET (FLG_EXPORTFCN, b);
3883       DOSET (FLG_EXPORTTYPE, b);
3884       DOSET (FLG_EXPORTMACRO, b);
3885       DOSET (FLG_EXPORTCONST, b);
3886       gc.anyExports = TRUE;
3887       break;
3888     case FLG_REPEXPOSE:
3889       DOSET (FLG_RETEXPOSE, b); 
3890       DOSET (FLG_ASSIGNEXPOSE, b); 
3891       DOSET (FLG_CASTEXPOSE, b); 
3892       break;
3893     case FLG_RETVAL:
3894       DOSET (FLG_RETVALBOOL, b);
3895       DOSET (FLG_RETVALINT, b);
3896       DOSET (FLG_RETVALOTHER, b);
3897       break;
3898     case FLG_PARTIAL:
3899       if (b)
3900         {
3901           DOSET (FLG_EXPORTLOCAL, FALSE);
3902           DOSET (FLG_DECLUNDEF, FALSE);
3903           DOSET (FLG_SPECUNDEF, FALSE);
3904           DOSET (FLG_TOPUNUSED, FALSE);
3905         }
3906       break;
3907     case FLG_DEEPBREAK:
3908       DOSET (FLG_LOOPLOOPBREAK, b);
3909       DOSET (FLG_LOOPSWITCHBREAK, b);
3910       DOSET (FLG_SWITCHLOOPBREAK, b);
3911       DOSET (FLG_SWITCHSWITCHBREAK, b);
3912       DOSET (FLG_LOOPLOOPCONTINUE, b);
3913       DOSET (FLG_DEEPBREAK, b);
3914       break;
3915     case FLG_ACCESSALL:
3916       DOSET (FLG_ACCESSMODULE, b);
3917       DOSET (FLG_ACCESSFILE, b);
3918       DOSET (FLG_ACCESSCZECH, b);
3919       break;
3920     case FLG_ALLMACROS:
3921       DOSET (FLG_ALLMACROS, b);
3922       DOSET (FLG_FCNMACROS, b);
3923       DOSET (FLG_CONSTMACROS, b);
3924       break;
3925     case FLG_CZECH:
3926       if (b) { DOSET (FLG_ACCESSCZECH, b); }
3927       DOSET (FLG_CZECHFUNCTIONS, b);
3928       DOSET (FLG_CZECHVARS, b);
3929       DOSET (FLG_CZECHCONSTANTS, b);
3930       DOSET (FLG_CZECHTYPES, b);
3931       break;
3932     case FLG_SLOVAK:
3933       if (b) { DOSET (FLG_ACCESSSLOVAK, b); }
3934       DOSET (FLG_SLOVAKFUNCTIONS, b);
3935       DOSET (FLG_SLOVAKVARS, b);
3936       DOSET (FLG_SLOVAKCONSTANTS, b);
3937       DOSET (FLG_SLOVAKTYPES, b);
3938       break;
3939     case FLG_CZECHOSLOVAK:
3940       if (b) { DOSET (FLG_ACCESSCZECHOSLOVAK, b); }
3941       DOSET (FLG_CZECHOSLOVAKFUNCTIONS, b);
3942       DOSET (FLG_CZECHOSLOVAKVARS, b);
3943       DOSET (FLG_CZECHOSLOVAKCONSTANTS, b);
3944       DOSET (FLG_CZECHOSLOVAKTYPES, b);
3945       break;
3946     case FLG_NULL:
3947       DOSET (FLG_NULLSTATE, b);
3948       DOSET (FLG_NULLDEREF, b);
3949       DOSET (FLG_NULLASSIGN, b);
3950       DOSET (FLG_NULLPASS, b);
3951       DOSET (FLG_NULLRET, b);
3952       break;
3953     case FLG_MEMCHECKS:
3954       DOSET (FLG_NULLSTATE, b);
3955       DOSET (FLG_NULLDEREF, b);
3956       DOSET (FLG_NULLASSIGN, b);
3957       DOSET (FLG_NULLPASS, b);
3958       DOSET (FLG_NULLRET, b);
3959       DOSET (FLG_COMPDEF, b);
3960       DOSET (FLG_COMPMEMPASS, b);
3961       DOSET (FLG_UNIONDEF, b);
3962       DOSET (FLG_MEMTRANS, b);
3963       DOSET (FLG_USERELEASED, b);
3964       DOSET (FLG_ALIASUNIQUE, b);
3965       DOSET (FLG_MAYALIASUNIQUE, b);
3966       DOSET (FLG_MUSTFREE, b);
3967       DOSET (FLG_MUSTDEFINE, b);
3968       DOSET (FLG_GLOBSTATE, b); 
3969       DOSET (FLG_COMPDESTROY, b);
3970       DOSET (FLG_MUSTNOTALIAS, b);
3971       DOSET (FLG_MEMIMPLICIT, b);
3972       DOSET (FLG_BRANCHSTATE, b); 
3973       /*@fallthrough@*/ /* also sets memtrans flags */
3974     case FLG_MEMTRANS:
3975       DOSET (FLG_MEMTRANS, b);
3976       DOSET (FLG_EXPOSETRANS, b);
3977       DOSET (FLG_OBSERVERTRANS, b);
3978       DOSET (FLG_DEPENDENTTRANS, b);
3979       DOSET (FLG_NEWREFTRANS, b);
3980       DOSET (FLG_ONLYTRANS, b);
3981       DOSET (FLG_OWNEDTRANS, b);
3982       DOSET (FLG_FRESHTRANS, b);
3983       DOSET (FLG_SHAREDTRANS, b);
3984       DOSET (FLG_TEMPTRANS, b);
3985       DOSET (FLG_KEPTTRANS, b);
3986       DOSET (FLG_REFCOUNTTRANS, b);
3987       DOSET (FLG_STATICTRANS, b);
3988       DOSET (FLG_UNKNOWNTRANS, b);
3989       DOSET (FLG_KEEPTRANS, b);
3990       DOSET (FLG_IMMEDIATETRANS, b);
3991       break;
3992       
3993     default:
3994       break;
3995     }
3996
3997   if (b && !gc.anyExports
3998       && (f == FLG_EXPORTVAR || f == FLG_EXPORTFCN
3999           || f == FLG_EXPORTTYPE || f == FLG_EXPORTMACRO
4000           || f == FLG_EXPORTCONST
4001           || f == FLG_EXPORTANY))
4002     {
4003       gc.anyExports = TRUE;
4004     }
4005 }
4006
4007 bool 
4008 context_maybeSet (flagcode d)
4009 {
4010   return (gc.flags[d] || gc.setLocally[d]);
4011 }
4012
4013 bool
4014 context_getFlag (flagcode d)
4015 {
4016   return (gc.flags[d]);
4017 }
4018
4019 bool
4020 context_flagOn (flagcode f, fileloc loc)
4021 {
4022   return (!context_suppressFlagMsg (f, loc));
4023 }
4024
4025 static void context_saveFlagSettings (void)
4026 {
4027   gc.savedFlags = TRUE;
4028   llassert (sizeof (gc.saveflags) == sizeof (gc.flags));
4029   memcpy (gc.saveflags, gc.flags, sizeof (gc.flags));
4030 }
4031
4032 static void context_restoreFlagSettings (void)
4033 {
4034   llassert (sizeof (gc.saveflags) == sizeof (gc.flags));
4035   memcpy (gc.flags, gc.saveflags, sizeof (gc.flags));
4036   gc.savedFlags = FALSE;
4037 }
4038
4039 void context_setFilename (fileId fid, int lineno) 
4040    /*@globals fileloc g_currentloc;@*/
4041    /*@modifies g_currentloc@*/
4042 {
4043   if (fileId_baseEqual (currentFile (), fid))
4044     {
4045       setLine (lineno);
4046       return;
4047     }
4048   else
4049     {
4050       fileloc_setColumn (g_currentloc, 0);
4051
4052       if (fileloc_isSpecialFile (g_currentloc))
4053         {
4054           gc.inDerivedFile = TRUE;
4055         }
4056
4057       if (filelocStack_popPushFile (gc.locstack, g_currentloc))
4058         {
4059           int maxdepth = context_getValue (FLG_INCLUDENEST);
4060
4061           if (filelocStack_size (gc.locstack) > maxdepth)
4062             {
4063               int depth = filelocStack_includeDepth (gc.locstack);
4064               
4065               if (depth > maxdepth)
4066                 {
4067                   if (optgenerror 
4068                       (FLG_INCLUDENEST,
4069                        message ("Maximum include nesting depth "
4070                                 "(%d, current depth %d) exceeded",
4071                                 maxdepth,
4072                                 depth),
4073                        filelocStack_nextTop (gc.locstack)))
4074                     {
4075                       filelocStack_printIncludes (gc.locstack);
4076                     }
4077                 }
4078             }
4079         }
4080       
4081       g_currentloc = fileloc_create (fid, lineno, 1);
4082       gc.inheader = fileId_isHeader (currentFile ());
4083
4084       context_enterFileAux ();
4085     }
4086 }
4087
4088 void context_enterIterDef (/*@observer@*/ uentry le)
4089 {
4090   context_enterMacro (le);
4091   gc.acct = typeIdSet_subtract (gc.facct, gc.nacct);
4092   gc.kind = CX_ITERDEF;
4093 }
4094
4095 void context_enterIterEnd (/*@observer@*/ uentry le)
4096 {
4097   context_enterMacro (le);
4098   gc.kind = CX_ITEREND;
4099 }
4100
4101 void 
4102 context_destroyMod (void) 
4103    /*@globals killed gc@*/
4104 {
4105   setCodePoint ();
4106   ctype_destroyMod ();
4107   setCodePoint ();
4108   usymtab_free ();
4109   setCodePoint ();
4110   fileTable_free (gc.ftab);
4111   filelocStack_free (gc.locstack);
4112   setCodePoint ();
4113   gc.ftab = fileTable_undefined;
4114
4115   macrocache_free (gc.mc);
4116   sfree (gc.moduleaccess);
4117   setCodePoint ();
4118
4119   fileloc_free (gc.saveloc); gc.saveloc = fileloc_undefined;
4120   fileloc_free (gc.pushloc); gc.pushloc = fileloc_undefined;
4121
4122   setCodePoint ();
4123   sRefSetList_free (gc.modrecs);
4124   setCodePoint ();
4125   flagMarkerList_free (gc.markers); 
4126   setCodePoint ();
4127   messageLog_free (gc.msgLog);
4128   setCodePoint ();
4129   clauseStack_free (gc.clauses);
4130   setCodePoint ();
4131   
4132   cstring_free (gc.msgAnnote);
4133   globSet_free (gc.globs_used);
4134   metaStateTable_free (gc.stateTable);
4135   annotationTable_free (gc.annotTable);
4136 }
4137
4138 /*
4139 ** Flag shortcuts.
4140 */
4141
4142 bool context_msgBoolInt (void)
4143 {
4144   return gc.flags [FLG_BOOLINT];
4145 }
4146
4147 bool context_msgCharInt (void)
4148 {
4149   return gc.flags [FLG_CHARINT];
4150 }
4151
4152 bool context_msgEnumInt (void)
4153 {
4154   return gc.flags [FLG_ENUMINT];
4155 }
4156
4157 bool context_msgPointerArith (void) 
4158 {
4159   return gc.flags [FLG_POINTERARITH];
4160 }
4161
4162 bool context_msgStrictOps (void) 
4163 {
4164   return gc.flags [FLG_STRICTOPS];
4165 }
4166
4167 # ifndef NOLCL
4168 bool context_msgLh (void)           
4169 {
4170   return gc.flags [FLG_DOLH];
4171 }
4172 # endif
4173
4174 void context_pushLoc (void) 
4175 {
4176   fileloc_free (gc.pushloc);
4177   gc.pushloc = gc.saveloc;
4178   gc.saveloc = fileloc_undefined;
4179 }
4180
4181 void context_popLoc (void) 
4182 {
4183   gc.saveloc = fileloc_update (gc.saveloc, gc.pushloc);
4184 }
4185
4186 bool context_inGlobalScope (void)
4187 {
4188   return (usymtab_inFileScope() || usymtab_inGlobalScope ());
4189 }
4190
4191 bool context_inInnerScope (void)
4192 {
4193   return (gc.kind == CX_INNER);
4194 }
4195
4196 void context_setProtectVars (void)
4197 {
4198   gc.protectVars = TRUE;
4199 }
4200
4201 bool context_anyErrors (void)
4202 {
4203   return (gc.numerrors > 0);
4204 }
4205
4206 void context_hasError (void)
4207 {
4208   gc.numerrors++;
4209   DPRINTF (("num errors: %d", gc.numerrors));
4210 }
4211
4212 int context_numErrors (void)
4213 {
4214   return gc.numerrors;
4215 }
4216
4217 bool context_neednl (void)
4218 {
4219   return gc.neednl;
4220 }
4221
4222 void context_setNeednl (void)
4223 {
4224   gc.neednl = TRUE;
4225 }
4226
4227 int context_getExpect (void)
4228 {
4229   return (context_getValue (FLG_EXPECT));
4230 }
4231
4232 # ifndef NOLCL
4233 int context_getLCLExpect (void)
4234 {
4235   return (context_getValue (FLG_LCLEXPECT));
4236 }
4237 # endif
4238
4239 int context_getLimit (void)
4240 {
4241   return (context_getValue (FLG_LIMIT));
4242 }
4243
4244 bool context_unlimitedMessages (void)
4245 {
4246   return (context_getLimit () < 0);
4247 }
4248
4249 void context_releaseVars (void)
4250 {
4251   llassert (gc.protectVars);
4252   gc.protectVars = FALSE;
4253 }
4254
4255 void context_sizeofReleaseVars (void)
4256 {
4257   /* If there is a nested sizeof, this might not hold:
4258      llassert (gc.protectVars);
4259      */
4260
4261   gc.protectVars = FALSE;
4262 }
4263
4264 bool context_inProtectVars (void)
4265 {
4266   return (gc.protectVars);
4267 }
4268
4269 void context_hideShowscan (void) 
4270 {
4271   gc.flags[FLG_SHOWSCAN] = FALSE;
4272 }
4273
4274 void context_unhideShowscan (void)
4275 {
4276   gc.flags[FLG_SHOWSCAN] = TRUE;
4277 }
4278
4279 bool context_inHeader (void)
4280 {
4281   return (gc.inheader);
4282 }
4283
4284 fileTable context_fileTable (void)
4285 {
4286   return gc.ftab;
4287 }
4288
4289 cstring context_tmpdir (void)
4290 {
4291   return (context_getString (FLG_TMPDIR));
4292 }
4293
4294 messageLog context_messageLog (void)
4295 {
4296   return gc.msgLog;
4297 }
4298
4299 bool context_inMacroFunction (void)
4300 {
4301   return (gc.kind == CX_MACROFCN);
4302 }
4303
4304 bool context_inMacroConstant (void)
4305 {   
4306   return (gc.kind == CX_MACROCONST);
4307 }
4308
4309 bool context_inUnknownMacro (void)
4310 {   
4311   return (gc.kind == CX_UNKNOWNMACRO);
4312 }
4313
4314 void context_setShownFunction (void)
4315 {
4316   gc.showfunction = FALSE;
4317 }
4318
4319 bool context_doDump (void)
4320 {   
4321   return cstring_isNonEmpty (context_getString (FLG_DUMP));
4322 }
4323
4324 bool context_doMerge (void)
4325 {
4326   return cstring_isNonEmpty (context_getString (FLG_MERGE));
4327 }
4328
4329 cstring context_getDump (void)
4330 {           
4331   return context_getString (FLG_DUMP);
4332 }
4333
4334 cstring context_getMerge (void)
4335 {
4336   return context_getString (FLG_MERGE);
4337 }
4338
4339 # ifndef NOLCL
4340 bool context_inLCLLib (void)
4341 {   
4342   return (gc.kind == CX_LCLLIB);
4343 }
4344
4345 bool context_inImport (void)
4346 {
4347   return (gc.inimport);
4348 }
4349
4350 void context_enterImport (void)
4351
4352   gc.inimport = TRUE;
4353 }
4354
4355 void context_leaveImport (void)
4356
4357   gc.inimport = FALSE;
4358 }
4359 # endif
4360
4361 bool context_inMacro (void) 
4362 {
4363   return (gc.kind == CX_MACROFCN || gc.kind == CX_MACROCONST 
4364           || gc.kind == CX_UNKNOWNMACRO
4365           || gc.kind == CX_ITERDEF || gc.kind == CX_ITEREND);
4366 }
4367
4368 bool context_inIterDef (void)
4369 {
4370   return (gc.kind == CX_ITERDEF);
4371 }
4372
4373 bool context_inIterEnd (void)
4374 {
4375   return (gc.kind == CX_ITEREND);
4376 }
4377
4378 int context_getLinesProcessed (void)    
4379 {
4380   return (gc.linesprocessed);
4381 }
4382
4383 int context_getSpecLinesProcessed (void)    
4384 {
4385   return (gc.speclinesprocessed);
4386 }
4387
4388 # ifndef NOLCL
4389 void context_processedSpecLine (void)
4390 {
4391   gc.speclinesprocessed++;
4392 }
4393
4394 void context_resetSpecLines (void)    
4395 {
4396   gc.speclinesprocessed = 0;
4397 }
4398 # endif
4399
4400 bool context_inGlobalContext (void)
4401 {
4402   return (gc.kind == CX_GLOBAL);
4403 }
4404
4405 static void context_quietExitScopes (void)
4406 {
4407   /*
4408   ** Try to restore the global scope (after an error).
4409   */
4410
4411   while (!usymtab_inFileScope ())
4412     {
4413       usymtab_quietExitScope (g_currentloc);
4414     }
4415
4416   gc.cont.glob = TRUE;
4417   gc.kind = CX_GLOBAL;
4418 }
4419
4420 void context_checkGlobalScope (void)
4421 {
4422   if (gc.kind != CX_GLOBAL)
4423     {
4424       llcontbug (message ("Not in global scope as expected: %q", context_unparse ()));
4425       context_quietExitScopes ();
4426     }
4427 }
4428
4429 void context_setFileId (fileId s)
4430 {
4431   g_currentloc = fileloc_updateFileId (g_currentloc, s); 
4432 }
4433
4434 bool context_setBoolName (void)
4435 {
4436   return (!cstring_equalLit (context_getString (FLG_BOOLTYPE),
4437                              DEFAULT_BOOLTYPE));
4438 }
4439
4440 cstring context_printBoolName (void)
4441 {
4442   if (context_setBoolName ()) 
4443     {
4444       return context_getBoolName ();
4445     }
4446   else
4447     {
4448       return cstring_makeLiteralTemp ("boolean");
4449     }
4450 }
4451
4452 cstring context_getBoolName (void)
4453 {
4454   return (context_getString (FLG_BOOLTYPE));
4455 }
4456
4457 cstring context_getFalseName (void)
4458 {
4459   return (context_getString (FLG_BOOLFALSE));
4460 }
4461
4462 cstring context_getTrueName (void)
4463 {
4464   return (context_getString (FLG_BOOLTRUE));
4465 }
4466
4467 cstring context_getLarchPath (void)
4468 {
4469   return (context_getString (FLG_LARCHPATH));
4470 }
4471
4472 cstring context_getLCLImportDir (void)
4473 {
4474   return (context_getString (FLG_LCLIMPORTDIR));
4475 }
4476
4477 static void context_setJustPopped (void)
4478 {
4479   gc.justpopped = TRUE;
4480 }
4481
4482 void context_clearJustPopped (void)
4483 {
4484   gc.justpopped = FALSE;
4485 }
4486
4487 bool context_justPopped (void)
4488 {
4489   return (gc.justpopped);
4490 }
4491
4492 void context_setMacroMissingParams (void)
4493 {
4494   gc.macroMissingParams = TRUE;
4495 }
4496
4497 void context_resetMacroMissingParams (void)
4498 {
4499   gc.macroMissingParams = FALSE;
4500 }
4501
4502 bool context_isMacroMissingParams (void)
4503 {
4504   return (gc.macroMissingParams);
4505 }
4506
4507 void context_showFilelocStack (void) 
4508 {
4509   filelocStack_printIncludes (gc.locstack);
4510 }
4511
4512 metaStateTable context_getMetaStateTable (void) 
4513 {
4514   return gc.stateTable;
4515 }
4516
4517 metaStateInfo context_lookupMetaStateInfo (cstring key)
4518 {
4519   return metaStateTable_lookup (gc.stateTable, key);
4520 }
4521
4522 /*@null@*/ annotationInfo context_lookupAnnotation (cstring annot) 
4523 {
4524   annotationInfo ainfo;
4525
4526   ainfo = annotationTable_lookup (gc.annotTable, annot);
4527
4528   return ainfo;
4529 }
4530
4531 void context_addAnnotation (annotationInfo ainfo)
4532 {
4533   if (annotationTable_contains (gc.annotTable, annotationInfo_getName (ainfo)))
4534     {
4535       voptgenerror 
4536         (FLG_SYNTAX,
4537          message ("Duplicate annotation declaration: %s", annotationInfo_getName (ainfo)),
4538          annotationInfo_getLoc (ainfo));
4539
4540       annotationInfo_free (ainfo);
4541     }
4542   else
4543     {
4544       annotationTable_insert (gc.annotTable, ainfo);
4545     }
4546 }
4547
4548 void context_addMetaState (cstring mname, metaStateInfo msinfo)
4549 {
4550   if (metaStateTable_contains (gc.stateTable, mname))
4551     {
4552       voptgenerror 
4553         (FLG_SYNTAX,
4554          message ("Duplicate metastate declaration: %s", mname),
4555          metaStateInfo_getLoc (msinfo));
4556       cstring_free (mname);
4557       metaStateInfo_free (msinfo);
4558     }
4559   else
4560     {
4561       DPRINTF (("Adding meta state: %s", mname));
4562       metaStateTable_insert (gc.stateTable, mname, msinfo); 
4563     }
4564 }
4565
4566 valueTable context_createValueTable (sRef s, stateInfo sinfo)
4567 {
4568   if (metaStateTable_size (gc.stateTable) > 0)
4569     {
4570       valueTable res = valueTable_create (metaStateTable_size (gc.stateTable));
4571       /*@i32 should use smaller value... */
4572       DPRINTF (("Value table for: %s", sRef_unparse (s)));
4573       
4574       metaStateTable_elements (gc.stateTable, msname, msi)
4575         {
4576           mtContextNode context = metaStateInfo_getContext (msi);
4577
4578           if (mtContextNode_matchesRefStrict (context, s))
4579             {
4580               DPRINTF (("Create: %s", metaStateInfo_unparse (msi)));
4581               llassert (cstring_equal (msname, metaStateInfo_getName (msi)));
4582               
4583               valueTable_insert 
4584                 (res,
4585                  cstring_copy (metaStateInfo_getName (msi)),
4586                  stateValue_createImplicit (metaStateInfo_getDefaultValue (msi, s), 
4587                                             stateInfo_copy (sinfo)));
4588             }
4589           else
4590             {
4591               DPRINTF (("No match: %s", metaStateInfo_unparse (msi)));
4592             }
4593         } 
4594       end_metaStateTable_elements ;
4595       
4596       stateInfo_free (sinfo);
4597       DPRINTF (("Value table: %s", valueTable_unparse (res)));
4598       return res;
4599     }
4600   else
4601     {
4602       stateInfo_free (sinfo);
4603       return valueTable_undefined;
4604     }
4605 }
4606
4607 valueTable context_createGlobalMarkerValueTable (stateInfo sinfo)
4608 {
4609   if (metaStateTable_size (gc.stateTable) > 0)
4610     {
4611       valueTable res = valueTable_create (metaStateTable_size (gc.stateTable));
4612       /*@i32 should use smaller value... */
4613       
4614       metaStateTable_elements (gc.stateTable, msname, msi)
4615         {
4616           /*@i23 only add global...*/
4617           DPRINTF (("Create: %s", metaStateInfo_unparse (msi)));
4618           llassert (cstring_equal (msname, metaStateInfo_getName (msi)));
4619           
4620           valueTable_insert (res,
4621                              cstring_copy (metaStateInfo_getName (msi)),
4622                              stateValue_create (metaStateInfo_getDefaultGlobalValue (msi),
4623                                                 stateInfo_copy (sinfo)));
4624         } 
4625       end_metaStateTable_elements ;
4626       
4627       stateInfo_free (sinfo);
4628       DPRINTF (("Value table: %s", valueTable_unparse (res)));
4629       return res;
4630     }
4631   else
4632     {
4633       stateInfo_free (sinfo);
4634       return valueTable_undefined;
4635     }
4636 }
4637
4638
4639
This page took 2.413783 seconds and 5 git commands to generate.