1 {
2     Copyright (c) 2012 by the FPC development team
3 
4     Contains functionality to save/restore the global compiler state when
5     switching between the compilation of different units.
6 
7     This program is free software; you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation; either version 2 of the License, or
10     (at your option) any later version.
11 
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16 
17     You should have received a copy of the GNU General Public License
18     along with this program; if not, write to the Free Software
19     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21  ****************************************************************************
22 }
23 unit globstat;
24 
25 {$i fpcdefs.inc}
26 
27 interface
28 
29 uses
30   globtype,tokens,globals,
31   aasmdata,
32   dbgbase,
33   symbase,symsym,
34   fmodule,
35   scanner,scandir,
36   procinfo;
37 
38 
39 type
40   pglobalstate=^tglobalstate;
41   tglobalstate=record
42   { scanner }
43     oldidtoken,
44     oldtoken       : ttoken;
45     oldtokenpos    : tfileposinfo;
46     oldc           : char;
47     oldpattern,
48     oldorgpattern  : string;
49     old_block_type : tblock_type;
50   { symtable }
51     oldsymtablestack,
52     oldmacrosymtablestack : TSymtablestack;
53     oldaktprocsym    : tprocsym;
54   { cg }
55     oldparse_only  : boolean;
56   { akt.. things }
57     oldcurrent_filepos      : tfileposinfo;
58     old_current_module : tmodule;
59     oldcurrent_procinfo : tprocinfo;
60     old_settings : tsettings;
61     old_switchesstatestack : tswitchesstatestack;
62     old_switchesstatestackpos : Integer;
63     old_verbosity : longint;
64 
65   { only saved/restored if "full" is true }
66     old_asmdata : tasmdata;
67     old_debuginfo : tdebuginfo;
68     old_scanner : tscannerfile;
69     old_parser_file : string;
70   end;
71 
72 procedure save_global_state(out state:tglobalstate;full:boolean);
73 procedure restore_global_state(const state:tglobalstate;full:boolean);
74 
75 implementation
76 
77 uses
78   pbase,comphook;
79 
80   procedure save_global_state(out state:tglobalstate;full:boolean);
81     begin
82       with state do
83         begin
84           old_current_module:=current_module;
85 
86           { save symtable state }
87           oldsymtablestack:=symtablestack;
88           oldmacrosymtablestack:=macrosymtablestack;
89           oldcurrent_procinfo:=current_procinfo;
90 
91           { save scanner state }
92           oldc:=c;
93           oldpattern:=pattern;
94           oldorgpattern:=orgpattern;
95           oldtoken:=token;
96           oldidtoken:=idtoken;
97           old_block_type:=block_type;
98           oldtokenpos:=current_tokenpos;
99           old_switchesstatestack:=switchesstatestack;
100           old_switchesstatestackpos:=switchesstatestackpos;
101 
102           { save cg }
103           oldparse_only:=parse_only;
104 
105           { save akt... state }
106           { handle the postponed case first }
107           //flushpendingswitchesstate;
108           oldcurrent_filepos:=current_filepos;
109           old_settings:=current_settings;
110           old_verbosity:=status.verbosity;
111 
112           if full then
113             begin
114               old_asmdata:=current_asmdata;
115               old_debuginfo:=current_debuginfo;
116               old_parser_file:=parser_current_file;
117               old_scanner:=current_scanner;
118             end;
119         end;
120     end;
121 
122 
123   procedure restore_global_state(const state:tglobalstate;full:boolean);
124     begin
125       with state do
126         begin
127           { restore scanner }
128           c:=oldc;
129           pattern:=oldpattern;
130           orgpattern:=oldorgpattern;
131           token:=oldtoken;
132           idtoken:=oldidtoken;
133           current_tokenpos:=oldtokenpos;
134           block_type:=old_block_type;
135           switchesstatestack:=old_switchesstatestack;
136           switchesstatestackpos:=old_switchesstatestackpos;
137 
138           { restore cg }
139           parse_only:=oldparse_only;
140 
141           { restore symtable state }
142           symtablestack:=oldsymtablestack;
143           macrosymtablestack:=oldmacrosymtablestack;
144           current_procinfo:=oldcurrent_procinfo;
145           current_filepos:=oldcurrent_filepos;
146           current_settings:=old_settings;
147           status.verbosity:=old_verbosity;
148 
149           if full then
150             begin
151               current_module:=old_current_module; {!}
152               current_asmdata:=old_asmdata;
153               current_debuginfo:=old_debuginfo;
154               current_scanner:=old_scanner;
155               parser_current_file:=old_parser_file;
156             end;
157         end;
158     end;
159 
160 end.
161 
162