Debian Codegraph @

gcc/m2/gm2-compiler/M2StateCheck.mod

raw
1 (* M2StateCheck.mod provide state check tracking for declarations.
2
3 Copyright (C) 2024-2025 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <[email protected]>.
5
6 This file is part of GNU Modula-2.
7
8 GNU Modula-2 is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
12
13 GNU Modula-2 is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GNU Modula-2; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. *)
21
22 IMPLEMENTATION MODULE M2StateCheck ;
23
24 FROM Storage IMPORT ALLOCATE ;
25 FROM M2MetaError IMPORT MetaErrorStringT1 ;
26 FROM DynamicStrings IMPORT String, InitString, ConCat, Mark ;
27 FROM SymbolTable IMPORT NulSym, IsType, IsVar, IsConst ;
28
29
30 TYPE
31 StateCheck = POINTER TO RECORD
32 state: StateSet ;
33 stack,
34 next : StateCheck ;
35 END ;
36
37 State = (const, var, type, constfunc, varparam, constructor) ;
38
39 StateSet = SET OF State ;
40
41 VAR
42 FreeList: StateCheck ;
43
44
45 (*
46 InitState - returns a new initialized StateCheck.
47 *)
48
49 PROCEDURE InitState () : StateCheck ;
50 VAR
51 s: StateCheck ;
52 BEGIN
53 s := New () ;
54 WITH s^ DO
55 state := StateSet {} ;
56 stack := NIL ;
57 next := NIL
58 END ;
59 RETURN s
60 END InitState ;
61
62
63 (*
64 New - returns an uninitialized StateCheck.
65 *)
66
67 PROCEDURE New () : StateCheck ;
68 VAR
69 s: StateCheck ;
70 BEGIN
71 IF FreeList = NIL
72 THEN
73 NEW (s)
74 ELSE
75 s := FreeList ;
76 FreeList := FreeList^.next
77 END ;
78 RETURN s
79 END New ;
80
81
82 (*
83 PushState - duplicates the StateCheck s and chains the new copy to s.
84 Return the copy.
85 *)
86
87 PROCEDURE PushState (VAR s: StateCheck) ;
88 VAR
89 copy: StateCheck ;
90 BEGIN
91 copy := InitState () ;
92 copy^.state := s^.state ;
93 copy^.stack := s ;
94 s := copy
95 END PushState ;
96
97
98 (*
99 KillState - destructor for StateCheck.
100 *)
101
102 PROCEDURE KillState (VAR s: StateCheck) ;
103 VAR
104 t: StateCheck ;
105 BEGIN
106 WHILE s^.stack # NIL DO
107 t := s^.stack ;
108 s^.stack := t^.stack ;
109 Dispose (t)
110 END ;
111 Dispose (s)
112 END KillState ;
113
114
115 (*
116 Dispose - place s onto the FreeList and set s to NIL.
117 *)
118
119 PROCEDURE Dispose (VAR s: StateCheck) ;
120 BEGIN
121 s^.next := FreeList ;
122 FreeList := s
123 END Dispose ;
124
125
126 (*
127 InclVar - s := s + {var}.
128 *)
129
130 PROCEDURE InclVar (s: StateCheck) ;
131 BEGIN
132 INCL (s^.state, var)
133 END InclVar ;
134
135
136 (*
137 InclConst - s := s + {const}.
138 *)
139
140 PROCEDURE InclConst (s: StateCheck) ;
141 BEGIN
142 INCL (s^.state, const)
143 END InclConst ;
144
145
146 (*
147 InclType - s := s + {type}.
148 *)
149
150 PROCEDURE InclType (s: StateCheck) ;
151 BEGIN
152 INCL (s^.state, type)
153 END InclType ;
154
155
156 (*
157 InclConstFunc - s := s + {constfunc}.
158 *)
159
160 PROCEDURE InclConstFunc (s: StateCheck) ;
161 BEGIN
162 INCL (s^.state, constfunc)
163 END InclConstFunc ;
164
165
166 (*
167 InclVarParam - s := s + {varparam}.
168 *)
169
170 PROCEDURE InclVarParam (s: StateCheck) ;
171 BEGIN
172 INCL (s^.state, varparam)
173 END InclVarParam ;
174
175
176 (*
177 InclConstructor - s := s + {constructor}.
178 *)
179
180 PROCEDURE InclConstructor (s: StateCheck) ;
181 BEGIN
182 INCL (s^.state, constructor)
183 END InclConstructor ;
184
185
186 (*
187 ExclVar - s := s - {var}.
188 *)
189
190 PROCEDURE ExclVar (s: StateCheck) ;
191 BEGIN
192 EXCL (s^.state, var)
193 END ExclVar ;
194
195
196 (*
197 ExclConst - s := s - {const}.
198 *)
199
200 PROCEDURE ExclConst (s: StateCheck) ;
201 BEGIN
202 EXCL (s^.state, const)
203 END ExclConst ;
204
205
206 (*
207 ExclType - s := s - {type}.
208 *)
209
210 PROCEDURE ExclType (s: StateCheck) ;
211 BEGIN
212 EXCL (s^.state, type)
213 END ExclType ;
214
215
216 (*
217 ExclConstFunc - s := s - {constfunc}.
218 *)
219
220 PROCEDURE ExclConstFunc (s: StateCheck) ;
221 BEGIN
222 EXCL (s^.state, constfunc)
223 END ExclConstFunc ;
224
225
226 (*
227 ExclVarParam - s := s - {varparam}.
228 *)
229
230 PROCEDURE ExclVarParam (s: StateCheck) ;
231 BEGIN
232 EXCL (s^.state, varparam)
233 END ExclVarParam ;
234
235
236 (*
237 ExclConstructor - s := s - {varparam}.
238 *)
239
240 PROCEDURE ExclConstructor (s: StateCheck) ;
241 BEGIN
242 EXCL (s^.state, constructor)
243 END ExclConstructor ;
244
245
246 (*
247 PopState - pops the current state.
248 *)
249
250 PROCEDURE PopState (VAR s: StateCheck) ;
251 VAR
252 t: StateCheck ;
253 BEGIN
254 t := s ;
255 s := s^.stack ;
256 t^.stack := NIL ;
257 Dispose (t)
258 END PopState ;
259
260
261 (*
262 CheckQualident - checks to see that qualident sym is allowed in the state s.
263 *)
264
265 PROCEDURE CheckQualident (tok: CARDINAL; s: StateCheck; sym: CARDINAL) ;
266 BEGIN
267 IF sym = NulSym
268 THEN
269 (* Ignore. *)
270 ELSIF IsType (sym)
271 THEN
272 IF (constfunc IN s^.state) OR (constructor IN s^.state)
273 THEN
274 (* Ok. *)
275 ELSIF const IN s^.state
276 THEN
277 GenerateError (tok, s, sym)
278 END
279 ELSIF IsConst (sym)
280 THEN
281 IF (constfunc IN s^.state) OR (constructor IN s^.state)
282 THEN
283 (* Ok. *)
284 ELSIF (var IN s^.state) OR (type IN s^.state)
285 THEN
286 GenerateError (tok, s, sym)
287 END
288 ELSIF IsVar (sym)
289 THEN
290 IF constfunc IN s^.state
291 THEN
292 (* Ok. *)
293 ELSIF (const IN s^.state) OR (type IN s^.state) OR (var IN s^.state)
294 THEN
295 GenerateError (tok, s, sym)
296 END
297 END
298 END CheckQualident ;
299
300
301 (*
302 GenerateError - generates an unrecoverable error string based on the state and sym.
303 *)
304
305 PROCEDURE GenerateError (tok: CARDINAL; s: StateCheck; sym: CARDINAL) ;
306 VAR
307 str: String ;
308 BEGIN
309 str := InitString ('not expecting the {%1Ad} {%1a} in a ') ;
310 IF const IN s^.state
311 THEN
312 str := ConCat (str, Mark (InitString ('{%kCONST} block')))
313 ELSIF type IN s^.state
314 THEN
315 str := ConCat (str, Mark (InitString ('{%kTYPE} block')))
316 ELSIF var IN s^.state
317 THEN
318 str := ConCat (str, Mark (InitString ('{%kVAR} block')))
319 END ;
320 IF constfunc IN s^.state
321 THEN
322 str := ConCat (str, Mark (InitString (' and within a constant procedure function actual parameter')))
323 END ;
324 IF constructor IN s^.state
325 THEN
326 str := ConCat (str, Mark (InitString (' and within a constructor')))
327 END ;
328 MetaErrorStringT1 (tok, str, sym)
329 END GenerateError ;
330
331
332 (*
333 init - initialize the global variables in the module.
334 *)
335
336 PROCEDURE init ;
337 BEGIN
338 FreeList := NIL
339 END init ;
340
341
342 BEGIN
343 init
344 END M2StateCheck.