Debian Codegraph @

gcc/m2/gm2-compiler/M2Comp.mod

raw
1 (* M2Comp.mod continually calls the compiler for every source file.
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 M2Comp ;
23
24
25 FROM M2Pass IMPORT SetPassToPass0, SetPassToPass1, SetPassToPass2, SetPassToPassC,
26 SetPassToPass3, SetPassToNoPass, SetPassToPassHidden ;
27
28 FROM M2Reserved IMPORT toktype ;
29 FROM M2Search IMPORT FindSourceDefFile, FindSourceModFile ;
30 FROM M2Code IMPORT Code ;
31
32 FROM M2LexBuf IMPORT OpenSource, CloseSource, ResetForNewPass, currenttoken, GetToken,
33 ReInitialize, currentstring, GetTokenNo, BuiltinTokenNo,
34 UnknownTokenNo ;
35
36 FROM M2FileName IMPORT CalculateFileName ;
37 FROM M2Preprocess IMPORT PreprocessModule, MakeSaveTempsFileNameExt, OnExitDelete ;
38 FROM libc IMPORT exit ;
39
40 FROM M2Error IMPORT ErrorStringAt, ErrorStringAt2, ErrorStringsAt2,
41 WriteFormat0, FlushErrors, FlushWarnings, ResetErrorScope ;
42
43 FROM M2MetaError IMPORT MetaErrorString0, MetaErrorString1, MetaError0, MetaError1,
44 MetaString0 ;
45
46 FROM FormatStrings IMPORT Sprintf1 ;
47 FROM P0SymBuild IMPORT P0Init, P1Init ;
48 FROM M2Debug IMPORT Assert ;
49
50 IMPORT m2flex ;
51 IMPORT P0SyntaxCheck ;
52 IMPORT P1Build ;
53 IMPORT P2Build ;
54 IMPORT PCBuild ;
55 IMPORT P3Build ;
56 IMPORT PHBuild ;
57 IMPORT PCSymBuild ;
58 IMPORT DynamicStrings ;
59
60 FROM M2Batch IMPORT GetSource, GetModuleNo, GetDefinitionModuleFile, GetModuleFile,
61 AssociateModule, AssociateDefinition, MakeImplementationSource,
62 MakeProgramSource ;
63
64 FROM SymbolTable IMPORT GetSymName, IsDefImp, NulSym,
65 IsHiddenTypeDeclared, GetFirstUsed, GetMainModule, SetMainModule,
66 ResolveConstructorTypes, SanityCheckConstants, IsDefinitionForC,
67 IsBuiltinInModule, PutModLink, IsDefLink, IsModLink, PutLibName,
68 GetModuleDefImportStatementList, GetModuleModImportStatementList,
69 GetImportModule, IsImportStatement, IsImport,
70 GetImportStatementList ;
71
72 FROM M2Search IMPORT FindSourceDefFile ;
73
74 FROM FIO IMPORT File, StdErr, StdOut, Close, EOF, IsNoError, WriteLine,
75 WriteChar, FlushOutErr ;
76
77 FROM SFIO IMPORT WriteS, OpenToRead, OpenToWrite, ReadS, WriteS ;
78 FROM NameKey IMPORT Name, GetKey, KeyToCharStar, makekey ;
79 FROM M2Printf IMPORT fprintf0, fprintf1 ;
80 FROM M2Quiet IMPORT qprintf0, qprintf1, qprintf2 ;
81
82 FROM M2Options IMPORT Verbose, GetM2Prefix, GetM, GetMM, GetDepTarget, GetMF, GetMP,
83 GetObj, PPonly, Statistics, Quiet, WholeProgram, GetMD, GetMMD,
84 ExtendedOpaque, GenModuleList ;
85
86 FROM PathName IMPORT DumpPathName ;
87 FROM Lists IMPORT List, NoOfItemsInList, GetItemFromList ;
88 FROM Indexing IMPORT Index, InitIndex, KillIndex, GetIndice, PutIndice, HighIndice ;
89
90 FROM DynamicStrings IMPORT String, InitString, KillString, InitStringCharStar,
91 Dup, Mark, EqualArray, string, Length, ConCat, ConCatChar,
92 InitStringChar, RIndex, Slice, Equal, RemoveWhitePrefix ;
93
94
95 CONST
96 Debugging = FALSE ;
97
98 VAR
99 ModuleType : (None, Definition, Implementation, Program) ;
100 DepContent : Index ;
101 DepOutput : String ;
102
103
104 (*
105 CompilingDefinitionModule - returns true if the current module being
106 compiled is a definition module.
107 *)
108
109 PROCEDURE CompilingDefinitionModule() : BOOLEAN ;
110 BEGIN
111 RETURN( ModuleType=Definition )
112 END CompilingDefinitionModule ;
113
114
115 (*
116 CompilingImplementationModule - returns true if the current module being
117 compiled is an implementation module.
118 *)
119
120 PROCEDURE CompilingImplementationModule() : BOOLEAN ;
121 BEGIN
122 RETURN( ModuleType=Implementation )
123 END CompilingImplementationModule ;
124
125
126 (*
127 CompilingProgramModule - returns true if the current module being
128 compiled is a program module.
129 *)
130
131 PROCEDURE CompilingProgramModule() : BOOLEAN ;
132 BEGIN
133 RETURN( ModuleType=Program )
134 END CompilingProgramModule ;
135
136
137 (*
138 NeedToParseImplementation -
139 *)
140
141 PROCEDURE NeedToParseImplementation (sym: CARDINAL) : BOOLEAN ;
142 BEGIN
143 RETURN (IsDefImp(sym) AND IsHiddenTypeDeclared(sym) AND ExtendedOpaque) OR
144 (IsDefImp(sym) AND IsBuiltinInModule(sym)) OR
145 (WholeProgram AND (NOT IsDefinitionForC(sym)))
146 END NeedToParseImplementation ;
147
148
149 (*
150 GenerateDefDependency - generate a single dependency for the definition module
151 providing that it can be found and is not blocked by -MM.
152 *)
153
154 PROCEDURE GenerateDefDependency (module: CARDINAL) ;
155 VAR
156 stem,
157 fullpath,
158 named : String ;
159 BEGIN
160 stem := InitStringCharStar (KeyToCharStar (GetSymName (module))) ;
161 named := NIL ;
162 IF FindSourceDefFile (stem, fullpath, named)
163 THEN
164 IF EqualArray (named, '') OR (NOT GetMM ())
165 THEN
166 MergeDep (DepContent, fullpath)
167 ELSE
168 fullpath := KillString (fullpath)
169 END
170 END ;
171 stem := KillString (stem) ;
172 named := KillString (named)
173 END GenerateDefDependency ;
174
175
176 (*
177 GenerateDependenciesFromImport - lookup the module associated with the import
178 and call GenerateDefDependency.
179 *)
180
181 PROCEDURE GenerateDependenciesFromImport (import: CARDINAL) ;
182 VAR
183 module : CARDINAL ;
184 BEGIN
185 Assert (IsImport (import)) ;
186 module := GetImportModule (import) ;
187 GenerateDefDependency (module)
188 END GenerateDependenciesFromImport ;
189
190
191 (*
192 GenerateDependenciesFromList - iterative over the import lists and for
193 each module issue a dependency.
194 *)
195
196 PROCEDURE GenerateDependenciesFromList (dep: List) ;
197 VAR
198 importList: List ;
199 import : CARDINAL ;
200 i, n, j, m: CARDINAL ;
201 BEGIN
202 n := NoOfItemsInList (dep) ;
203 i := 1 ;
204 WHILE i <= n DO
205 import := GetItemFromList (dep, i) ;
206 IF IsImportStatement (import)
207 THEN
208 importList := GetImportStatementList (import) ;
209 j := 1 ;
210 m := NoOfItemsInList (importList) ;
211 WHILE j <= m DO
212 import := GetItemFromList (importList, j) ;
213 GenerateDependenciesFromImport (import) ;
214 INC (j)
215 END
216 ELSE
217 GenerateDependenciesFromImport (import)
218 END ;
219 INC (i)
220 END
221 END GenerateDependenciesFromList ;
222
223
224 (*
225 GenerateDependencies - generate a list of dependencies for the main module where
226 the source code is found in sourcefile.
227 *)
228
229 PROCEDURE GenerateDependencies ;
230 BEGIN
231 IF IsDefImp (GetMainModule ())
232 THEN
233 GenerateDependenciesFromList (GetModuleDefImportStatementList (GetMainModule ())) ;
234 GenerateDefDependency (GetMainModule ())
235 END ;
236 GenerateDependenciesFromList (GetModuleModImportStatementList (GetMainModule ())) ;
237 WriteDepContents (DepOutput, DepContent)
238 END GenerateDependencies ;
239
240
241 (*
242 Compile - compile file, s, using a 5 pass technique.
243 *)
244
245 PROCEDURE Compile (s: String) ;
246 BEGIN
247 DoPass0 (s) ;
248 FlushWarnings ; FlushErrors ;
249 ResetForNewPass ; ResetErrorScope ;
250 qprintf0('Pass 1: scopes, enumerated types, imports and exports\n') ;
251 DoPass1 ;
252 FlushWarnings ; FlushErrors ;
253 IF GetM () OR GetMM ()
254 THEN
255 GenerateDependencies
256 END ;
257 IF NOT PPonly
258 THEN
259 qprintf0('Pass 2: constants and types\n') ;
260 ResetForNewPass ; ResetErrorScope ;
261 DoPass2 ;
262 FlushWarnings ; FlushErrors ;
263 qprintf0('Pass C: aggregate constants\n') ;
264 ResetForNewPass ; ResetErrorScope ;
265 DoPassC ;
266 FlushWarnings ; FlushErrors ;
267 qprintf0('Pass 3: quadruple generation\n') ;
268 ResetForNewPass ; ResetErrorScope ;
269 DoPass3 ;
270 FlushWarnings ; FlushErrors ;
271 qprintf0('Pass 4: gcc tree generation\n') ;
272 Code ;
273 FlushWarnings ; FlushErrors
274 END
275 END Compile ;
276
277
278 (*
279 compile - compile the filename.
280 *)
281
282 PROCEDURE compile (filename: ADDRESS) ;
283 VAR
284 f: String ;
285 BEGIN
286 f := InitStringCharStar (filename) ;
287 Compile (f) ;
288 f := KillString (f)
289 END compile ;
290
291
292 (*
293 ExamineHeader - examines up until the ';', '[' or eof and determines if the source file
294 is a program, implementation/definition module.
295 *)
296
297 PROCEDURE ExamineHeader (VAR name: ADDRESS; VAR isdefimp, module: BOOLEAN) ;
298 BEGIN
299 (* Stop if we see one of eof ';' '['. *)
300 WHILE (currenttoken#eoftok) AND
301 (currenttoken#semicolontok) AND (currenttoken#lsbratok) DO
302 IF name = NIL
303 THEN
304 IF (currenttoken=implementationtok) OR (currenttoken=definitiontok)
305 THEN
306 isdefimp := TRUE ;
307 GetToken
308 END ;
309 IF currenttoken=moduletok
310 THEN
311 module := TRUE ;
312 GetToken ;
313 IF currenttoken=identtok
314 THEN
315 name := currentstring
316 END
317 END ;
318 END ;
319 GetToken
320 END ;
321 END ExamineHeader ;
322
323
324 (*
325 ExamineCompilationUnit - opens the source file to obtain the module name and kind of module.
326 *)
327
328 PROCEDURE ExamineCompilationUnit () : CARDINAL ;
329 VAR
330 Message : String ;
331 name : ADDRESS ;
332 module,
333 isdefimp: BOOLEAN ;
334 BEGIN
335 name := NIL ;
336 isdefimp := FALSE ; (* default to program module *)
337 module := FALSE ; (* Seen module keyword? *)
338 ExamineHeader (name, isdefimp, module) ;
339 IF name = NIL
340 THEN
341 IF module
342 THEN
343 Message := MetaString0 (InitString ('no {%kMODULE} keyword seen'))
344 ELSE
345 Message := MetaString0 (InitString ('no module ident seen'))
346 END ;
347 m2flex.M2Error (string (Message)) ;
348 exit (1)
349 ELSE
350 (* The token used is will be overwritten when P0 is underway.
351 At this point we are determining the module kind and the tokens
352 read will be discarded (see ReInitialize below). *)
353 IF isdefimp
354 THEN
355 RETURN MakeImplementationSource (BuiltinTokenNo, makekey (name))
356 ELSE
357 RETURN MakeProgramSource (BuiltinTokenNo, makekey (name))
358 END
359 END
360 END ExamineCompilationUnit ;
361
362
363 (*
364 PeepInto - peeps into source, s, and initializes a definition/implementation or
365 program module accordingly.
366 *)
367
368 PROCEDURE PeepInto (s: String) ;
369 VAR
370 mainModule: CARDINAL ;
371 BEGIN
372 IF OpenSource (s)
373 THEN
374 mainModule := ExamineCompilationUnit () ;
375 IF mainModule # NulSym
376 THEN
377 SetMainModule (mainModule)
378 END ;
379 CloseSource ;
380 ReInitialize
381 ELSE
382 fprintf1 (StdErr, 'failed to open %s\n', s) ;
383 exit (1)
384 END
385 END PeepInto ;
386
387
388 (*
389 qprintLibName - print the libname.
390 *)
391
392 PROCEDURE qprintLibName (LibName: String) ;
393 BEGIN
394 IF (LibName # NIL) AND (NOT EqualArray (LibName, ''))
395 THEN
396 qprintf1 (' [%s]', LibName)
397 END
398 END qprintLibName ;
399
400
401 (*
402 CreateFileStem - create a stem using the template LibName_ModuleName.
403 *)
404
405 PROCEDURE CreateFileStem (SymName, LibName: String) : String ;
406 BEGIN
407 IF Length (LibName) > 0
408 THEN
409 RETURN ConCat (Dup (LibName), ConCat (InitStringChar ('_'), SymName))
410 ELSE
411 RETURN SymName
412 END
413 END CreateFileStem ;
414
415
416 (*
417 Return basename of path. CutExt determines whether the .extension
418 should be removed.
419 *)
420
421 PROCEDURE BaseName (Path: String; CutExt: BOOLEAN) : String ;
422 VAR
423 ext,
424 basename: INTEGER ;
425 BEGIN
426 basename := RIndex (Path, '/', 0) ;
427 IF basename = -1
428 THEN
429 basename := 0
430 ELSE
431 basename := basename + 1
432 END ;
433 IF CutExt
434 THEN
435 ext := RIndex (Path, '.', 0) ;
436 IF ext=-1
437 THEN
438 ext := 0
439 END
440 ELSE
441 ext := 0
442 END ;
443 RETURN Slice (Path, basename, ext)
444 END BaseName ;
445
446
447 (*
448 IsLibrary - return TRUE if line contains a library module.
449 *)
450
451 PROCEDURE IsLibrary (line: String) : BOOLEAN ;
452 VAR
453 moduleName,
454 libname, filename: String ;
455 result : BOOLEAN ;
456 BEGIN
457 result := FALSE ;
458 moduleName := BaseName (line, TRUE) ;
459 filename := NIL ;
460 libname := NIL ;
461 IF FindSourceDefFile (moduleName, filename, libname)
462 THEN
463 moduleName := KillString (moduleName) ;
464 IF Length (libname) > 0
465 THEN
466 moduleName := BaseName (line, FALSE) ;
467 line := BaseName (line, FALSE) ;
468 result := Equal (line, moduleName) ;
469 line := KillString (line) ;
470 END
471 END ;
472 libname := KillString (libname) ;
473 filename := KillString (filename) ;
474 moduleName := KillString (moduleName) ;
475 RETURN result
476 END IsLibrary ;
477
478
479 (*
480 IsUnique - return TRUE if line is unique in array content.
481 *)
482
483 PROCEDURE IsUnique (content: Index; line: String) : BOOLEAN ;
484 VAR
485 high, i: CARDINAL ;
486 BEGIN
487 high := HighIndice (content) ;
488 i := 1 ;
489 WHILE i <= high DO
490 IF Equal (line, GetIndice (content, i))
491 THEN
492 RETURN FALSE
493 END ;
494 INC (i)
495 END ;
496 RETURN TRUE
497 END IsUnique ;
498
499
500 (*
501 Append - append line to array content.
502 *)
503
504 PROCEDURE Append (content: Index; line: String) ;
505 VAR
506 high: CARDINAL ;
507 BEGIN
508 high := HighIndice (content) ;
509 PutIndice (content, high+1, line)
510 END Append ;
511
512
513 (*
514 MergeDep - if line is unique in array content then append.
515 Check to see (and ignore) if line is a library module and -MM
516 is present.
517 *)
518
519 PROCEDURE MergeDep (content: Index; line: String) ;
520 BEGIN
521 line := RemoveWhitePrefix (line) ;
522 IF (NOT EqualArray (line, "\")) AND (Length (line) > 0)
523 THEN
524 (* Ignore if -MM and is a library module. *)
525 IF NOT (GetMM () AND IsLibrary (line))
526 THEN
527 IF IsUnique (content, line)
528 THEN
529 Append (content, line)
530 END
531 END
532 END
533 END MergeDep ;
534
535
536 (*
537 splitLine - split a line into words separated by spaces
538 and call MergeDep on each word.
539 *)
540
541 PROCEDURE splitLine (content: Index; line: String) ;
542 VAR
543 word : String ;
544 space: INTEGER ;
545 BEGIN
546 REPEAT
547 line := RemoveWhitePrefix (line) ;
548 space := DynamicStrings.Index (line, ' ', 0) ;
549 IF space > 0
550 THEN
551 word := Slice (line, 0, space) ;
552 word := RemoveWhitePrefix (word) ;
553 IF Length (word) > 0
554 THEN
555 MergeDep (content, word)
556 END ;
557 line := Slice (line, space, 0) ;
558 ELSIF space < 0
559 THEN
560 MergeDep (content, line)
561 END
562 UNTIL space <= 0
563 END splitLine ;
564
565
566 (*
567 MergeDeps - foreach dependency in ChildDep do
568 add dependency to ChildDep if not already present.
569 ignore all ChildDep if -MM and libname # "".
570 *)
571
572 PROCEDURE MergeDeps (content: Index; ChildDep, LibName: String) ;
573 VAR
574 line: String ;
575 in : File ;
576 BEGIN
577 IF (content # NIL) AND (NOT (GetMM () AND (Length (LibName) > 0)))
578 THEN
579 in := OpenToRead (ChildDep) ;
580 IF IsNoError (in)
581 THEN
582 line := ReadS (in) ; (* Skip over first line containing the module object. *)
583 WHILE NOT EOF (in) DO
584 line := ReadS (in) ;
585 splitLine (content, line)
586 END
587 END ;
588 Close (in)
589 END
590 END MergeDeps ;
591
592
593 (*
594 GetRuleTarget - return the rule target which is derived from the -MT arg
595 or -o arg or filename.mod.
596 *)
597
598 PROCEDURE GetRuleTarget (filename: String) : String ;
599 BEGIN
600 IF GetDepTarget () # NIL
601 THEN
602 RETURN InitStringCharStar (GetDepTarget ())
603 ELSIF GetMF () # NIL
604 THEN
605 RETURN InitStringCharStar (GetMF ())
606 ELSE
607 RETURN ConCat (BaseName (filename, TRUE), InitString ('.o'))
608 END
609 END GetRuleTarget ;
610
611
612 (*
613 ReadDepContents - reads the contents of file dep into a dynamic array
614 and return the array. The file will be split into words
615 and each word stored as an entry in the array.
616 *)
617
618 PROCEDURE ReadDepContents (filename, dep: String) : Index ;
619 VAR
620 content: Index ;
621 line : String ;
622 in : File ;
623 BEGIN
624 content := NIL ;
625 IF GetM () OR GetMM ()
626 THEN
627 in := OpenToRead (dep) ;
628 (* The file might not be created (if -MD or -MMD is used as these options
629 operate without preprocessing) in which case we create an dynamic
630 array with the source filename and target. *)
631 content := InitIndex (1) ;
632 IF GetMD () OR GetMMD () OR (NOT IsNoError (in))
633 THEN
634 (* No preprocessing done therefore create first two lines using
635 target and source. *)
636 PutIndice (content, 1, ConCatChar (GetRuleTarget (filename), ':')) ;
637 PutIndice (content, 2, Dup (filename))
638 ELSE
639 (* Preprocessing (using cc1) has created one for us, so we read it. *)
640 WHILE NOT EOF (in) DO
641 line := ReadS (in) ;
642 splitLine (content, line)
643 END
644 END ;
645 Close (in)
646 END ;
647 RETURN content
648 END ReadDepContents ;
649
650
651 (*
652 WriteDep - write the dependencies and target to file out.
653 *)
654
655 PROCEDURE WriteDep (contents: Index; out: File) ;
656 VAR
657 i, h: CARDINAL ;
658 line: String ;
659 BEGIN
660 i := 1 ;
661 h := HighIndice (contents) ;
662 WHILE i <= h DO
663 line := GetIndice (contents, i) ;
664 line := RemoveWhitePrefix (line) ;
665 IF Length (line) > 0
666 THEN
667 IF i = 1
668 THEN
669 (* First line is always the target. *)
670 IF GetDepTarget () # NIL
671 THEN
672 line := ConCatChar (InitStringCharStar (GetDepTarget ()), ':')
673 END
674 ELSIF i > 1
675 THEN
676 WriteChar (out, ' ')
677 END ;
678 line := WriteS (out, line) ;
679 IF i < h
680 THEN
681 WriteChar (out, ' ') ;
682 WriteChar (out, '\')
683 END ;
684 WriteLine (out)
685 END ;
686 INC (i)
687 END
688 END WriteDep ;
689
690
691 (*
692 WritePhonyDep - write the dependencies and target to file out.
693 *)
694
695 PROCEDURE WritePhonyDep (contents: Index; out: File) ;
696 VAR
697 i, h: CARDINAL ;
698 line: String ;
699 BEGIN
700 (* The first line is always the target and the second line is always
701 the top level source file. *)
702 i := 3 ;
703 h := HighIndice (contents) ;
704 WHILE i <= h DO
705 line := GetIndice (contents, i) ;
706 line := RemoveWhitePrefix (line) ;
707 IF Length (line) > 0
708 THEN
709 line := WriteS (out, line) ;
710 WriteChar (out, ':') ;
711 WriteLine (out)
712 END ;
713 INC (i)
714 END
715 END WritePhonyDep ;
716
717
718 (*
719 WriteDepContents - write the dynamic array to filename dep (or StdOut) if
720 the GetMF file is NIL.
721 *)
722
723 PROCEDURE WriteDepContents (dep: String; contents: Index) ;
724 VAR
725 out: File ;
726 BEGIN
727 IF (contents # NIL) AND (GetM () OR GetMM ())
728 THEN
729 IF GetMF () = NIL
730 THEN
731 out := StdOut ;
732 dep := OnExitDelete (dep)
733 ELSE
734 out := OpenToWrite (dep)
735 END ;
736 IF IsNoError (out)
737 THEN
738 WriteDep (contents, out) ;
739 IF GetMP ()
740 THEN
741 WritePhonyDep (contents, out)
742 END
743 END ;
744 IF GetMF () = NIL
745 THEN
746 FlushOutErr
747 ELSE
748 Close (out) ;
749 END ;
750 contents := KillIndex (contents)
751 END
752 END WriteDepContents ;
753
754
755 (*
756 CreateDepFilename - return a dependency filename associated with filename or use GetMF.
757 *)
758
759 PROCEDURE CreateDepFilename (filename: String) : String ;
760 VAR
761 depfile: String ;
762 BEGIN
763 IF GetMF () = NIL
764 THEN
765 depfile := MakeSaveTempsFileNameExt (filename, InitString ('.d')) ;
766 RETURN OnExitDelete (depfile)
767 ELSE
768 RETURN InitStringCharStar (GetMF ())
769 END
770 END CreateDepFilename ;
771
772
773 (*
774 Pass0CheckDef -
775 *)
776
777 PROCEDURE Pass0CheckDef (sym: CARDINAL) : BOOLEAN ;
778 VAR
779 ChildDep,
780 SymName,
781 FileName,
782 LibName : String ;
783 BEGIN
784 LibName := NIL ;
785 FileName := NIL ;
786 SymName := InitStringCharStar (KeyToCharStar (GetSymName (sym))) ;
787 IF IsDefImp (sym)
788 THEN
789 IF FindSourceDefFile (SymName, FileName, LibName)
790 THEN
791 ModuleType := Definition ;
792 ChildDep := MakeSaveTempsFileNameExt (CreateFileStem (SymName, LibName), InitString ('.def.d')) ;
793 IF OpenSource (AssociateDefinition (PreprocessModule (FileName, FALSE, TRUE,
794 ChildDep), sym))
795 THEN
796 IF NOT P0SyntaxCheck.CompilationUnit ()
797 THEN
798 WriteFormat0 ('compilation failed') ;
799 CloseSource ;
800 SymName := KillString (SymName) ;
801 FileName := KillString (FileName) ;
802 LibName := KillString (LibName) ;
803 RETURN FALSE
804 END ;
805 qprintf2 (' Module %-20s : %s', SymName, FileName) ;
806 qprintLibName (LibName) ;
807 PutLibName (sym, makekey (string (LibName))) ;
808 IF IsDefinitionForC (sym)
809 THEN
810 qprintf0 (' (for C)')
811 END ;
812 IF IsDefLink (sym)
813 THEN
814 qprintf0 (' (linking)')
815 END ;
816 qprintf0 ('\n') ;
817 CloseSource ;
818 MergeDeps (DepContent, ChildDep, LibName)
819 ELSE
820 (* Unrecoverable error. *)
821 MetaErrorString1 (Sprintf1 (InitString ('file {%%1EUAF%s} containing module {%%1a} cannot be found'),
822 FileName), sym)
823 END
824 ELSE
825 (* Unrecoverable error. *)
826 MetaError1 ('the file containing the definition module {%1EMAa} cannot be found', sym)
827 END ;
828 ModuleType := Implementation
829 ELSE
830 ModuleType := Program
831 END ;
832 SymName := KillString (SymName) ;
833 FileName := KillString (FileName) ;
834 LibName := KillString (LibName) ;
835 RETURN TRUE
836 END Pass0CheckDef ;
837
838
839 (*
840 Pass0CheckMod -
841 *)
842
843 PROCEDURE Pass0CheckMod (sym: CARDINAL; PPSource: String) : BOOLEAN ;
844 VAR
845 Main : CARDINAL ;
846 ChildDep,
847 SymName,
848 FileName,
849 LibName : String ;
850 BEGIN
851 SymName := InitStringCharStar (KeyToCharStar (GetSymName (sym))) ;
852 FileName := NIL ;
853 LibName := NIL ;
854 Main := GetMainModule () ;
855 IF (Main = sym) OR NeedToParseImplementation (sym)
856 THEN
857 (* Only need to read implementation module if hidden types are
858 declared or it is the main module. *)
859 IF Main = sym
860 THEN
861 FileName := Dup (PPSource) ;
862 LibName := InitStringCharStar (GetM2Prefix ()) ;
863 PutLibName (sym, makekey (string (LibName)))
864 ELSE
865 IF FindSourceModFile (SymName, FileName, LibName)
866 THEN
867 ChildDep := MakeSaveTempsFileNameExt (CreateFileStem (SymName, LibName), InitString ('.mod.d')) ;
868 FileName := PreprocessModule (FileName, FALSE, TRUE, ChildDep) ;
869 PutLibName (sym, makekey (string (LibName))) ;
870 MergeDeps (DepContent, ChildDep, LibName)
871 ELSE
872 qprintf1 (' Module %-20s : implementation source file not found\n', SymName)
873 END
874 END ;
875
876 IF FileName # NIL
877 THEN
878 IF OpenSource (AssociateModule (Dup (FileName), sym))
879 THEN
880 IF NOT P0SyntaxCheck.CompilationUnit ()
881 THEN
882 WriteFormat0 ('compilation failed') ;
883 CloseSource ;
884 SymName := KillString (SymName) ;
885 FileName := KillString (FileName) ;
886 LibName := KillString (LibName) ;
887 RETURN FALSE
888 END ;
889 qprintf2 (' Module %-20s : %s', SymName, FileName) ;
890 qprintLibName (LibName) ;
891 IF IsModLink (sym)
892 THEN
893 qprintf0 (' (linking)')
894 END ;
895 qprintf0 ('\n') ;
896 CloseSource
897 ELSE
898 (* It is quite legitimate to implement a module in C (and pretend it was a M2
899 implementation) providing that it is not the main program module and the
900 definition module does not declare a hidden type when -fextended-opaque
901 is used. *)
902 IF (NOT WholeProgram) OR (sym = Main) OR IsHiddenTypeDeclared (sym)
903 THEN
904 (* Unrecoverable error. *)
905 MetaErrorString1 (Sprintf1 (InitString ('file {%%1EUAF%s} containing module {%%1a} cannot be found'),
906 FileName), sym) ;
907 END
908 END
909 END
910 ELSIF GenModuleList
911 THEN
912 IF IsDefImp (sym) AND (NOT IsDefinitionForC (sym))
913 THEN
914 (* The implementation module is only useful if -fgen-module-list= is
915 used (to gather all dependencies). Note that we do not insist
916 upon finding the implementation module. *)
917 LibName := NIL ;
918 IF FindSourceModFile (SymName, FileName, LibName)
919 THEN
920 PutLibName (sym, makekey (string (LibName))) ;
921 qprintf2 (' Module %-20s : %s' , SymName, FileName) ;
922 qprintLibName (LibName) ;
923 qprintf0 (' (linking)\n') ;
924 ChildDep := MakeSaveTempsFileNameExt (CreateFileStem (SymName, LibName), InitString ('.mod.d')) ;
925 IF OpenSource (AssociateModule (PreprocessModule (FileName, FALSE, TRUE, ChildDep), sym))
926 THEN
927 PutModLink (sym, TRUE) ; (* This source is only used to determine link time info. *)
928 IF NOT P0SyntaxCheck.CompilationUnit ()
929 THEN
930 WriteFormat0 ('compilation failed') ;
931 CloseSource ;
932 SymName := KillString (SymName) ;
933 FileName := KillString (FileName) ;
934 LibName := KillString (LibName) ;
935 RETURN FALSE
936 END ;
937 CloseSource ;
938 MergeDeps (DepContent, ChildDep, LibName)
939 END
940 END
941 END
942 END ;
943 SymName := KillString (SymName) ;
944 FileName := KillString (FileName) ;
945 LibName := KillString (LibName) ;
946 RETURN TRUE
947 END Pass0CheckMod ;
948
949
950 (*
951 DoPass0 -
952 *)
953
954 PROCEDURE DoPass0 (filename: String) ;
955 VAR
956 sym : CARDINAL ;
957 i : CARDINAL ;
958 PPSource : String ;
959 BEGIN
960 P0Init ;
961 SetPassToPass0 ;
962 (* Maybe preprocess the main file. *)
963 DepOutput := CreateDepFilename (filename) ;
964 PPSource := PreprocessModule (filename, TRUE, FALSE, DepOutput) ;
965 DepContent := ReadDepContents (filename, DepOutput) ;
966 PeepInto (PPSource) ;
967 i := 1 ;
968 sym := GetModuleNo (i) ;
969 qprintf1 ('Compiling: %s\n', PPSource) ;
970 IF Debugging
971 THEN
972 DumpPathName ('DoPass0')
973 END ;
974 IF Verbose
975 THEN
976 fprintf1 (StdOut, 'Compiling: %s\n', PPSource)
977 END ;
978 qprintf0 ('Pass 0: lexical analysis, parsing, modules and associated filenames\n') ;
979 WHILE sym # NulSym DO
980 IF NOT Pass0CheckDef (sym)
981 THEN
982 RETURN
983 END ;
984 IF NOT Pass0CheckMod (sym, PPSource)
985 THEN
986 RETURN
987 END ;
988 INC (i) ;
989 sym := GetModuleNo (i)
990 END ;
991 SetPassToNoPass
992 END DoPass0 ;
993
994
995 (*
996 DoPass1 - parses the sources of all modules necessary to compile
997 the required module, Main.
998 *)
999
1000 PROCEDURE DoPass1 ;
1001 VAR
1002 name : Name ;
1003 Sym : CARDINAL ;
1004 i : CARDINAL ;
1005 FileName: String ;
1006 BEGIN
1007 P1Init ;
1008 SetPassToPass1 ;
1009 i := 1 ;
1010 Sym := GetModuleNo(i) ;
1011 WHILE Sym#NulSym DO
1012 FileName := GetDefinitionModuleFile(Sym) ;
1013 IF FileName#NIL
1014 THEN
1015 IF Debugging
1016 THEN
1017 name := GetSymName(Sym) ;
1018 qprintf1(' Module %a\n', name)
1019 END ;
1020 IF OpenSource(FileName)
1021 THEN
1022 ModuleType := Definition ;
1023 IF NOT P1Build.CompilationUnit()
1024 THEN
1025 MetaError0('compilation failed') ;
1026 CloseSource ;
1027 RETURN
1028 END ;
1029 CloseSource
1030 ELSE
1031 fprintf1(StdErr, 'failed to open %s\n', FileName) ;
1032 exit(1)
1033 END ;
1034 ModuleType := Implementation
1035 ELSE
1036 ModuleType := Program
1037 END ;
1038 FileName := GetModuleFile(Sym) ;
1039 IF FileName#NIL
1040 THEN
1041 IF Debugging
1042 THEN
1043 name := GetSymName(Sym) ;
1044 qprintf1(' Module %a\n', name)
1045 END ;
1046 IF OpenSource(FileName)
1047 THEN
1048 IF NOT P1Build.CompilationUnit()
1049 THEN
1050 MetaError0('compilation failed') ;
1051 CloseSource ;
1052 RETURN
1053 END ;
1054 CloseSource
1055 ELSE
1056 fprintf1(StdErr, 'failed to open %s\n', FileName) ;
1057 exit(1)
1058 END
1059 END ;
1060 INC(i) ;
1061 Sym := GetModuleNo(i)
1062 END ;
1063 SetPassToNoPass
1064 END DoPass1 ;
1065
1066
1067 (*
1068 DoPass2 - parses the sources of all modules necessary to compile
1069 the required module, Main.
1070 *)
1071
1072 PROCEDURE DoPass2 ;
1073 VAR
1074 name : Name ;
1075 Sym : CARDINAL ;
1076 i : CARDINAL ;
1077 FileName: String ;
1078 BEGIN
1079 SetPassToPass2 ;
1080 i := 1 ;
1081 Sym := GetModuleNo(i) ;
1082 WHILE Sym#NulSym DO
1083 FileName := GetDefinitionModuleFile(Sym) ;
1084 IF FileName#NIL
1085 THEN
1086 IF Debugging
1087 THEN
1088 name := GetSymName(Sym) ;
1089 qprintf1(' Module %a\n', name)
1090 END ;
1091 IF OpenSource(FileName)
1092 THEN
1093 ModuleType := Definition ;
1094 IF NOT P2Build.CompilationUnit()
1095 THEN
1096 MetaError0('compilation failed') ;
1097 CloseSource ;
1098 RETURN
1099 END ;
1100 CloseSource
1101 ELSE
1102 fprintf1(StdErr, 'failed to open %s\n', FileName) ;
1103 exit(1)
1104 END ;
1105 ModuleType := Implementation
1106 ELSE
1107 ModuleType := Program
1108 END ;
1109 FileName := GetModuleFile(Sym) ;
1110 IF FileName#NIL
1111 THEN
1112 IF Debugging
1113 THEN
1114 name := GetSymName(Sym) ;
1115 qprintf1(' Module %a\n', name)
1116 END ;
1117 IF OpenSource(FileName)
1118 THEN
1119 IF NOT P2Build.CompilationUnit()
1120 THEN
1121 MetaError0('compilation failed') ;
1122 CloseSource ;
1123 RETURN
1124 END ;
1125 CloseSource
1126 ELSE
1127 fprintf1(StdErr, 'failed to open %s\n', FileName) ;
1128 exit(1)
1129 END
1130 END ;
1131 INC(i) ;
1132 Sym := GetModuleNo(i)
1133 END ;
1134 SetPassToNoPass
1135 END DoPass2 ;
1136
1137
1138 (*
1139 DoPassC - parses the sources of all modules necessary to compile
1140 the required module, Main.
1141 *)
1142
1143 PROCEDURE DoPassC ;
1144 VAR
1145 name : Name ;
1146 Sym : CARDINAL ;
1147 i : CARDINAL ;
1148 FileName: String ;
1149 BEGIN
1150 SetPassToPassC ;
1151 i := 1 ;
1152 Sym := GetModuleNo(i) ;
1153 WHILE Sym#NulSym DO
1154 FileName := GetDefinitionModuleFile(Sym) ;
1155 IF FileName#NIL
1156 THEN
1157 IF Debugging
1158 THEN
1159 name := GetSymName(Sym) ;
1160 qprintf1(' Module %a\n', name)
1161 END ;
1162 IF OpenSource(FileName)
1163 THEN
1164 ModuleType := Definition ;
1165 IF NOT PCBuild.CompilationUnit()
1166 THEN
1167 MetaError0('compilation failed') ;
1168 CloseSource ;
1169 RETURN
1170 END ;
1171 CloseSource
1172 ELSE
1173 fprintf1(StdErr, 'failed to open %s\n', FileName) ;
1174 exit(1)
1175 END ;
1176 ModuleType := Implementation
1177 ELSE
1178 ModuleType := Program
1179 END ;
1180 FileName := GetModuleFile(Sym) ;
1181 IF FileName#NIL
1182 THEN
1183 IF Debugging
1184 THEN
1185 name := GetSymName(Sym) ;
1186 qprintf1(' Module %a\n', name)
1187 END ;
1188 IF OpenSource(FileName)
1189 THEN
1190 IF NOT PCBuild.CompilationUnit()
1191 THEN
1192 MetaError0('compilation failed') ;
1193 CloseSource ;
1194 RETURN
1195 END ;
1196 CloseSource
1197 ELSE
1198 fprintf1(StdErr, 'failed to open %s\n', FileName) ;
1199 exit(1)
1200 END
1201 END ;
1202 INC(i) ;
1203 Sym := GetModuleNo(i)
1204 END ;
1205 PCSymBuild.ResolveConstTypes ;
1206 ResolveConstructorTypes ;
1207 SanityCheckConstants ;
1208 SetPassToNoPass
1209 END DoPassC ;
1210
1211
1212 (*
1213 DoPass3 - parses the sources of all modules necessary to compile
1214 the required module, Main.
1215 *)
1216
1217 PROCEDURE DoPass3 ;
1218 VAR
1219 Main,
1220 Sym : CARDINAL ;
1221 i : CARDINAL ;
1222 FileName: String ;
1223 BEGIN
1224 SetPassToPass3 ;
1225 Main := GetMainModule() ;
1226 i := 1 ;
1227 Sym := GetModuleNo(i) ;
1228 WHILE Sym#NulSym DO
1229 FileName := GetDefinitionModuleFile(Sym) ;
1230 IF FileName#NIL
1231 THEN
1232 IF OpenSource(FileName)
1233 THEN
1234 ModuleType := Definition ;
1235 IF NOT P3Build.CompilationUnit()
1236 THEN
1237 MetaError0('compilation failed') ;
1238 CloseSource ;
1239 RETURN
1240 END ;
1241 CloseSource
1242 ELSE
1243 fprintf1(StdErr, 'failed to open %s\n', FileName) ;
1244 exit(1)
1245 END ;
1246 ModuleType := Implementation
1247 ELSE
1248 ModuleType := Program
1249 END ;
1250 FileName := GetModuleFile(Sym) ;
1251 IF FileName#NIL
1252 THEN
1253 IF OpenSource(FileName)
1254 THEN
1255 IF (Main=Sym) OR WholeProgram
1256 THEN
1257 IF NOT P3Build.CompilationUnit()
1258 THEN
1259 MetaError0('compilation failed') ;
1260 CloseSource ;
1261 RETURN
1262 END
1263 ELSE
1264 (*
1265 not the main module .mod therefore must be implementing
1266 a hidden type - we dont want to generate any
1267 StatementSequence quadrupes but we do want to build TYPEs
1268 and ConstExpressions.
1269 *)
1270 SetPassToNoPass ;
1271 SetPassToPassHidden ;
1272 IF NOT PHBuild.CompilationUnit()
1273 THEN
1274 MetaError0('compilation failed') ;
1275 CloseSource ;
1276 RETURN
1277 END ;
1278 SetPassToNoPass ;
1279 SetPassToPass3
1280 END ;
1281 CloseSource
1282 ELSE
1283 fprintf1(StdErr, 'failed to open %s\n', FileName) ;
1284 exit(1)
1285 END
1286 END ;
1287 INC(i) ;
1288 Sym := GetModuleNo(i)
1289 END ;
1290 SetPassToNoPass
1291 END DoPass3 ;
1292
1293
1294 BEGIN
1295 ModuleType := None ;
1296 DepContent := NIL ;
1297 DepOutput := NIL
1298 END M2Comp.