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