Line data Source code
1 : /* do not edit automatically generated by mc from M2Comp. */
2 : /* M2Comp.mod continually calls the compiler for every source file.
3 :
4 : Copyright (C) 2001-2026 Free Software Foundation, Inc.
5 : Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>.
6 :
7 : This file is part of GNU Modula-2.
8 :
9 : GNU Modula-2 is free software; you can redistribute it and/or modify
10 : it under the terms of the GNU General Public License as published by
11 : the Free Software Foundation; either version 3, or (at your option)
12 : any later version.
13 :
14 : GNU Modula-2 is distributed in the hope that it will be useful, but
15 : WITHOUT ANY WARRANTY; without even the implied warranty of
16 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 : General Public License for more details.
18 :
19 : You should have received a copy of the GNU General Public License
20 : along with GNU Modula-2; see the file COPYING3. If not see
21 : <http://www.gnu.org/licenses/>. */
22 :
23 : #include "config.h"
24 : #include "system.h"
25 : #include "gcc-consolidation.h"
26 :
27 : #include <stdbool.h>
28 : # if !defined (PROC_D)
29 : # define PROC_D
30 : typedef void (*PROC_t) (void);
31 : typedef struct { PROC_t proc; } PROC;
32 : # endif
33 :
34 : # if !defined (TRUE)
35 : # define TRUE (1==1)
36 : # endif
37 :
38 : # if !defined (FALSE)
39 : # define FALSE (1==0)
40 : # endif
41 :
42 : # include "Gmcrts.h"
43 : #if defined(__cplusplus)
44 : # undef NULL
45 : # define NULL 0
46 : #endif
47 : #define _M2Comp_C
48 :
49 : #include "GM2Comp.h"
50 : # include "GM2Pass.h"
51 : # include "GM2Reserved.h"
52 : # include "GM2Search.h"
53 : # include "GM2Code.h"
54 : # include "GM2LexBuf.h"
55 : # include "GM2FileName.h"
56 : # include "GM2Preprocess.h"
57 : # include "Glibc.h"
58 : # include "GM2Error.h"
59 : # include "GM2MetaError.h"
60 : # include "GFormatStrings.h"
61 : # include "GP0SymBuild.h"
62 : # include "GM2Debug.h"
63 : # include "Gm2flex.h"
64 : # include "Gm2block.h"
65 : # include "GP0SyntaxCheck.h"
66 : # include "GP1Build.h"
67 : # include "GP2Build.h"
68 : # include "GPCBuild.h"
69 : # include "GP3Build.h"
70 : # include "GPHBuild.h"
71 : # include "GPCSymBuild.h"
72 : # include "GDynamicStrings.h"
73 : # include "GM2Diagnostic.h"
74 : # include "GM2Defaults.h"
75 : # include "GEnvironment.h"
76 : # include "GArgs.h"
77 : # include "GTimeString.h"
78 : # include "GFpuIO.h"
79 : # include "GM2StateCheck.h"
80 : # include "GP2SymBuild.h"
81 : # include "GP3SymBuild.h"
82 : # include "GM2Batch.h"
83 : # include "GSymbolTable.h"
84 : # include "GFIO.h"
85 : # include "GSFIO.h"
86 : # include "GNameKey.h"
87 : # include "GM2Printf.h"
88 : # include "GM2Quiet.h"
89 : # include "GM2Options.h"
90 : # include "GPathName.h"
91 : # include "GLists.h"
92 : # include "GIndexing.h"
93 :
94 : # define Debugging false
95 : static enum {M2Comp_None, M2Comp_Definition, M2Comp_Implementation, M2Comp_Program} ModuleType;
96 : static Indexing_Index DepContent;
97 : static DynamicStrings_String DepOutput;
98 :
99 : /*
100 : compile - compile the filename.
101 : */
102 :
103 : extern "C" void M2Comp_compile (void * filename);
104 :
105 : /*
106 : CompilingDefinitionModule - returns true if the current module being
107 : compiled is a definition module.
108 : */
109 :
110 : extern "C" bool M2Comp_CompilingDefinitionModule (void);
111 :
112 : /*
113 : CompilingImplementationModule - returns true if the current module being
114 : compiled is an implementation module.
115 : */
116 :
117 : extern "C" bool M2Comp_CompilingImplementationModule (void);
118 :
119 : /*
120 : CompilingProgramModule - returns true if the current module being
121 : compiled is a program module.
122 : */
123 :
124 : extern "C" bool M2Comp_CompilingProgramModule (void);
125 :
126 : /*
127 : NeedToParseImplementation -
128 : */
129 :
130 : static bool NeedToParseImplementation (unsigned int sym);
131 :
132 : /*
133 : GenerateDefDependency - generate a single dependency for the definition module
134 : providing that it can be found and is not blocked by -MM.
135 : */
136 :
137 : static void GenerateDefDependency (unsigned int module);
138 :
139 : /*
140 : GenerateDependenciesFromImport - lookup the module associated with the import
141 : and call GenerateDefDependency.
142 : */
143 :
144 : static void GenerateDependenciesFromImport (unsigned int import);
145 :
146 : /*
147 : GenerateDependenciesFromList - iterative over the import lists and for
148 : each module issue a dependency.
149 : */
150 :
151 : static void GenerateDependenciesFromList (Lists_List dep);
152 :
153 : /*
154 : GenerateDependencies - generate a list of dependencies for the main module where
155 : the source code is found in sourcefile.
156 : */
157 :
158 : static void GenerateDependencies (void);
159 :
160 : /*
161 : Compile - compile file, s, using a 5 pass technique.
162 : */
163 :
164 : static void Compile (DynamicStrings_String s);
165 :
166 : /*
167 : PopulateResource -
168 : */
169 :
170 : static void PopulateResource (void);
171 :
172 : /*
173 : ExamineHeader - examines up until the ';', '[' or eof and determines if the source file
174 : is a program, implementation/definition module.
175 : */
176 :
177 : static void ExamineHeader (void * *name, bool *isdefimp, bool *module);
178 :
179 : /*
180 : ExamineCompilationUnit - opens the source file to obtain the module name and kind of module.
181 : */
182 :
183 : static unsigned int ExamineCompilationUnit (void);
184 :
185 : /*
186 : PeepInto - peeps into source, s, and initializes a definition/implementation or
187 : program module accordingly.
188 : */
189 :
190 : static void PeepInto (DynamicStrings_String s);
191 :
192 : /*
193 : qprintLibName - print the libname.
194 : */
195 :
196 : static void qprintLibName (DynamicStrings_String LibName);
197 :
198 : /*
199 : CreateFileStem - create a stem using the template LibName_ModuleName.
200 : */
201 :
202 : static DynamicStrings_String CreateFileStem (DynamicStrings_String SymName, DynamicStrings_String LibName);
203 : static DynamicStrings_String BaseName (DynamicStrings_String Path, bool CutExt);
204 :
205 : /*
206 : IsLibrary - return TRUE if line contains a library module.
207 : */
208 :
209 : static bool IsLibrary (DynamicStrings_String line);
210 :
211 : /*
212 : IsUnique - return TRUE if line is unique in array content.
213 : */
214 :
215 : static bool IsUnique (Indexing_Index content, DynamicStrings_String line);
216 :
217 : /*
218 : Append - append line to array content.
219 : */
220 :
221 : static void Append (Indexing_Index content, DynamicStrings_String line);
222 :
223 : /*
224 : MergeDep - if line is unique in array content then append.
225 : Check to see (and ignore) if line is a library module and -MM
226 : is present.
227 : */
228 :
229 : static void MergeDep (Indexing_Index content, DynamicStrings_String line);
230 :
231 : /*
232 : splitLine - split a line into words separated by spaces
233 : and call MergeDep on each word.
234 : */
235 :
236 : static void splitLine (Indexing_Index content, DynamicStrings_String line);
237 :
238 : /*
239 : MergeDeps - foreach dependency in ChildDep do
240 : add dependency to ChildDep if not already present.
241 : ignore all ChildDep if -MM and libname # "".
242 : */
243 :
244 : static void MergeDeps (Indexing_Index content, DynamicStrings_String ChildDep, DynamicStrings_String LibName);
245 :
246 : /*
247 : GetRuleTarget - return the rule target which is derived from the -MT arg
248 : or -o arg or filename.mod.
249 : */
250 :
251 : static DynamicStrings_String GetRuleTarget (DynamicStrings_String filename);
252 :
253 : /*
254 : ReadDepContents - reads the contents of file dep into a dynamic array
255 : and return the array. The file will be split into words
256 : and each word stored as an entry in the array.
257 : */
258 :
259 : static Indexing_Index ReadDepContents (DynamicStrings_String filename, DynamicStrings_String dep);
260 :
261 : /*
262 : WriteDep - write the dependencies and target to file out.
263 : */
264 :
265 : static void WriteDep (Indexing_Index contents, FIO_File out);
266 :
267 : /*
268 : WritePhonyDep - write the dependencies and target to file out.
269 : */
270 :
271 : static void WritePhonyDep (Indexing_Index contents, FIO_File out);
272 :
273 : /*
274 : WriteDepContents - write the dynamic array to filename dep (or StdOut) if
275 : the GetMF file is NIL.
276 : */
277 :
278 : static void WriteDepContents (DynamicStrings_String dep, Indexing_Index contents);
279 :
280 : /*
281 : CreateDepFilename - return a dependency filename associated with filename or use GetMF.
282 : */
283 :
284 : static DynamicStrings_String CreateDepFilename (DynamicStrings_String filename);
285 :
286 : /*
287 : Pass0CheckDef -
288 : */
289 :
290 : static bool Pass0CheckDef (unsigned int sym);
291 :
292 : /*
293 : Pass0CheckMod -
294 : */
295 :
296 : static bool Pass0CheckMod (unsigned int sym, DynamicStrings_String PPSource);
297 :
298 : /*
299 : DoPass0 -
300 : */
301 :
302 : static void DoPass0 (DynamicStrings_String filename);
303 :
304 : /*
305 : DoPass1 - parses the sources of all modules necessary to compile
306 : the required module, Main.
307 : */
308 :
309 : static void DoPass1 (void);
310 :
311 : /*
312 : DoPass2 - parses the sources of all modules necessary to compile
313 : the required module, Main.
314 : */
315 :
316 : static void DoPass2 (void);
317 :
318 : /*
319 : DoPassC - parses the sources of all modules necessary to compile
320 : the required module, Main.
321 : */
322 :
323 : static void DoPassC (void);
324 :
325 : /*
326 : DoPass3 - parses the sources of all modules necessary to compile
327 : the required module, Main.
328 : */
329 :
330 : static void DoPass3 (void);
331 :
332 :
333 : /*
334 : NeedToParseImplementation -
335 : */
336 :
337 170658 : static bool NeedToParseImplementation (unsigned int sym)
338 : {
339 170658 : return ((((SymbolTable_IsDefImp (sym)) && (SymbolTable_IsHiddenTypeDeclared (sym))) && M2Options_ExtendedOpaque) || ((SymbolTable_IsDefImp (sym)) && (SymbolTable_IsBuiltinInModule (sym)))) || (M2Options_WholeProgram && (! (SymbolTable_IsDefinitionForC (sym))));
340 : /* static analysis guarentees a RETURN statement will be used before here. */
341 : __builtin_unreachable ();
342 : }
343 :
344 :
345 : /*
346 : GenerateDefDependency - generate a single dependency for the definition module
347 : providing that it can be found and is not blocked by -MM.
348 : */
349 :
350 0 : static void GenerateDefDependency (unsigned int module)
351 : {
352 0 : DynamicStrings_String stem;
353 0 : DynamicStrings_String fullpath;
354 0 : DynamicStrings_String named;
355 :
356 0 : stem = DynamicStrings_InitStringCharStar (NameKey_KeyToCharStar (SymbolTable_GetSymName (module)));
357 0 : named = static_cast<DynamicStrings_String> (NULL);
358 0 : if (M2Search_FindSourceDefFile (stem, &fullpath, &named))
359 : {
360 : /* avoid gcc warning by using compound statement even if not strictly necessary. */
361 0 : if ((DynamicStrings_EqualArray (named, (const char *) "", 0)) || (! (M2Options_GetMM ())))
362 : {
363 0 : MergeDep (DepContent, fullpath);
364 : }
365 : else
366 : {
367 0 : fullpath = DynamicStrings_KillString (fullpath);
368 : }
369 : }
370 0 : stem = DynamicStrings_KillString (stem);
371 0 : named = DynamicStrings_KillString (named);
372 0 : }
373 :
374 :
375 : /*
376 : GenerateDependenciesFromImport - lookup the module associated with the import
377 : and call GenerateDefDependency.
378 : */
379 :
380 0 : static void GenerateDependenciesFromImport (unsigned int import)
381 : {
382 0 : unsigned int module;
383 :
384 0 : M2Debug_Assert (SymbolTable_IsImport (import));
385 0 : module = SymbolTable_GetImportModule (import);
386 0 : GenerateDefDependency (module);
387 0 : }
388 :
389 :
390 : /*
391 : GenerateDependenciesFromList - iterative over the import lists and for
392 : each module issue a dependency.
393 : */
394 :
395 0 : static void GenerateDependenciesFromList (Lists_List dep)
396 : {
397 0 : Lists_List importList;
398 0 : unsigned int import;
399 0 : unsigned int i;
400 0 : unsigned int n;
401 0 : unsigned int j;
402 0 : unsigned int m;
403 :
404 0 : n = Lists_NoOfItemsInList (dep);
405 0 : i = 1;
406 0 : while (i <= n)
407 : {
408 0 : import = static_cast<unsigned int> (Lists_GetItemFromList (dep, i));
409 0 : if (SymbolTable_IsImportStatement (import))
410 : {
411 0 : importList = SymbolTable_GetImportStatementList (import);
412 0 : j = 1;
413 0 : m = Lists_NoOfItemsInList (importList);
414 0 : while (j <= m)
415 : {
416 0 : import = static_cast<unsigned int> (Lists_GetItemFromList (importList, j));
417 0 : GenerateDependenciesFromImport (import);
418 0 : j += 1;
419 : }
420 : }
421 : else
422 : {
423 0 : GenerateDependenciesFromImport (import);
424 : }
425 0 : i += 1;
426 : }
427 0 : }
428 :
429 :
430 : /*
431 : GenerateDependencies - generate a list of dependencies for the main module where
432 : the source code is found in sourcefile.
433 : */
434 :
435 0 : static void GenerateDependencies (void)
436 : {
437 0 : if (SymbolTable_IsDefImp (SymbolTable_GetMainModule ()))
438 : {
439 0 : GenerateDependenciesFromList (SymbolTable_GetModuleDefImportStatementList (SymbolTable_GetMainModule ()));
440 0 : GenerateDefDependency (SymbolTable_GetMainModule ());
441 : }
442 0 : GenerateDependenciesFromList (SymbolTable_GetModuleModImportStatementList (SymbolTable_GetMainModule ()));
443 0 : WriteDepContents (DepOutput, DepContent);
444 0 : }
445 :
446 :
447 : /*
448 : Compile - compile file, s, using a 5 pass technique.
449 : */
450 :
451 16979 : static void Compile (DynamicStrings_String s)
452 : {
453 16979 : DoPass0 (s);
454 16937 : M2Error_FlushWarnings ();
455 16937 : M2Error_FlushErrors ();
456 16847 : M2LexBuf_ResetForNewPass ();
457 16847 : M2Error_ResetErrorScope ();
458 16847 : M2Quiet_qprintf0 ((const char *) "Pass 1: scopes, enumerated types, imports and exports\\n", 55);
459 16847 : DoPass1 ();
460 16841 : M2Error_FlushWarnings ();
461 16841 : M2Error_FlushErrors ();
462 16817 : if ((M2Options_GetM ()) || (M2Options_GetMM ()))
463 : {
464 0 : GenerateDependencies ();
465 : }
466 16817 : if (! M2Options_PPonly)
467 : {
468 16817 : M2Quiet_qprintf0 ((const char *) "Pass 2: constants and types\\n", 29);
469 16817 : M2LexBuf_ResetForNewPass ();
470 16817 : M2Error_ResetErrorScope ();
471 16817 : DoPass2 ();
472 16799 : M2Error_FlushWarnings ();
473 16799 : M2Error_FlushErrors ();
474 16715 : M2Quiet_qprintf0 ((const char *) "Pass C: aggregate constants\\n", 29);
475 16715 : M2LexBuf_ResetForNewPass ();
476 16715 : M2Error_ResetErrorScope ();
477 16715 : DoPassC ();
478 16685 : M2Error_FlushWarnings ();
479 16685 : M2Error_FlushErrors ();
480 16595 : M2Quiet_qprintf0 ((const char *) "Pass 3: quadruple generation\\n", 30);
481 16595 : M2LexBuf_ResetForNewPass ();
482 16595 : M2Error_ResetErrorScope ();
483 16595 : DoPass3 ();
484 16376 : M2Error_FlushWarnings ();
485 16376 : M2Error_FlushErrors ();
486 15953 : M2Quiet_qprintf0 ((const char *) "Pass 4: gcc tree generation\\n", 29);
487 15953 : M2Code_Code ();
488 15665 : M2Error_FlushWarnings ();
489 15665 : M2Error_FlushErrors ();
490 : }
491 15425 : }
492 :
493 :
494 : /*
495 : PopulateResource -
496 : */
497 :
498 15425 : static void PopulateResource (void)
499 : {
500 15425 : M2Diagnostic_Diagnostic StatsMemDiag;
501 :
502 15425 : if (M2Options_MemReport)
503 : {
504 0 : StatsMemDiag = M2Diagnostic_InitMemDiagnostic ((const char *) "M2Comp:statistics", 17, (const char *) "total source lines {1d} total constants {2d} total types {3d}", 61);
505 0 : M2Diagnostic_MemSet (StatsMemDiag, 1, m2flex_GetTotalLines ());
506 0 : M2Diagnostic_MemSet (StatsMemDiag, 2, m2block_GetTotalConstants ());
507 0 : M2Diagnostic_MemSet (StatsMemDiag, 3, m2block_GetGlobalTypes ());
508 : }
509 15425 : }
510 :
511 :
512 : /*
513 : ExamineHeader - examines up until the ';', '[' or eof and determines if the source file
514 : is a program, implementation/definition module.
515 : */
516 :
517 16978 : static void ExamineHeader (void * *name, bool *isdefimp, bool *module)
518 : {
519 : /* Stop if we see one of eof ';' '['. */
520 33950 : while (((M2LexBuf_currenttoken != M2Reserved_eoftok) && (M2LexBuf_currenttoken != M2Reserved_semicolontok)) && (M2LexBuf_currenttoken != M2Reserved_lsbratok))
521 : {
522 16972 : if ((*name) == NULL)
523 : {
524 16972 : if ((M2LexBuf_currenttoken == M2Reserved_implementationtok) || (M2LexBuf_currenttoken == M2Reserved_definitiontok))
525 : {
526 4941 : (*isdefimp) = true;
527 4941 : M2LexBuf_GetToken ();
528 : }
529 16972 : if (M2LexBuf_currenttoken == M2Reserved_moduletok)
530 : {
531 16972 : (*module) = true;
532 16972 : M2LexBuf_GetToken ();
533 16972 : if (M2LexBuf_currenttoken == M2Reserved_identtok)
534 : {
535 16972 : (*name) = M2LexBuf_currentstring;
536 : }
537 : }
538 : }
539 16972 : M2LexBuf_GetToken ();
540 : }
541 16978 : }
542 :
543 :
544 : /*
545 : ExamineCompilationUnit - opens the source file to obtain the module name and kind of module.
546 : */
547 :
548 16978 : static unsigned int ExamineCompilationUnit (void)
549 : {
550 16978 : DynamicStrings_String Message;
551 16978 : void * name;
552 16978 : bool module;
553 16978 : bool isdefimp;
554 :
555 16978 : name = NULL;
556 16978 : isdefimp = false; /* default to program module */
557 16978 : module = false; /* Seen module keyword? */
558 16978 : ExamineHeader (&name, &isdefimp, &module); /* Seen module keyword? */
559 16978 : if (name == NULL)
560 : {
561 6 : if (module)
562 : {
563 0 : Message = M2MetaError_MetaString0 (DynamicStrings_InitString ((const char *) "no {%kMODULE} keyword seen", 26));
564 : }
565 : else
566 : {
567 6 : Message = M2MetaError_MetaString0 (DynamicStrings_InitString ((const char *) "no module ident seen", 20));
568 : }
569 6 : m2flex_M2Error (DynamicStrings_string (Message));
570 6 : libc_exit (1);
571 : }
572 : else
573 : {
574 : /* The token used is will be overwritten when P0 is underway.
575 : At this point we are determining the module kind and the tokens
576 : read will be discarded (see ReInitialize below). */
577 16972 : if (isdefimp)
578 : {
579 4941 : return M2Batch_MakeImplementationSource (M2LexBuf_BuiltinTokenNo, NameKey_makekey (name));
580 : }
581 : else
582 : {
583 12031 : return M2Batch_MakeProgramSource (M2LexBuf_BuiltinTokenNo, NameKey_makekey (name));
584 : }
585 : }
586 : ReturnException ("/home/worker/buildworker/tiber-lcov/build/gcc/m2/gm2-compiler/M2Comp.def", 20, 1);
587 : __builtin_unreachable ();
588 : }
589 :
590 :
591 : /*
592 : PeepInto - peeps into source, s, and initializes a definition/implementation or
593 : program module accordingly.
594 : */
595 :
596 16979 : static void PeepInto (DynamicStrings_String s)
597 : {
598 16979 : unsigned int mainModule;
599 :
600 16979 : if (M2LexBuf_OpenSource (s))
601 : {
602 16978 : mainModule = ExamineCompilationUnit ();
603 16972 : if (mainModule != SymbolTable_NulSym)
604 : {
605 16972 : SymbolTable_SetMainModule (mainModule);
606 : }
607 16972 : M2LexBuf_CloseSource ();
608 16972 : M2LexBuf_ReInitialize ();
609 : }
610 : else
611 : {
612 1 : M2Printf_fprintf1 (FIO_StdErr, (const char *) "failed to open %s\\n", 19, (const unsigned char *) &s, (sizeof (s)-1));
613 1 : libc_exit (1);
614 : }
615 16972 : }
616 :
617 :
618 : /*
619 : qprintLibName - print the libname.
620 : */
621 :
622 262091 : static void qprintLibName (DynamicStrings_String LibName)
623 : {
624 262091 : if ((LibName != NULL) && (! (DynamicStrings_EqualArray (LibName, (const char *) "", 0))))
625 : {
626 238959 : M2Quiet_qprintf1 ((const char *) " [%s]", 5, (const unsigned char *) &LibName, (sizeof (LibName)-1));
627 : }
628 262091 : }
629 :
630 :
631 : /*
632 : CreateFileStem - create a stem using the template LibName_ModuleName.
633 : */
634 :
635 245189 : static DynamicStrings_String CreateFileStem (DynamicStrings_String SymName, DynamicStrings_String LibName)
636 : {
637 245189 : if ((DynamicStrings_Length (LibName)) > 0)
638 : {
639 238327 : return DynamicStrings_ConCat (DynamicStrings_Dup (LibName), DynamicStrings_ConCat (DynamicStrings_InitStringChar ('_'), SymName));
640 : }
641 : else
642 : {
643 : return SymName;
644 : }
645 : /* static analysis guarentees a RETURN statement will be used before here. */
646 : __builtin_unreachable ();
647 : }
648 :
649 0 : static DynamicStrings_String BaseName (DynamicStrings_String Path, bool CutExt)
650 : {
651 0 : int ext;
652 0 : int basename;
653 :
654 : /*
655 : Return basename of path. CutExt determines whether the .extension
656 : should be removed.
657 : */
658 0 : basename = DynamicStrings_RIndex (Path, '/', 0);
659 0 : if (basename == -1)
660 : {
661 : basename = 0;
662 : }
663 : else
664 : {
665 0 : basename = basename+1;
666 : }
667 0 : if (CutExt)
668 : {
669 : /* avoid dangling else. */
670 0 : ext = DynamicStrings_RIndex (Path, '.', 0);
671 0 : if (ext == -1)
672 : {
673 0 : ext = 0;
674 : }
675 : }
676 : else
677 : {
678 : ext = 0;
679 : }
680 0 : return DynamicStrings_Slice (Path, basename, ext);
681 : /* static analysis guarentees a RETURN statement will be used before here. */
682 : __builtin_unreachable ();
683 : }
684 :
685 :
686 : /*
687 : IsLibrary - return TRUE if line contains a library module.
688 : */
689 :
690 0 : static bool IsLibrary (DynamicStrings_String line)
691 : {
692 0 : DynamicStrings_String moduleName;
693 0 : DynamicStrings_String libname;
694 0 : DynamicStrings_String filename;
695 0 : bool result;
696 :
697 0 : result = false;
698 0 : moduleName = BaseName (line, true);
699 0 : filename = static_cast<DynamicStrings_String> (NULL);
700 0 : libname = static_cast<DynamicStrings_String> (NULL);
701 0 : if (M2Search_FindSourceDefFile (moduleName, &filename, &libname))
702 : {
703 0 : moduleName = DynamicStrings_KillString (moduleName);
704 0 : if ((DynamicStrings_Length (libname)) > 0)
705 : {
706 0 : moduleName = BaseName (line, false);
707 0 : line = BaseName (line, false);
708 0 : result = DynamicStrings_Equal (line, moduleName);
709 0 : line = DynamicStrings_KillString (line);
710 : }
711 : }
712 0 : libname = DynamicStrings_KillString (libname);
713 0 : filename = DynamicStrings_KillString (filename);
714 0 : moduleName = DynamicStrings_KillString (moduleName);
715 0 : return result;
716 : /* static analysis guarentees a RETURN statement will be used before here. */
717 : __builtin_unreachable ();
718 : }
719 :
720 :
721 : /*
722 : IsUnique - return TRUE if line is unique in array content.
723 : */
724 :
725 0 : static bool IsUnique (Indexing_Index content, DynamicStrings_String line)
726 : {
727 0 : unsigned int high;
728 0 : unsigned int i;
729 :
730 0 : high = Indexing_HighIndice (content);
731 0 : i = 1;
732 0 : while (i <= high)
733 : {
734 0 : if (DynamicStrings_Equal (line, static_cast<DynamicStrings_String> (Indexing_GetIndice (content, i))))
735 : {
736 : return false;
737 : }
738 0 : i += 1;
739 : }
740 : return true;
741 : /* static analysis guarentees a RETURN statement will be used before here. */
742 : __builtin_unreachable ();
743 : }
744 :
745 :
746 : /*
747 : Append - append line to array content.
748 : */
749 :
750 0 : static void Append (Indexing_Index content, DynamicStrings_String line)
751 : {
752 0 : unsigned int high;
753 :
754 0 : high = Indexing_HighIndice (content);
755 0 : Indexing_PutIndice (content, high+1, reinterpret_cast <void *> (line));
756 0 : }
757 :
758 :
759 : /*
760 : MergeDep - if line is unique in array content then append.
761 : Check to see (and ignore) if line is a library module and -MM
762 : is present.
763 : */
764 :
765 0 : static void MergeDep (Indexing_Index content, DynamicStrings_String line)
766 : {
767 0 : line = DynamicStrings_RemoveWhitePrefix (line);
768 0 : if ((! (DynamicStrings_EqualArray (line, (const char *) "\\", 1))) && ((DynamicStrings_Length (line)) > 0))
769 : {
770 : /* Ignore if -MM and is a library module. */
771 0 : if (! ((M2Options_GetMM ()) && (IsLibrary (line))))
772 : {
773 0 : if (IsUnique (content, line))
774 : {
775 0 : Append (content, line);
776 : }
777 : }
778 : }
779 0 : }
780 :
781 :
782 : /*
783 : splitLine - split a line into words separated by spaces
784 : and call MergeDep on each word.
785 : */
786 :
787 0 : static void splitLine (Indexing_Index content, DynamicStrings_String line)
788 : {
789 0 : DynamicStrings_String word;
790 0 : int space;
791 :
792 0 : do {
793 0 : line = DynamicStrings_RemoveWhitePrefix (line);
794 0 : space = DynamicStrings_Index (line, ' ', 0);
795 0 : if (space > 0)
796 : {
797 0 : word = DynamicStrings_Slice (line, 0, space);
798 0 : word = DynamicStrings_RemoveWhitePrefix (word);
799 0 : if ((DynamicStrings_Length (word)) > 0)
800 : {
801 0 : MergeDep (content, word);
802 : }
803 0 : line = DynamicStrings_Slice (line, space, 0);
804 : }
805 0 : else if (space < 0)
806 : {
807 : /* avoid dangling else. */
808 0 : MergeDep (content, line);
809 : }
810 0 : } while (! (space <= 0));
811 0 : }
812 :
813 :
814 : /*
815 : MergeDeps - foreach dependency in ChildDep do
816 : add dependency to ChildDep if not already present.
817 : ignore all ChildDep if -MM and libname # "".
818 : */
819 :
820 245183 : static void MergeDeps (Indexing_Index content, DynamicStrings_String ChildDep, DynamicStrings_String LibName)
821 : {
822 245183 : DynamicStrings_String line;
823 245183 : FIO_File in;
824 :
825 245183 : if ((content != NULL) && (! ((M2Options_GetMM ()) && ((DynamicStrings_Length (LibName)) > 0))))
826 : {
827 0 : in = SFIO_OpenToRead (ChildDep);
828 0 : if (FIO_IsNoError (in))
829 : {
830 0 : line = SFIO_ReadS (in); /* Skip over first line containing the module object. */
831 0 : while (! (FIO_EOF (in))) /* Skip over first line containing the module object. */
832 : {
833 0 : line = SFIO_ReadS (in);
834 0 : splitLine (content, line);
835 : }
836 : }
837 0 : FIO_Close (in);
838 : }
839 245183 : }
840 :
841 :
842 : /*
843 : GetRuleTarget - return the rule target which is derived from the -MT arg
844 : or -o arg or filename.mod.
845 : */
846 :
847 0 : static DynamicStrings_String GetRuleTarget (DynamicStrings_String filename)
848 : {
849 0 : if ((M2Options_GetDepTarget ()) != NULL)
850 : {
851 0 : return DynamicStrings_InitStringCharStar (M2Options_GetDepTarget ());
852 : }
853 0 : else if ((M2Options_GetMF ()) != NULL)
854 : {
855 : /* avoid dangling else. */
856 0 : return DynamicStrings_InitStringCharStar (M2Options_GetMF ());
857 : }
858 : else
859 : {
860 : /* avoid dangling else. */
861 0 : return DynamicStrings_ConCat (BaseName (filename, true), DynamicStrings_InitString ((const char *) ".o", 2));
862 : }
863 : /* static analysis guarentees a RETURN statement will be used before here. */
864 : __builtin_unreachable ();
865 : }
866 :
867 :
868 : /*
869 : ReadDepContents - reads the contents of file dep into a dynamic array
870 : and return the array. The file will be split into words
871 : and each word stored as an entry in the array.
872 : */
873 :
874 16979 : static Indexing_Index ReadDepContents (DynamicStrings_String filename, DynamicStrings_String dep)
875 : {
876 16979 : Indexing_Index content;
877 16979 : DynamicStrings_String line;
878 16979 : FIO_File in;
879 :
880 16979 : content = static_cast<Indexing_Index> (NULL);
881 16979 : if ((M2Options_GetM ()) || (M2Options_GetMM ()))
882 : {
883 0 : in = SFIO_OpenToRead (dep);
884 : /* The file might not be created (if -MD or -MMD is used as these options
885 : operate without preprocessing) in which case we create an dynamic
886 : array with the source filename and target. */
887 0 : content = Indexing_InitIndex (1);
888 0 : if (((M2Options_GetMD ()) || (M2Options_GetMMD ())) || (! (FIO_IsNoError (in))))
889 : {
890 : /* No preprocessing done therefore create first two lines using
891 : target and source. */
892 0 : Indexing_PutIndice (content, 1, reinterpret_cast <void *> (DynamicStrings_ConCatChar (GetRuleTarget (filename), ':')));
893 0 : Indexing_PutIndice (content, 2, reinterpret_cast <void *> (DynamicStrings_Dup (filename)));
894 : }
895 : else
896 : {
897 : /* Preprocessing (using cc1) has created one for us, so we read it. */
898 0 : while (! (FIO_EOF (in)))
899 : {
900 0 : line = SFIO_ReadS (in);
901 0 : splitLine (content, line);
902 : }
903 : }
904 0 : FIO_Close (in);
905 : }
906 16979 : return content;
907 : /* static analysis guarentees a RETURN statement will be used before here. */
908 : __builtin_unreachable ();
909 : }
910 :
911 :
912 : /*
913 : WriteDep - write the dependencies and target to file out.
914 : */
915 :
916 0 : static void WriteDep (Indexing_Index contents, FIO_File out)
917 : {
918 0 : unsigned int i;
919 0 : unsigned int h;
920 0 : DynamicStrings_String line;
921 :
922 0 : i = 1;
923 0 : h = Indexing_HighIndice (contents);
924 0 : while (i <= h)
925 : {
926 0 : line = static_cast<DynamicStrings_String> (Indexing_GetIndice (contents, i));
927 0 : line = DynamicStrings_RemoveWhitePrefix (line);
928 0 : if ((DynamicStrings_Length (line)) > 0)
929 : {
930 0 : if (i == 1)
931 : {
932 : /* avoid dangling else. */
933 : /* First line is always the target. */
934 0 : if ((M2Options_GetDepTarget ()) != NULL)
935 : {
936 0 : line = DynamicStrings_ConCatChar (DynamicStrings_InitStringCharStar (M2Options_GetDepTarget ()), ':');
937 : }
938 : }
939 0 : else if (i > 1)
940 : {
941 : /* avoid dangling else. */
942 0 : FIO_WriteChar (out, ' ');
943 : }
944 0 : line = SFIO_WriteS (out, line);
945 0 : if (i < h)
946 : {
947 0 : FIO_WriteChar (out, ' ');
948 0 : FIO_WriteChar (out, '\\');
949 : }
950 0 : FIO_WriteLine (out);
951 : }
952 0 : i += 1;
953 : }
954 0 : }
955 :
956 :
957 : /*
958 : WritePhonyDep - write the dependencies and target to file out.
959 : */
960 :
961 0 : static void WritePhonyDep (Indexing_Index contents, FIO_File out)
962 : {
963 0 : unsigned int i;
964 0 : unsigned int h;
965 0 : DynamicStrings_String line;
966 :
967 : /* The first line is always the target and the second line is always
968 : the top level source file. */
969 0 : i = 3;
970 0 : h = Indexing_HighIndice (contents);
971 0 : while (i <= h)
972 : {
973 0 : line = static_cast<DynamicStrings_String> (Indexing_GetIndice (contents, i));
974 0 : line = DynamicStrings_RemoveWhitePrefix (line);
975 0 : if ((DynamicStrings_Length (line)) > 0)
976 : {
977 0 : line = SFIO_WriteS (out, line);
978 0 : FIO_WriteChar (out, ':');
979 0 : FIO_WriteLine (out);
980 : }
981 0 : i += 1;
982 : }
983 0 : }
984 :
985 :
986 : /*
987 : WriteDepContents - write the dynamic array to filename dep (or StdOut) if
988 : the GetMF file is NIL.
989 : */
990 :
991 0 : static void WriteDepContents (DynamicStrings_String dep, Indexing_Index contents)
992 : {
993 0 : FIO_File out;
994 :
995 0 : if ((contents != NULL) && ((M2Options_GetM ()) || (M2Options_GetMM ())))
996 : {
997 0 : if ((M2Options_GetMF ()) == NULL)
998 : {
999 0 : out = FIO_StdOut;
1000 0 : dep = M2Preprocess_OnExitDelete (dep);
1001 : }
1002 : else
1003 : {
1004 0 : out = SFIO_OpenToWrite (dep);
1005 : }
1006 0 : if (FIO_IsNoError (out))
1007 : {
1008 0 : WriteDep (contents, out);
1009 0 : if (M2Options_GetMP ())
1010 : {
1011 0 : WritePhonyDep (contents, out);
1012 : }
1013 : }
1014 0 : if ((M2Options_GetMF ()) == NULL)
1015 : {
1016 0 : FIO_FlushOutErr ();
1017 : }
1018 : else
1019 : {
1020 0 : FIO_Close (out);
1021 : }
1022 0 : contents = Indexing_KillIndex (contents);
1023 : }
1024 0 : }
1025 :
1026 :
1027 : /*
1028 : CreateDepFilename - return a dependency filename associated with filename or use GetMF.
1029 : */
1030 :
1031 16979 : static DynamicStrings_String CreateDepFilename (DynamicStrings_String filename)
1032 : {
1033 16979 : DynamicStrings_String depfile;
1034 :
1035 16979 : if ((M2Options_GetMF ()) == NULL)
1036 : {
1037 16979 : depfile = M2Preprocess_MakeSaveTempsFileNameExt (filename, DynamicStrings_InitString ((const char *) ".d", 2));
1038 16979 : return M2Preprocess_OnExitDelete (depfile);
1039 : }
1040 : else
1041 : {
1042 0 : return DynamicStrings_InitStringCharStar (M2Options_GetMF ());
1043 : }
1044 : /* static analysis guarentees a RETURN statement will be used before here. */
1045 : __builtin_unreachable ();
1046 : }
1047 :
1048 :
1049 : /*
1050 : Pass0CheckDef -
1051 : */
1052 :
1053 187655 : static bool Pass0CheckDef (unsigned int sym)
1054 : {
1055 187655 : DynamicStrings_String ChildDep;
1056 187655 : DynamicStrings_String SymName;
1057 187655 : DynamicStrings_String FileName;
1058 187655 : DynamicStrings_String LibName;
1059 :
1060 187655 : LibName = static_cast<DynamicStrings_String> (NULL);
1061 187655 : FileName = static_cast<DynamicStrings_String> (NULL);
1062 187655 : SymName = DynamicStrings_InitStringCharStar (NameKey_KeyToCharStar (SymbolTable_GetSymName (sym)));
1063 187655 : if (SymbolTable_IsDefImp (sym))
1064 : {
1065 175624 : if (M2Search_FindSourceDefFile (SymName, &FileName, &LibName))
1066 : {
1067 175601 : ModuleType = M2Comp_Definition;
1068 175601 : ChildDep = M2Preprocess_MakeSaveTempsFileNameExt (CreateFileStem (SymName, LibName), DynamicStrings_InitString ((const char *) ".def.d", 6));
1069 175601 : if (M2LexBuf_OpenSource (M2Batch_AssociateDefinition (M2Preprocess_PreprocessModule (FileName, false, true, ChildDep), sym)))
1070 : {
1071 175601 : if (! (P0SyntaxCheck_CompilationUnit ()))
1072 : {
1073 6 : M2Error_WriteFormat0 ((const char *) "compilation failed", 18);
1074 6 : M2LexBuf_CloseSource ();
1075 6 : SymName = DynamicStrings_KillString (SymName);
1076 6 : FileName = DynamicStrings_KillString (FileName);
1077 6 : LibName = DynamicStrings_KillString (LibName);
1078 6 : return false;
1079 : }
1080 175595 : M2Quiet_qprintf2 ((const char *) " Module %-20s : %s", 20, (const unsigned char *) &SymName, (sizeof (SymName)-1), (const unsigned char *) &FileName, (sizeof (FileName)-1));
1081 175595 : qprintLibName (LibName);
1082 175595 : SymbolTable_PutLibName (sym, NameKey_makekey (DynamicStrings_string (LibName)));
1083 175595 : if (SymbolTable_IsDefinitionForC (sym))
1084 : {
1085 13580 : M2Quiet_qprintf0 ((const char *) " (for C)", 8);
1086 : }
1087 175595 : if (SymbolTable_IsDefLink (sym))
1088 : {
1089 83372 : M2Quiet_qprintf0 ((const char *) " (linking)", 10);
1090 : }
1091 175595 : M2Quiet_qprintf0 ((const char *) "\\n", 2);
1092 175595 : M2LexBuf_CloseSource ();
1093 175595 : MergeDeps (DepContent, ChildDep, LibName);
1094 : }
1095 : else
1096 : {
1097 : /* Unrecoverable error. */
1098 0 : M2MetaError_MetaErrorString1 (FormatStrings_Sprintf1 (DynamicStrings_InitString ((const char *) "file {%%1EUAF%s} containing module {%%1a} cannot be found {%%1&Ds}", 66), (const unsigned char *) &FileName, (sizeof (FileName)-1)), sym);
1099 : }
1100 : }
1101 : else
1102 : {
1103 23 : M2MetaError_MetaError1 ((const char *) "the file containing the definition module {%1EMAa} cannot be found {%1&Ds}", 74, sym);
1104 : }
1105 175595 : ModuleType = M2Comp_Implementation;
1106 : }
1107 : else
1108 : {
1109 12031 : ModuleType = M2Comp_Program;
1110 : }
1111 187626 : SymName = DynamicStrings_KillString (SymName);
1112 187626 : FileName = DynamicStrings_KillString (FileName);
1113 187626 : LibName = DynamicStrings_KillString (LibName);
1114 187626 : return true;
1115 : /* static analysis guarentees a RETURN statement will be used before here. */
1116 : __builtin_unreachable ();
1117 : }
1118 :
1119 :
1120 : /*
1121 : Pass0CheckMod -
1122 : */
1123 :
1124 187626 : static bool Pass0CheckMod (unsigned int sym, DynamicStrings_String PPSource)
1125 : {
1126 187626 : unsigned int Main;
1127 187626 : DynamicStrings_String ChildDep;
1128 187626 : DynamicStrings_String SymName;
1129 187626 : DynamicStrings_String FileName;
1130 187626 : DynamicStrings_String LibName;
1131 :
1132 187626 : SymName = DynamicStrings_InitStringCharStar (NameKey_KeyToCharStar (SymbolTable_GetSymName (sym)));
1133 187626 : FileName = static_cast<DynamicStrings_String> (NULL);
1134 187626 : LibName = static_cast<DynamicStrings_String> (NULL);
1135 187626 : Main = SymbolTable_GetMainModule ();
1136 187626 : if ((Main == sym) || (NeedToParseImplementation (sym)))
1137 : {
1138 : /* avoid dangling else. */
1139 : /* Only need to read implementation module if hidden types are
1140 : declared or it is the main module. */
1141 22122 : if (Main == sym)
1142 : {
1143 16968 : FileName = DynamicStrings_Dup (PPSource);
1144 16968 : LibName = DynamicStrings_InitStringCharStar (M2Options_GetM2Prefix ());
1145 16968 : SymbolTable_PutLibName (sym, NameKey_makekey (DynamicStrings_string (LibName)));
1146 : }
1147 : else
1148 : {
1149 5154 : if (M2Search_FindSourceModFile (SymName, &FileName, &LibName))
1150 : {
1151 4986 : ChildDep = M2Preprocess_MakeSaveTempsFileNameExt (CreateFileStem (SymName, LibName), DynamicStrings_InitString ((const char *) ".mod.d", 6));
1152 4986 : FileName = M2Preprocess_PreprocessModule (FileName, false, true, ChildDep);
1153 4986 : SymbolTable_PutLibName (sym, NameKey_makekey (DynamicStrings_string (LibName)));
1154 4986 : MergeDeps (DepContent, ChildDep, LibName);
1155 : }
1156 : else
1157 : {
1158 168 : M2Quiet_qprintf1 ((const char *) " Module %-20s : implementation source file not found\\n", 56, (const unsigned char *) &SymName, (sizeof (SymName)-1));
1159 : }
1160 : }
1161 22122 : if (FileName != NULL)
1162 : {
1163 : /* avoid gcc warning by using compound statement even if not strictly necessary. */
1164 21954 : if (M2LexBuf_OpenSource (M2Batch_AssociateModule (DynamicStrings_Dup (FileName), sym)))
1165 : {
1166 21954 : if (! (P0SyntaxCheck_CompilationUnit ()))
1167 : {
1168 48 : M2Error_WriteFormat0 ((const char *) "compilation failed", 18);
1169 48 : M2LexBuf_CloseSource ();
1170 48 : SymName = DynamicStrings_KillString (SymName);
1171 48 : FileName = DynamicStrings_KillString (FileName);
1172 48 : LibName = DynamicStrings_KillString (LibName);
1173 48 : return false;
1174 : }
1175 21894 : M2Quiet_qprintf2 ((const char *) " Module %-20s : %s", 20, (const unsigned char *) &SymName, (sizeof (SymName)-1), (const unsigned char *) &FileName, (sizeof (FileName)-1));
1176 21894 : qprintLibName (LibName);
1177 21894 : if (SymbolTable_IsModLink (sym))
1178 : {
1179 2768 : M2Quiet_qprintf0 ((const char *) " (linking)", 10);
1180 : }
1181 21894 : M2Quiet_qprintf0 ((const char *) "\\n", 2);
1182 21894 : M2LexBuf_CloseSource ();
1183 : }
1184 : else
1185 : {
1186 : /* It is legitimate to implement a module in C (and pretend it was a M2
1187 : implementation) providing that it is not the main program module and the
1188 : definition module does not declare a hidden type when -fextended-opaque
1189 : is used. */
1190 0 : if ((! M2Options_WholeProgram || (sym == Main)) || (SymbolTable_IsHiddenTypeDeclared (sym)))
1191 : {
1192 : /* Unrecoverable error. */
1193 0 : M2MetaError_MetaErrorString1 (FormatStrings_Sprintf1 (DynamicStrings_InitString ((const char *) "file {%%1EUAF%s} containing module {%%1a} cannot be found {%%1&Ds}", 66), (const unsigned char *) &FileName, (sizeof (FileName)-1)), sym);
1194 : }
1195 : }
1196 : }
1197 : }
1198 165504 : else if (M2Options_GenModuleList)
1199 : {
1200 : /* avoid dangling else. */
1201 93812 : if ((SymbolTable_IsDefImp (sym)) && (! (SymbolTable_IsDefinitionForC (sym))))
1202 : {
1203 : /* The implementation module is only useful if -fgen-module-list= is
1204 : used (to gather all dependencies). Note that we do not insist
1205 : upon finding the implementation module. */
1206 84230 : LibName = static_cast<DynamicStrings_String> (NULL);
1207 84230 : if (M2Search_FindSourceModFile (SymName, &FileName, &LibName))
1208 : {
1209 64602 : SymbolTable_PutLibName (sym, NameKey_makekey (DynamicStrings_string (LibName)));
1210 64602 : M2Quiet_qprintf2 ((const char *) " Module %-20s : %s", 20, (const unsigned char *) &SymName, (sizeof (SymName)-1), (const unsigned char *) &FileName, (sizeof (FileName)-1));
1211 64602 : qprintLibName (LibName);
1212 64602 : M2Quiet_qprintf0 ((const char *) " (linking)\\n", 12);
1213 64602 : ChildDep = M2Preprocess_MakeSaveTempsFileNameExt (CreateFileStem (SymName, LibName), DynamicStrings_InitString ((const char *) ".mod.d", 6));
1214 64602 : if (M2LexBuf_OpenSource (M2Batch_AssociateModule (M2Preprocess_PreprocessModule (FileName, false, true, ChildDep), sym)))
1215 : {
1216 64602 : SymbolTable_PutModLink (sym, true); /* This source is only used to determine link time info. */
1217 64602 : if (! (P0SyntaxCheck_CompilationUnit ())) /* This source is only used to determine link time info. */
1218 : {
1219 0 : M2Error_WriteFormat0 ((const char *) "compilation failed", 18);
1220 0 : M2LexBuf_CloseSource ();
1221 0 : SymName = DynamicStrings_KillString (SymName);
1222 0 : FileName = DynamicStrings_KillString (FileName);
1223 0 : LibName = DynamicStrings_KillString (LibName);
1224 0 : return false;
1225 : }
1226 64602 : M2LexBuf_CloseSource ();
1227 64602 : MergeDeps (DepContent, ChildDep, LibName);
1228 : }
1229 : }
1230 : }
1231 : }
1232 187566 : SymName = DynamicStrings_KillString (SymName);
1233 187566 : FileName = DynamicStrings_KillString (FileName);
1234 187566 : LibName = DynamicStrings_KillString (LibName);
1235 187566 : return true;
1236 : /* static analysis guarentees a RETURN statement will be used before here. */
1237 : __builtin_unreachable ();
1238 : }
1239 :
1240 :
1241 : /*
1242 : DoPass0 -
1243 : */
1244 :
1245 16979 : static void DoPass0 (DynamicStrings_String filename)
1246 : {
1247 16979 : unsigned int sym;
1248 16979 : unsigned int i;
1249 16979 : DynamicStrings_String PPSource;
1250 :
1251 16979 : P0SymBuild_P0Init ();
1252 16979 : M2Pass_SetPassToPass0 ();
1253 : /* Maybe preprocess the main file. */
1254 16979 : DepOutput = CreateDepFilename (filename);
1255 16979 : PPSource = M2Preprocess_PreprocessModule (filename, true, false, DepOutput);
1256 16979 : DepContent = ReadDepContents (filename, DepOutput);
1257 16979 : PeepInto (PPSource);
1258 16972 : i = 1;
1259 16972 : sym = M2Batch_GetModuleNo (i);
1260 16972 : M2Quiet_qprintf1 ((const char *) "Compiling: %s\\n", 15, (const unsigned char *) &PPSource, (sizeof (PPSource)-1));
1261 16972 : if (Debugging)
1262 : {
1263 : PathName_DumpPathName ((const char *) "DoPass0", 7);
1264 : }
1265 16972 : if (M2Options_Verbose)
1266 : {
1267 0 : M2Printf_fprintf1 (FIO_StdOut, (const char *) "Compiling: %s\\n", 15, (const unsigned char *) &PPSource, (sizeof (PPSource)-1));
1268 : }
1269 16972 : M2Quiet_qprintf0 ((const char *) "Pass 0: lexical analysis, parsing, modules and associated filenames\\n", 69);
1270 221510 : while (sym != SymbolTable_NulSym)
1271 : {
1272 187655 : if (! (Pass0CheckDef (sym)))
1273 : {
1274 54 : return;
1275 : }
1276 187626 : if (! (Pass0CheckMod (sym, PPSource)))
1277 : {
1278 : return;
1279 : }
1280 187566 : i += 1;
1281 187566 : sym = M2Batch_GetModuleNo (i);
1282 : }
1283 16883 : M2Pass_SetPassToNoPass ();
1284 : }
1285 :
1286 :
1287 : /*
1288 : DoPass1 - parses the sources of all modules necessary to compile
1289 : the required module, Main.
1290 : */
1291 :
1292 16847 : static void DoPass1 (void)
1293 : {
1294 16847 : NameKey_Name name;
1295 16847 : unsigned int Sym;
1296 16847 : unsigned int i;
1297 16847 : DynamicStrings_String FileName;
1298 :
1299 16847 : P0SymBuild_P1Init ();
1300 16847 : M2Pass_SetPassToPass1 ();
1301 16847 : i = 1;
1302 16847 : Sym = M2Batch_GetModuleNo (i);
1303 220571 : while (Sym != SymbolTable_NulSym)
1304 : {
1305 186883 : FileName = M2Batch_GetDefinitionModuleFile (Sym);
1306 186883 : if (FileName != NULL)
1307 : {
1308 174961 : if (Debugging)
1309 : {
1310 : name = SymbolTable_GetSymName (Sym);
1311 : M2Quiet_qprintf1 ((const char *) " Module %a\\n", 14, (const unsigned char *) &name, (sizeof (name)-1));
1312 : }
1313 174961 : if (M2LexBuf_OpenSource (FileName))
1314 : {
1315 174961 : ModuleType = M2Comp_Definition;
1316 174961 : if (! (P1Build_CompilationUnit ()))
1317 : {
1318 0 : M2MetaError_MetaError0 ((const char *) "compilation failed", 18);
1319 0 : M2LexBuf_CloseSource ();
1320 0 : return;
1321 : }
1322 174961 : M2LexBuf_CloseSource ();
1323 : }
1324 : else
1325 : {
1326 0 : M2Printf_fprintf1 (FIO_StdErr, (const char *) "failed to open %s\\n", 19, (const unsigned char *) &FileName, (sizeof (FileName)-1));
1327 0 : libc_exit (1);
1328 : }
1329 174961 : ModuleType = M2Comp_Implementation;
1330 : }
1331 : else
1332 : {
1333 11922 : ModuleType = M2Comp_Program;
1334 : }
1335 186883 : FileName = M2Batch_GetModuleFile (Sym);
1336 186883 : if (FileName != NULL)
1337 : {
1338 86369 : if (Debugging)
1339 : {
1340 : name = SymbolTable_GetSymName (Sym);
1341 : M2Quiet_qprintf1 ((const char *) " Module %a\\n", 14, (const unsigned char *) &name, (sizeof (name)-1));
1342 : }
1343 86369 : if (M2LexBuf_OpenSource (FileName))
1344 : {
1345 86369 : if (! (P1Build_CompilationUnit ()))
1346 : {
1347 0 : M2MetaError_MetaError0 ((const char *) "compilation failed", 18);
1348 0 : M2LexBuf_CloseSource ();
1349 0 : return;
1350 : }
1351 86363 : M2LexBuf_CloseSource ();
1352 : }
1353 : else
1354 : {
1355 0 : M2Printf_fprintf1 (FIO_StdErr, (const char *) "failed to open %s\\n", 19, (const unsigned char *) &FileName, (sizeof (FileName)-1));
1356 0 : libc_exit (1);
1357 : }
1358 : }
1359 186877 : i += 1;
1360 186877 : Sym = M2Batch_GetModuleNo (i);
1361 : }
1362 16841 : M2Pass_SetPassToNoPass ();
1363 : }
1364 :
1365 :
1366 : /*
1367 : DoPass2 - parses the sources of all modules necessary to compile
1368 : the required module, Main.
1369 : */
1370 :
1371 16817 : static void DoPass2 (void)
1372 : {
1373 16817 : NameKey_Name name;
1374 16817 : unsigned int Sym;
1375 16817 : unsigned int i;
1376 16817 : DynamicStrings_String FileName;
1377 :
1378 16817 : M2Pass_SetPassToPass2 ();
1379 16817 : i = 1;
1380 16817 : Sym = M2Batch_GetModuleNo (i);
1381 220283 : while (Sym != SymbolTable_NulSym)
1382 : {
1383 186667 : FileName = M2Batch_GetDefinitionModuleFile (Sym);
1384 186667 : if (FileName != NULL)
1385 : {
1386 174769 : if (Debugging)
1387 : {
1388 : name = SymbolTable_GetSymName (Sym);
1389 : M2Quiet_qprintf1 ((const char *) " Module %a\\n", 14, (const unsigned char *) &name, (sizeof (name)-1));
1390 : }
1391 174769 : if (M2LexBuf_OpenSource (FileName))
1392 : {
1393 174769 : ModuleType = M2Comp_Definition;
1394 174769 : if (! (P2Build_CompilationUnit ()))
1395 : {
1396 0 : M2MetaError_MetaError0 ((const char *) "compilation failed", 18);
1397 0 : M2LexBuf_CloseSource ();
1398 0 : return;
1399 : }
1400 174769 : M2LexBuf_CloseSource ();
1401 : }
1402 : else
1403 : {
1404 0 : M2Printf_fprintf1 (FIO_StdErr, (const char *) "failed to open %s\\n", 19, (const unsigned char *) &FileName, (sizeof (FileName)-1));
1405 0 : libc_exit (1);
1406 : }
1407 174769 : ModuleType = M2Comp_Implementation;
1408 : }
1409 : else
1410 : {
1411 11898 : ModuleType = M2Comp_Program;
1412 : }
1413 186667 : FileName = M2Batch_GetModuleFile (Sym);
1414 186667 : if (FileName != NULL)
1415 : {
1416 86267 : if (Debugging)
1417 : {
1418 : name = SymbolTable_GetSymName (Sym);
1419 : M2Quiet_qprintf1 ((const char *) " Module %a\\n", 14, (const unsigned char *) &name, (sizeof (name)-1));
1420 : }
1421 86267 : if (M2LexBuf_OpenSource (FileName))
1422 : {
1423 86267 : if (! (P2Build_CompilationUnit ()))
1424 : {
1425 0 : M2MetaError_MetaError0 ((const char *) "compilation failed", 18);
1426 0 : M2LexBuf_CloseSource ();
1427 0 : return;
1428 : }
1429 86249 : M2LexBuf_CloseSource ();
1430 : }
1431 : else
1432 : {
1433 0 : M2Printf_fprintf1 (FIO_StdErr, (const char *) "failed to open %s\\n", 19, (const unsigned char *) &FileName, (sizeof (FileName)-1));
1434 0 : libc_exit (1);
1435 : }
1436 : }
1437 186649 : i += 1;
1438 186649 : Sym = M2Batch_GetModuleNo (i);
1439 : }
1440 16799 : M2Pass_SetPassToNoPass ();
1441 : }
1442 :
1443 :
1444 : /*
1445 : DoPassC - parses the sources of all modules necessary to compile
1446 : the required module, Main.
1447 : */
1448 :
1449 16715 : static void DoPassC (void)
1450 : {
1451 16715 : NameKey_Name name;
1452 16715 : unsigned int Sym;
1453 16715 : unsigned int i;
1454 16715 : DynamicStrings_String FileName;
1455 :
1456 16715 : M2Pass_SetPassToPassC ();
1457 16715 : i = 1;
1458 16715 : Sym = M2Batch_GetModuleNo (i);
1459 219539 : while (Sym != SymbolTable_NulSym)
1460 : {
1461 186133 : FileName = M2Batch_GetDefinitionModuleFile (Sym);
1462 186133 : if (FileName != NULL)
1463 : {
1464 174283 : if (Debugging)
1465 : {
1466 : name = SymbolTable_GetSymName (Sym);
1467 : M2Quiet_qprintf1 ((const char *) " Module %a\\n", 14, (const unsigned char *) &name, (sizeof (name)-1));
1468 : }
1469 174283 : if (M2LexBuf_OpenSource (FileName))
1470 : {
1471 174283 : ModuleType = M2Comp_Definition;
1472 174283 : if (! (PCBuild_CompilationUnit ()))
1473 : {
1474 0 : M2MetaError_MetaError0 ((const char *) "compilation failed", 18);
1475 0 : M2LexBuf_CloseSource ();
1476 0 : return;
1477 : }
1478 174283 : M2LexBuf_CloseSource ();
1479 : }
1480 : else
1481 : {
1482 0 : M2Printf_fprintf1 (FIO_StdErr, (const char *) "failed to open %s\\n", 19, (const unsigned char *) &FileName, (sizeof (FileName)-1));
1483 0 : libc_exit (1);
1484 : }
1485 174283 : ModuleType = M2Comp_Implementation;
1486 : }
1487 : else
1488 : {
1489 11850 : ModuleType = M2Comp_Program;
1490 : }
1491 186133 : FileName = M2Batch_GetModuleFile (Sym);
1492 186133 : if (FileName != NULL)
1493 : {
1494 86165 : if (Debugging)
1495 : {
1496 : name = SymbolTable_GetSymName (Sym);
1497 : M2Quiet_qprintf1 ((const char *) " Module %a\\n", 14, (const unsigned char *) &name, (sizeof (name)-1));
1498 : }
1499 86165 : if (M2LexBuf_OpenSource (FileName))
1500 : {
1501 86165 : if (! (PCBuild_CompilationUnit ()))
1502 : {
1503 0 : M2MetaError_MetaError0 ((const char *) "compilation failed", 18);
1504 0 : M2LexBuf_CloseSource ();
1505 0 : return;
1506 : }
1507 86141 : M2LexBuf_CloseSource ();
1508 : }
1509 : else
1510 : {
1511 0 : M2Printf_fprintf1 (FIO_StdErr, (const char *) "failed to open %s\\n", 19, (const unsigned char *) &FileName, (sizeof (FileName)-1));
1512 0 : libc_exit (1);
1513 : }
1514 : }
1515 186109 : i += 1;
1516 186109 : Sym = M2Batch_GetModuleNo (i);
1517 : }
1518 16691 : PCSymBuild_ResolveConstTypes ();
1519 16691 : SymbolTable_ResolveConstructorTypes ();
1520 16685 : SymbolTable_SanityCheckConstants ();
1521 16685 : M2Pass_SetPassToNoPass ();
1522 : }
1523 :
1524 :
1525 : /*
1526 : DoPass3 - parses the sources of all modules necessary to compile
1527 : the required module, Main.
1528 : */
1529 :
1530 16595 : static void DoPass3 (void)
1531 : {
1532 16595 : unsigned int Main;
1533 16595 : unsigned int Sym;
1534 16595 : unsigned int i;
1535 16595 : DynamicStrings_String FileName;
1536 :
1537 16595 : M2Pass_SetPassToPass3 ();
1538 16595 : Main = SymbolTable_GetMainModule ();
1539 16595 : i = 1;
1540 16595 : Sym = M2Batch_GetModuleNo (i);
1541 218363 : while (Sym != SymbolTable_NulSym)
1542 : {
1543 185392 : FileName = M2Batch_GetDefinitionModuleFile (Sym);
1544 185392 : if (FileName != NULL)
1545 : {
1546 173644 : if (M2LexBuf_OpenSource (FileName))
1547 : {
1548 173644 : ModuleType = M2Comp_Definition;
1549 173644 : if (! (P3Build_CompilationUnit ()))
1550 : {
1551 0 : M2MetaError_MetaError0 ((const char *) "compilation failed", 18);
1552 0 : M2LexBuf_CloseSource ();
1553 0 : return;
1554 : }
1555 173644 : M2LexBuf_CloseSource ();
1556 : }
1557 : else
1558 : {
1559 0 : M2Printf_fprintf1 (FIO_StdErr, (const char *) "failed to open %s\\n", 19, (const unsigned char *) &FileName, (sizeof (FileName)-1));
1560 0 : libc_exit (1);
1561 : }
1562 173644 : ModuleType = M2Comp_Implementation;
1563 : }
1564 : else
1565 : {
1566 11748 : ModuleType = M2Comp_Program;
1567 : }
1568 185392 : FileName = M2Batch_GetModuleFile (Sym);
1569 185392 : if (FileName != NULL)
1570 : {
1571 : /* avoid gcc warning by using compound statement even if not strictly necessary. */
1572 86045 : if (M2LexBuf_OpenSource (FileName))
1573 : {
1574 86045 : if ((Main == Sym) || M2Options_WholeProgram)
1575 : {
1576 : /* avoid dangling else. */
1577 17123 : if (! (P3Build_CompilationUnit ()))
1578 : {
1579 0 : M2MetaError_MetaError0 ((const char *) "compilation failed", 18);
1580 0 : M2LexBuf_CloseSource ();
1581 0 : return;
1582 : }
1583 : }
1584 : else
1585 : {
1586 : /*
1587 : not the main module .mod therefore must be implementing
1588 : a hidden type - we dont want to generate any
1589 : StatementSequence quadrupes but we do want to build TYPEs
1590 : and ConstExpressions.
1591 : */
1592 68922 : M2Pass_SetPassToNoPass ();
1593 68922 : M2Pass_SetPassToPassHidden ();
1594 68922 : if (! (PHBuild_CompilationUnit ()))
1595 : {
1596 0 : M2MetaError_MetaError0 ((const char *) "compilation failed", 18);
1597 0 : M2LexBuf_CloseSource ();
1598 0 : return;
1599 : }
1600 68922 : M2Pass_SetPassToNoPass ();
1601 68922 : M2Pass_SetPassToPass3 ();
1602 : }
1603 85826 : M2LexBuf_CloseSource ();
1604 : }
1605 : else
1606 : {
1607 0 : M2Printf_fprintf1 (FIO_StdErr, (const char *) "failed to open %s\\n", 19, (const unsigned char *) &FileName, (sizeof (FileName)-1));
1608 0 : libc_exit (1);
1609 : }
1610 : }
1611 185173 : i += 1;
1612 185173 : Sym = M2Batch_GetModuleNo (i);
1613 : }
1614 16376 : M2Pass_SetPassToNoPass ();
1615 : }
1616 :
1617 :
1618 : /*
1619 : compile - compile the filename.
1620 : */
1621 :
1622 16979 : extern "C" void M2Comp_compile (void * filename)
1623 : {
1624 16979 : DynamicStrings_String f;
1625 16979 : DynamicStrings_String s;
1626 :
1627 16979 : M2Diagnostic_Configure (M2Options_TimeReport, M2Options_MemReport);
1628 16979 : f = DynamicStrings_InitStringCharStar (filename);
1629 16979 : Compile (f);
1630 15425 : f = DynamicStrings_KillString (f);
1631 15425 : PopulateResource ();
1632 15425 : if (M2Options_TimeReport || M2Options_MemReport)
1633 : {
1634 0 : s = SFIO_WriteS (FIO_StdOut, M2Diagnostic_Generate (false));
1635 0 : FIO_FlushOutErr ();
1636 0 : s = DynamicStrings_KillString (s);
1637 : }
1638 15425 : }
1639 :
1640 :
1641 : /*
1642 : CompilingDefinitionModule - returns true if the current module being
1643 : compiled is a definition module.
1644 : */
1645 :
1646 154822140 : extern "C" bool M2Comp_CompilingDefinitionModule (void)
1647 : {
1648 154822140 : return ModuleType == M2Comp_Definition;
1649 : /* static analysis guarentees a RETURN statement will be used before here. */
1650 : __builtin_unreachable ();
1651 : }
1652 :
1653 :
1654 : /*
1655 : CompilingImplementationModule - returns true if the current module being
1656 : compiled is an implementation module.
1657 : */
1658 :
1659 10532124 : extern "C" bool M2Comp_CompilingImplementationModule (void)
1660 : {
1661 10532124 : return ModuleType == M2Comp_Implementation;
1662 : /* static analysis guarentees a RETURN statement will be used before here. */
1663 : __builtin_unreachable ();
1664 : }
1665 :
1666 :
1667 : /*
1668 : CompilingProgramModule - returns true if the current module being
1669 : compiled is a program module.
1670 : */
1671 :
1672 94605 : extern "C" bool M2Comp_CompilingProgramModule (void)
1673 : {
1674 94605 : return ModuleType == M2Comp_Program;
1675 : /* static analysis guarentees a RETURN statement will be used before here. */
1676 : __builtin_unreachable ();
1677 : }
1678 :
1679 16979 : extern "C" void _M2_M2Comp_init (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[])
1680 : {
1681 16979 : ModuleType = M2Comp_None;
1682 16979 : DepContent = static_cast<Indexing_Index> (NULL);
1683 16979 : DepOutput = static_cast<DynamicStrings_String> (NULL);
1684 16979 : }
1685 :
1686 0 : extern "C" void _M2_M2Comp_fini (__attribute__((unused)) int argc, __attribute__((unused)) char *argv[], __attribute__((unused)) char *envp[])
1687 : {
1688 0 : }
|