Debian Codegraph @

gcc/m2/gm2-compiler/M2Batch.mod

raw
1 (* M2Batch.mod implements a queue for modules pending compilation.
2
3 Copyright (C) 2001-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 M2Batch ;
23
24
25 FROM M2Debug IMPORT Assert ;
26
27 FROM SymbolTable IMPORT MakeModule, MakeDefImp, IsModule, IsDefImp,
28 GetScope, GetLocalSym, GetCurrentScope,
29 PutDeclared, GetSym, NulSym ;
30
31 FROM NameKey IMPORT GetKey, WriteKey ;
32 FROM M2Printf IMPORT printf2 ;
33 FROM M2Error IMPORT InternalError ;
34 FROM M2MetaError IMPORT MetaError1 ;
35 FROM Indexing IMPORT Index, InitIndex, GetIndice, HighIndice, RemoveIndiceFromIndex, IncludeIndiceIntoIndex, InBounds ;
36 FROM Lists IMPORT List, InitList, IncludeItemIntoList, RemoveItemFromList,
37 GetItemFromList, NoOfItemsInList ;
38 FROM Storage IMPORT ALLOCATE ;
39 FROM DynamicStrings IMPORT String ;
40 FROM M2Pass IMPORT IsPass1, IsPass2, IsPass3, IsPassC ;
41
42
43 TYPE
44 Module = POINTER TO RECORD
45 SymNo : CARDINAL ;
46 Key : Name ;
47 DefFile,
48 ModFile: String ;
49 END ;
50
51 VAR
52 SeenList : Index ;
53 PendingQueue: List ;
54
55
56 (*
57 MakeProgramSource - is given a Name, n, which is used to create a program module.
58 The program module will be placed onto the compilation
59 pending queue if it has not yet been compiled.
60 If the module has been compiled then no action is taken.
61 The Module Sym is returned.
62 *)
63
64 PROCEDURE MakeProgramSource (tok: CARDINAL; n: Name) : CARDINAL ;
65 VAR
66 Sym: CARDINAL ;
67 BEGIN
68 Sym := Get (n) ;
69 IF Sym = NulSym
70 THEN
71 Assert ((NOT IsPass1 ()) AND (NOT IsPass2 ()) AND (NOT IsPass3 ()) AND (NOT IsPassC ())) ;
72 (* Neither been compiled or on the Pending Queue *)
73 Sym := MakeModule (tok, n) ;
74 Put (Sym, n) ;
75 Push (Sym)
76 ELSE
77 PutDeclared (tok, Sym)
78 END ;
79 RETURN Sym
80 END MakeProgramSource ;
81
82
83 (*
84 MakeDefinitionSource - is given a Name, n, which is used to create a Definition
85 module.
86 The Definition Module will be placed onto the
87 compilation pending queue if it has not yet been
88 compiled.
89 If the module has been compiled then no action is
90 taken. The Module Sym is returned.
91 *)
92
93 PROCEDURE MakeDefinitionSource (tok: CARDINAL; n: Name) : CARDINAL ;
94 VAR
95 Sym: CARDINAL ;
96 BEGIN
97 Sym := Get (n) ;
98 IF Sym = NulSym
99 THEN
100 Assert ((NOT IsPass1 ()) AND (NOT IsPass2 ()) AND (NOT IsPass3 ()) AND (NOT IsPassC ())) ;
101 (* Neither been compiled or on the Pending Queue *)
102 Sym := MakeDefImp (tok, n) ;
103 Put (Sym, n) ;
104 Push (Sym)
105 ELSE
106 PutDeclared (tok, Sym)
107 END ;
108 RETURN Sym
109 END MakeDefinitionSource ;
110
111
112 (*
113 MakeImplementationSource - is given a Name, n, which is used to create an
114 implementation module.
115 The implementation Module will be placed onto
116 the compilation pending
117 queue if it has not yet been compiled.
118 If the module has been compiled then no
119 action is taken. The Module Sym is returned.
120 *)
121
122 PROCEDURE MakeImplementationSource (tok: CARDINAL; n: Name) : CARDINAL ;
123 VAR
124 Sym: CARDINAL ;
125 BEGIN
126 Sym := Get (n) ;
127 IF Sym = NulSym
128 THEN
129 Assert ((NOT IsPass1 ()) AND (NOT IsPass2 ()) AND (NOT IsPass3 ()) AND (NOT IsPassC ())) ;
130 (* Neither been compiled or on the Pending Queue *)
131 Sym := MakeDefImp (tok, n) ;
132 Put (Sym, n) ;
133 Push (Sym)
134 ELSE
135 PutDeclared (tok, Sym)
136 END ;
137 RETURN Sym
138 END MakeImplementationSource ;
139
140
141 (*
142 GetSource - returns with the symbol Sym of the next module to be compiled.
143 If Sym returns with value 0 then no module should be compiled.
144 *)
145
146 PROCEDURE GetSource () : CARDINAL ;
147 BEGIN
148 RETURN Pop ()
149 END GetSource ;
150
151
152 (*
153 GetModuleNo - returns with symbol number of the nth module read during Pass 1.
154 *)
155
156 PROCEDURE GetModuleNo (nth: CARDINAL) : CARDINAL ;
157 VAR
158 m: Module ;
159 BEGIN
160 Assert (nth#0) ;
161 IF InBounds (SeenList, nth)
162 THEN
163 m := GetIndice (SeenList, nth) ;
164 RETURN m^.SymNo
165 ELSE
166 RETURN NulSym
167 END
168 END GetModuleNo ;
169
170
171 (*
172 IsModuleKnown - returns TRUE if the Name n matches a module.
173 *)
174
175 PROCEDURE IsModuleKnown (n: Name) : BOOLEAN ;
176 BEGIN
177 RETURN Get (n) # NulSym
178 END IsModuleKnown ;
179
180
181 (*
182 Get - returns the module symbol matching name n.
183 *)
184
185 PROCEDURE Get (n: Name) : CARDINAL ;
186 VAR
187 i, no: CARDINAL ;
188 m : Module ;
189 BEGIN
190 i := 1 ;
191 no := HighIndice (SeenList) ;
192 WHILE i <= no DO
193 m := GetIndice (SeenList, i) ;
194 WITH m^ DO
195 IF Key = n
196 THEN
197 RETURN SymNo
198 ELSE
199 INC (i)
200 END
201 END
202 END ;
203 RETURN NulSym
204 END Get ;
205
206
207 PROCEDURE Put (Sym: CARDINAL; n: Name) ;
208 VAR
209 m: Module ;
210 BEGIN
211 NEW (m) ;
212 IncludeIndiceIntoIndex (SeenList, m) ;
213 WITH m^ DO
214 SymNo := Sym ;
215 Key := n ;
216 DefFile := NIL ;
217 ModFile := NIL
218 END
219 END Put ;
220
221
222 PROCEDURE Push (Sym: CARDINAL) ;
223 BEGIN
224 IncludeItemIntoList (PendingQueue, Sym)
225 END Push ;
226
227
228 PROCEDURE Pop () : CARDINAL ;
229 VAR
230 n : CARDINAL ;
231 Sym: CARDINAL ;
232 BEGIN
233 n := NoOfItemsInList (PendingQueue) ;
234 IF n = 0
235 THEN
236 RETURN NulSym
237 ELSE
238 Sym := GetItemFromList (PendingQueue, n) ;
239 RemoveItemFromList (PendingQueue, Sym) ;
240 RETURN Sym
241 END
242 END Pop ;
243
244
245 (*
246 DisplayModules - a debugging routine to textually emit the names of modules in the SeenList.
247 *)
248
249 PROCEDURE DisplayModules ;
250 VAR
251 m : Module ;
252 n, i: CARDINAL ;
253 BEGIN
254 i := 1 ;
255 n := HighIndice (SeenList) ;
256 WHILE i<=n DO
257 m := GetIndice (SeenList, i) ;
258 WITH m^ DO
259 printf2 ('Module %a %d\n', Key, i)
260 END ;
261 INC (i)
262 END
263 END DisplayModules ;
264
265
266 (*
267 AssociateDefinition - associate the source file, filename, with the definition module,
268 Sym.
269 *)
270
271 PROCEDURE AssociateDefinition (filename: String; Sym: CARDINAL) : String ;
272 VAR
273 no, i: CARDINAL ;
274 m : Module ;
275 BEGIN
276 i := 1 ;
277 no := HighIndice (SeenList) ;
278 WHILE i <= no DO
279 m := GetIndice (SeenList, i) ;
280 WITH m^ DO
281 IF SymNo = Sym
282 THEN
283 DefFile := filename ;
284 RETURN filename
285 ELSE
286 INC (i)
287 END
288 END
289 END ;
290 InternalError ('failed to find module sym')
291 END AssociateDefinition ;
292
293
294 (*
295 GetDefinitionModuleFile - returns the filename associated with the definition module, Sym.
296 It may return a temporary preprocessed file.
297 *)
298
299 PROCEDURE GetDefinitionModuleFile (Sym: CARDINAL) : String ;
300 VAR
301 no, i: CARDINAL ;
302 m : Module ;
303 BEGIN
304 i := 1 ;
305 no := HighIndice (SeenList) ;
306 WHILE i <= no DO
307 m := GetIndice (SeenList, i) ;
308 WITH m^ DO
309 IF SymNo = Sym
310 THEN
311 RETURN DefFile
312 ELSE
313 INC (i)
314 END
315 END
316 END ;
317 RETURN NIL
318 END GetDefinitionModuleFile ;
319
320
321 (*
322 AssociateModule - associate the source file, filename, with the implementation/program
323 module, Sym.
324 *)
325
326 PROCEDURE AssociateModule (filename: String; Sym: CARDINAL) : String ;
327 VAR
328 no, i: CARDINAL ;
329 m : Module ;
330 BEGIN
331 i := 1 ;
332 no := HighIndice (SeenList) ;
333 WHILE i<=no DO
334 m := GetIndice (SeenList, i) ;
335 WITH m^ DO
336 IF SymNo = Sym
337 THEN
338 ModFile := filename ;
339 RETURN filename
340 ELSE
341 INC (i)
342 END
343 END
344 END ;
345 InternalError ('failed to find module sym')
346 END AssociateModule ;
347
348
349 (*
350 GetModuleFile - returns the filename associated with the implementation/program module, Sym.
351 It may return a temporary preprocessed file.
352 *)
353
354 PROCEDURE GetModuleFile (Sym: CARDINAL) : String ;
355 VAR
356 no, i: CARDINAL ;
357 m : Module ;
358 BEGIN
359 i := 1 ;
360 no := HighIndice (SeenList) ;
361 WHILE i <= no DO
362 m := GetIndice (SeenList, i) ;
363 WITH m^ DO
364 IF SymNo = Sym
365 THEN
366 RETURN ModFile
367 ELSE
368 INC (i)
369 END
370 END
371 END ;
372 RETURN NIL
373 END GetModuleFile ;
374
375
376 (*
377 ForeachSourceModuleDo - for each source file call procedure, p.
378 *)
379
380 PROCEDURE ForeachSourceModuleDo (p: DoProcedure) ;
381 VAR
382 i, no: CARDINAL ;
383 m : Module ;
384 BEGIN
385 i := 1 ;
386 no := HighIndice (SeenList) ;
387 WHILE i<=no DO
388 m := GetIndice (SeenList, i) ;
389 WITH m^ DO
390 IF ModFile # NIL
391 THEN
392 p (SymNo)
393 END
394 END ;
395 INC (i)
396 END
397 END ForeachSourceModuleDo ;
398
399
400 (*
401 IsSourceSeen - returns TRUE if the source for the program module or
402 implementation module has been seen.
403 *)
404
405 PROCEDURE IsSourceSeen (sym: CARDINAL) : BOOLEAN ;
406 BEGIN
407 Assert (IsModule (sym) OR IsDefImp (sym)) ;
408 RETURN GetModuleFile (sym) # NIL
409 END IsSourceSeen ;
410
411
412 (*
413 IsModuleSeen - returns TRUE if the source for module, name, has been seen.
414 *)
415
416 PROCEDURE IsModuleSeen (n: Name) : BOOLEAN ;
417 BEGIN
418 RETURN Get (n) # NulSym
419 END IsModuleSeen ;
420
421
422 (*
423 LookupModule - looks up a module in the current scope, if a module does not exist
424 then it creates a DefImp module.
425 *)
426
427 PROCEDURE LookupModule (tok: CARDINAL; n: Name) : CARDINAL ;
428 VAR
429 sym: CARDINAL ;
430 BEGIN
431 sym := GetSym (n) ;
432 IF sym = NulSym
433 THEN
434 RETURN MakeDefinitionSource (tok, n)
435 ELSIF IsModule (sym) OR IsDefImp (sym)
436 THEN
437 RETURN sym
438 ELSE
439 RETURN MakeDefinitionSource (tok, n)
440 END
441 END LookupModule ;
442
443
444 (*
445 LookupOuterModule - looks up a module in the order of: current scope, then outer scope, finally if a
446 module does not exist then it creates a DefImp module.
447 *)
448
449 PROCEDURE LookupOuterModule (tok: CARDINAL; n: Name) : CARDINAL ;
450 VAR
451 outer: CARDINAL ;
452 sym : CARDINAL ;
453 BEGIN
454 sym := GetSym (n) ;
455 IF sym = NulSym
456 THEN
457 outer := GetScope (GetCurrentScope ()) ;
458 IF outer # NulSym
459 THEN
460 sym := GetLocalSym (outer, n)
461 END ;
462 IF sym = NulSym
463 THEN
464 (* not a local module, so it must be refering to a definition module. *)
465 sym := MakeDefinitionSource (tok, n)
466 END
467 END ;
468 IF IsModule (sym) OR IsDefImp (sym)
469 THEN
470 RETURN sym
471 ELSE
472 RETURN MakeDefinitionSource (tok, n)
473 END
474 END LookupOuterModule ;
475
476
477 BEGIN
478 InitList (PendingQueue) ;
479 SeenList := InitIndex (1)
480 END M2Batch.