-
-
Notifications
You must be signed in to change notification settings - Fork 267
/
Copy pathmain.d
1377 lines (1230 loc) · 39.3 KB
/
main.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Entry point for DMD console version.
*
* This modules defines the entry point (main) for DMD, as well as related
* utilities needed for arguments parsing, path manipulation, etc...
* This file is not shared with other compilers which use the DMD front-end.
*
* Copyright: Copyright (C) 1999-2025 by The D Language Foundation, All Rights Reserved
* Authors: $(LINK2 https://www.digitalmars.com, Walter Bright)
* License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/compiler/src/dmd/main.d, _main.d)
* Documentation: https://dlang.org/phobos/dmd_main.html
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/compiler/src/dmd/main.d
*/
module dmd.main;
version (NoMain) {} else
{
import core.stdc.stdio;
import core.stdc.stdlib;
import core.stdc.string;
import dmd.arraytypes : Modules, Strings;
import dmd.astenums;
import dmd.common.outbuffer;
import dmd.compiler;
import dmd.cond;
import dmd.console;
// IN_LLVM import dmd.cpreprocess;
import dmd.deps;
// IN_LLVM import dmd.dinifile;
import dmd.dinterpret;
// IN_LLVM import dmd.dmdparams;
import dmd.dsymbolsem;
import dmd.dtemplate;
import dmd.dtoh;
// IN_LLVM import dmd.glue : generateCodeAndWrite;
import dmd.dmodule;
// IN_LLVM import dmd.dmsc : backend_init, backend_term;
import dmd.doc;
import dmd.dsymbol;
import dmd.errors;
import dmd.expression;
import dmd.file_manager;
import dmd.hdrgen;
import dmd.globals;
import dmd.hdrgen;
import dmd.id;
import dmd.identifier;
import dmd.inline;
// IN_LLVM import dmd.link;
import dmd.location;
import dmd.mars;
import dmd.mtype;
import dmd.objc;
// IN_LLVM import dmd.root.env;
import dmd.root.file;
import dmd.root.filename;
import dmd.root.man;
// IN_LLVM import dmd.root.response;
import dmd.root.rmem;
import dmd.root.string;
import dmd.root.stringtable;
import dmd.root.array;
import dmd.semantic2;
import dmd.semantic3;
import dmd.target;
import dmd.timetrace;
import dmd.utils;
import dmd.vsoptions;
version (IN_LLVM)
{
import gen.semantic : extraLDCSpecificSemanticAnalysis;
extern (C++):
// in driver/main.cpp
void registerPredefinedVersions();
void codegenModules(ref Modules modules);
// in driver/archiver.cpp
int createStaticLibrary();
// in driver/linker.cpp
int linkObjToBinary();
void deleteExeFile();
int runProgram();
}
version (IN_LLVM) {} else {
/**
* DMD's entry point, C main.
*
* Without `-lowmem`, we need to switch to the bump-pointer allocation scheme
* right from the start, before any module ctors are run, so we need this hook
* before druntime is initialized and `_Dmain` is called.
*
* Returns:
* Return code of the application
*/
extern (C) int main(int argc, char** argv)
{
bool lowmem = false;
foreach (i; 1 .. argc)
{
if (strcmp(argv[i], "-lowmem") == 0)
{
lowmem = true;
break;
}
}
if (!lowmem)
{
__gshared string[] disable_options = [ "gcopt=disable:1" ];
rt_options = disable_options;
mem.disableGC();
}
// initialize druntime and call _Dmain() below
return _d_run_main(argc, argv, &_Dmain);
}
/**
* Manual D main (for druntime initialization), which forwards to `tryMain`.
*
* Returns:
* Return code of the application
*/
extern (C) int _Dmain(char[][])
{
// possibly install memory error handler
version (DigitalMars)
{
installMemErrHandler();
}
import core.runtime;
version(D_Coverage)
{
// for now we need to manually set the source path
string dirName(string path, char separator)
{
for (size_t i = path.length - 1; i > 0; i--)
{
if (path[i] == separator)
return path[0..i];
}
return path;
}
version (Windows)
enum sourcePath = dirName(dirName(dirName(__FILE_FULL_PATH__, '\\'), '\\'), '\\');
else
enum sourcePath = dirName(dirName(dirName(__FILE_FULL_PATH__, '/'), '/'), '/');
dmd_coverSourcePath(sourcePath);
dmd_coverDestPath(sourcePath);
dmd_coverSetMerge(true);
}
version (D_Exceptions)
scope(failure) stderr.printInternalFailure;
auto args = Runtime.cArgs();
return tryMain(args.argc, cast(const(char)**)args.argv, global.params);
}
} // !IN_LLVM
/************************************************************************************/
private:
/**
* DMD's real entry point
*
* Parses command line arguments and config file, open and read all
* provided source file and do semantic analysis on them.
*
* Params:
* argc = Number of arguments passed via command line
* argv = Array of string arguments passed via command line
*
* Returns:
* Application return code
*/
// LDC: changed from `private int tryMain(size_t argc, const(char)** argv, ref Param params)`
extern (C++) int mars_tryMain(ref Param params, ref Strings files)
{
import dmd.common.charactertables;
import dmd.sarif;
import core.stdc.stdarg;
version (IN_LLVM)
{
Strings libmodules;
}
else
{
Strings files;
Strings libmodules;
global._init();
}
scope(exit)
{
// If we are here then compilation has ended
// gracefully as opposed to with `fatal`
global.plugErrorSinks();
if (global.errors == 0 && global.params.v.messageStyle == MessageStyle.sarif)
{
generateSarifReport(true);
}
}
version (IN_LLVM) {} else
{
target.setTargetBuildDefaults();
if (parseCommandlineAndConfig(argc, argv, params, files))
return EXIT_FAILURE;
}
global.compileEnv.previewIn = global.params.previewIn;
global.compileEnv.transitionIn = global.params.v.vin;
global.compileEnv.ddocOutput = global.params.ddoc.doOutput;
final switch(global.params.cIdentifierTable)
{
case CLIIdentifierTable.C99:
global.compileEnv.cCharLookupTable = IdentifierCharLookup.forTable(IdentifierTable.C99);
break;
case CLIIdentifierTable.C11:
case CLIIdentifierTable.default_:
// ImportC is defined against C11, not C23.
// If it was C23 this needs to be changed to UAX31 instead.
global.compileEnv.cCharLookupTable = IdentifierCharLookup.forTable(IdentifierTable.C11);
break;
case CLIIdentifierTable.UAX31:
global.compileEnv.cCharLookupTable = IdentifierCharLookup.forTable(IdentifierTable.UAX31);
break;
case CLIIdentifierTable.All:
global.compileEnv.cCharLookupTable = IdentifierCharLookup.forTable(IdentifierTable.LR);
break;
}
final switch(global.params.dIdentifierTable)
{
case CLIIdentifierTable.C99:
global.compileEnv.dCharLookupTable = IdentifierCharLookup.forTable(IdentifierTable.C99);
break;
case CLIIdentifierTable.C11:
global.compileEnv.dCharLookupTable = IdentifierCharLookup.forTable(IdentifierTable.C11);
break;
case CLIIdentifierTable.UAX31:
global.compileEnv.dCharLookupTable = IdentifierCharLookup.forTable(IdentifierTable.UAX31);
break;
case CLIIdentifierTable.All:
case CLIIdentifierTable.default_:
// @@@DEPRECATED_2.119@@@
// Change the default to UAX31,
// this is a breaking change as C99 (what D used for ~23 years),
// has characters that are not in UAX31.
global.compileEnv.dCharLookupTable = IdentifierCharLookup.forTable(IdentifierTable.LR);
break;
}
version (IN_LLVM) {} else
{
if (params.help.usage)
{
usage();
return EXIT_SUCCESS;
}
if (params.v.logo)
{
logo();
return EXIT_SUCCESS;
}
}
/*
Prints a supplied usage text to the console and
returns the exit code for the help usage page.
Returns:
`EXIT_SUCCESS` if no errors occurred, `EXIT_FAILURE` otherwise
*/
static int printHelpUsage(string help)
{
printf("%.*s", cast(int)help.length, &help[0]);
return global.errors ? EXIT_FAILURE : EXIT_SUCCESS;
}
/*
Print a message to make it clear when warnings are treated as errors.
*/
static void errorOnWarning()
{
error(Loc.initial, "warnings are treated as errors");
errorSupplemental(Loc.initial, "Use -wi if you wish to treat warnings only as informational.");
}
// In case deprecation messages were omitted, inform the user about it
static void mentionOmittedDeprecations()
{
if (global.params.v.errorLimit != 0 &&
global.deprecations > global.params.v.errorLimit)
{
const omitted = global.deprecations - global.params.v.errorLimit;
message(Loc.initial, "%d deprecation warning%s omitted, use `-verrors=0` to show all",
omitted, omitted == 1 ? "".ptr : "s".ptr);
}
}
/*
Generates code to check for all `params` whether any usage page
has been requested.
If so, the generated code will print the help page of the flag
and return with an exit code.
Params:
params = parameters with `Usage` suffices in `params` for which
their truthness should be checked.
Returns: generated code for checking the usage pages of the provided `params`.
*/
static string generateUsageChecks(string[] params)
{
string s;
foreach (n; params)
{
s ~= q{
if (params.help.}~n~q{)
return printHelpUsage(CLIUsage.}~n~q{Usage);
};
}
return s;
}
import dmd.cli : CLIUsage;
version (IN_LLVM)
{
mixin(generateUsageChecks(["transition", "preview", "revert"]));
}
else
{
mixin(generateUsageChecks(["mcpu", "transition", "check", "checkAction",
"preview", "revert", "externStd", "hc"]));
}
version (IN_LLVM) {} else
{
if (params.help.manual)
{
version (Windows)
{
browse("https://dlang.org/dmd-windows.html");
}
version (linux)
{
browse("https://dlang.org/dmd-linux.html");
}
version (OSX)
{
browse("https://dlang.org/dmd-osx.html");
}
version (FreeBSD)
{
browse("https://dlang.org/dmd-freebsd.html");
}
/*NOTE: No regular builds for openbsd/dragonflybsd (yet) */
/*
version (OpenBSD)
{
browse("https://dlang.org/dmd-openbsd.html");
}
version (DragonFlyBSD)
{
browse("https://dlang.org/dmd-dragonflybsd.html");
}
*/
return EXIT_SUCCESS;
}
} // !IN_LLVM
if (params.v.color)
global.console = cast(void*) createConsole(core.stdc.stdio.stderr);
version (IN_LLVM) {} else
{
target.setCPU();
}
Loc.set(params.v.showColumns, params.v.messageStyle);
if (global.errors)
{
fatal();
}
if (files.length == 0 && !params.readStdin)
{
if (params.jsonFieldFlags)
{
Modules modules; // empty
if (generateJson(modules, global.errorSink))
fatal();
return EXIT_SUCCESS;
}
version (IN_LLVM)
{
error(Loc.initial, "No source files");
}
else
{
usage();
}
return EXIT_FAILURE;
}
reconcileCommands(params, target);
version (IN_LLVM)
{
registerPredefinedVersions();
}
else
{
setDefaultLibraries(target, driverParams.defaultlibname, driverParams.debuglibname);
}
// Initialization
target._init(params);
Type._init();
Id.initialize();
Module._init();
Expression._init();
Objc._init();
reconcileLinkRunLib(params, files.length, target.obj_ext);
version(CRuntime_Microsoft)
{
import dmd.root.longdouble;
initFPU();
}
import dmd.root.ctfloat : CTFloat;
CTFloat.initialize();
version (IN_LLVM) {} else
{
// Predefined version identifiers
addDefaultVersionIdentifiers(params, target);
}
if (params.v.verbose)
{
stdout.printPredefinedVersions();
version (IN_LLVM)
{
// LDC prints binary/version/config before entering this function.
}
else
{
stdout.printGlobalConfigs();
}
}
//printf("%d source files\n", cast(int) files.length);
// Build import search path
static void buildImportPath(ref Array!ImportPathInfo imppath, ref Array!ImportPathInfo result, ref Strings pathsOnlyResult)
{
Array!ImportPathInfo array;
Strings pathsOnlyArray;
foreach (entry; imppath)
{
int sink(const(char)* p) nothrow
{
ImportPathInfo temp = entry;
temp.path = p;
array.push(temp);
return 0;
}
FileName.splitPath(&sink, entry.path);
FileName.appendSplitPath(entry.path, pathsOnlyArray);
}
result.append(&array);
pathsOnlyResult.append(&pathsOnlyArray);
}
static void buildFileImportPath(ref Strings imppath, ref Strings result)
{
Strings array;
foreach (const path; imppath)
{
FileName.appendSplitPath(path, array);
}
result.append(&array);
}
if (params.mixinOut.doOutput)
{
params.mixinOut.buffer = cast(OutBuffer*)Mem.check(calloc(1, OutBuffer.sizeof));
atexit(&flushMixins); // see comment for flushMixins
}
scope(exit) flushMixins();
buildImportPath(params.imppath, global.path, global.importPaths);
buildFileImportPath(params.fileImppath, global.filePath);
if (params.timeTrace)
{
import dmd.timetrace;
version (IN_LLVM)
{
initializeTimeTrace(params.timeTraceGranularityUs, params.argv0.toCString.ptr);
}
else
{
initializeTimeTrace(params.timeTraceGranularityUs, argv[0]);
}
}
// Create Modules
Modules modules;
modules.reserve(files.length);
if (createModules(files, libmodules, params, target, global.errorSink, modules))
fatal();
// Read files
foreach (m; modules)
{
m.read(Loc.initial);
}
OutBuffer ddocbuf; // buffer for contents of .ddoc files
bool ddocbufIsRead; // set when ddocbuf is filled
/* Read ddoc macro files named by the DDOCFILE environment variable and command line
* and concatenate the text into ddocbuf
*/
void readDdocFiles(Loc loc, ref const Strings ddocfiles, ref OutBuffer ddocbuf)
{
foreach (file; ddocfiles)
{
if (readFile(loc, file.toDString(), ddocbuf))
fatal();
// BUG: convert file contents to UTF-8 before use
//printf("file: '%.*s'\n", cast(int)buffer.data.length, buffer.data.ptr);
}
ddocbufIsRead = true;
}
bool anydocfiles = false;
OutBuffer ddocOutputText;
{
// Parse files
timeTraceBeginEvent(TimeTraceEventType.parseGeneral);
scope (exit) timeTraceEndEvent(TimeTraceEventType.parseGeneral);
size_t filecount = modules.length;
for (size_t filei = 0, modi = 0; filei < filecount; filei++, modi++)
{
Module m = modules[modi];
if (params.v.verbose)
message("parse %s", m.toChars());
if (!Module.rootModule)
Module.rootModule = m;
m.importedFrom = m; // m.isRoot() == true
version (IN_LLVM) {} else
{
// if (!driverParams.oneobj || modi == 0 || m.isDocFile)
// m.deleteObjFile();
}
m.parse();
// Finalize output filenames. Update if `-oq` was specified (only feasible after parsing).
if (params.fullyQualifiedObjectFiles && m.md)
{
m.objfile = m.setOutfilename(params.objname, params.objdir, m.arg, FileName.ext(m.objfile.toString()));
if (m.docfile)
m.setDocfile();
if (m.hdrfile)
m.hdrfile = m.setOutfilename(params.dihdr.name, params.dihdr.dir, m.arg, hdr_ext);
}
version (IN_LLVM)
{
// Set object filename in params.objfiles.
for (size_t j = 0; j < params.objfiles.length; j++)
{
if (params.objfiles[j] == cast(const(char)*)m)
{
params.objfiles[j] = m.objfile.toChars();
if (m.filetype != FileType.dhdr && m.filetype != FileType.ddoc && params.obj)
m.checkAndAddOutputFile(m.objfile);
break;
}
}
if (!driverParams.oneobj || modi == 0 || m.filetype == FileType.ddoc)
m.deleteObjFile();
} // IN_LLVM
if (m.filetype == FileType.dhdr)
{
// Remove m's object file from list of object files
for (size_t j = 0; j < params.objfiles.length; j++)
{
if (m.objfile.toChars() == params.objfiles[j])
{
params.objfiles.remove(j);
break;
}
}
if (params.objfiles.length == 0)
driverParams.link = false;
}
if (m.filetype == FileType.ddoc)
{
anydocfiles = true;
if (!ddocbufIsRead)
readDdocFiles(m.loc, global.params.ddoc.files, ddocbuf);
ddocOutputText.setsize(0);
gendocfile(m, ddocbuf[], global.datetime.ptr, global.errorSink, ddocOutputText);
if (!writeFile(m.loc, m.docfile.toString(), ddocOutputText[]))
fatal();
// Remove m from list of modules
modules.remove(modi);
modi--;
// Remove m's object file from list of object files
for (size_t j = 0; j < params.objfiles.length; j++)
{
if (m.objfile.toChars() == params.objfiles[j])
{
params.objfiles.remove(j);
break;
}
}
if (params.objfiles.length == 0)
driverParams.link = false;
}
}
}
if (anydocfiles && modules.length && (driverParams.oneobj || params.objname))
{
error(Loc.initial, "conflicting Ddoc and obj generation options");
fatal();
}
if (global.errors)
fatal();
if (params.dihdr.doOutput)
{
/* Generate 'header' import files.
* Since 'header' import files must be independent of command
* line switches and what else is imported, they are generated
* before any semantic analysis.
*/
OutBuffer buf;
foreach (m; modules)
{
if (m.filetype == FileType.dhdr)
continue;
if (params.v.verbose)
message("import %s", m.toChars());
buf.reset(); // reuse the buffer
genhdrfile(m, params.dihdr.fullOutput, buf);
if (!writeFile(m.loc, m.hdrfile.toString(), buf[]))
fatal();
}
}
if (global.errors)
removeHdrFilesAndFail(params, modules);
{
timeTraceBeginEvent(TimeTraceEventType.semaGeneral);
scope (exit) timeTraceEndEvent(TimeTraceEventType.semaGeneral);
// load all unconditional imports for better symbol resolving
foreach (m; modules)
{
if (params.v.verbose)
message("importall %s", m.toChars());
m.importAll(null);
}
if (global.errors)
removeHdrFilesAndFail(params, modules);
version (IN_LLVM) {} else
{
backend_init(params, driverParams, target);
}
// Do semantic analysis
foreach (m; modules)
{
if (params.v.verbose)
message("semantic %s", m.toChars());
m.dsymbolSemantic(null);
}
//if (global.errors)
// fatal();
Module.runDeferredSemantic();
if (Module.deferred.length)
{
for (size_t i = 0; i < Module.deferred.length; i++)
{
Dsymbol sd = Module.deferred[i];
error(sd.loc, "%s `%s` unable to resolve forward reference in definition", sd.kind(), sd.toPrettyChars());
}
//fatal();
}
// Do pass 2 semantic analysis
foreach (m; modules)
{
if (params.v.verbose)
message("semantic2 %s", m.toChars());
m.semantic2(null);
}
Module.runDeferredSemantic2();
if (global.errors)
removeHdrFilesAndFail(params, modules);
// Do pass 3 semantic analysis
foreach (m; modules)
{
if (params.v.verbose)
message("semantic3 %s", m.toChars());
m.semantic3(null);
}
if (includeImports)
{
// Note: DO NOT USE foreach here because Module.amodules.length can
// change on each iteration of the loop
for (size_t i = 0; i < compiledImports.length; i++)
{
auto m = compiledImports[i];
assert(m.isRoot);
if (params.v.verbose)
message("semantic3 %s", m.toChars());
m.semantic3(null);
modules.push(m);
}
}
Module.runDeferredSemantic3();
if (global.errors)
removeHdrFilesAndFail(params, modules);
version (IN_LLVM)
{
extraLDCSpecificSemanticAnalysis(modules);
}
else
{
// Scan for functions to inline
foreach (m; modules)
{
if (params.useInline || m.hasAlwaysInlines)
{
if (params.v.verbose)
message("inline scan %s", m.toChars());
inlineScanModule(m);
}
}
}
if (global.warnings)
errorOnWarning();
if (global.params.useDeprecated == DiagnosticReporting.inform)
mentionOmittedDeprecations();
// Do not attempt to generate output files if errors or warnings occurred
if (global.errors || global.warnings)
removeHdrFilesAndFail(params, modules);
// inlineScan incrementally run semantic3 of each expanded functions.
// So deps file generation should be moved after the inlining stage.
if (OutBuffer* ob = params.moduleDeps.buffer)
{
foreach (i; 1 .. modules[0].aimports.length)
semantic3OnDependencies(modules[0].aimports[i]);
Module.runDeferredSemantic3();
const data = (*ob)[];
if (params.moduleDeps.name)
{
if (!writeFile(Loc.initial, params.moduleDeps.name, data))
fatal();
version (IN_LLVM)
{
// fix LDC issue #1625
params.moduleDeps = Output();
}
}
else
printf("%.*s", cast(int)data.length, data.ptr);
}
}
printCtfePerformanceStats();
printTemplateStats(global.params.v.templatesListInstances, global.errorSink);
// Generate output files
if (params.json.doOutput)
{
if (generateJson(modules, global.errorSink))
fatal();
}
if (!global.errors && params.ddoc.doOutput)
{
foreach (m; modules)
{
if (!ddocbufIsRead)
readDdocFiles(m.loc, global.params.ddoc.files, ddocbuf);
ddocOutputText.setsize(0);
gendocfile(m, ddocbuf[], global.datetime.ptr, global.errorSink, ddocOutputText);
if (!writeFile(m.loc, m.docfile.toString(), ddocOutputText[]))
fatal();
}
}
if (params.vcg_ast)
{
import dmd.hdrgen;
foreach (mod; modules)
{
auto buf = OutBuffer();
buf.doindent = 1;
moduleToBuffer(buf, params.vcg_ast, mod);
// write the output to $(filename).cg
auto cgFilename = FileName.addExt(mod.srcfile.toString(), "cg");
File.write(cgFilename.ptr, buf[]);
}
}
if (global.params.cxxhdr.doOutput)
genCppHdrFiles(modules);
if (global.errors)
fatal();
if (!IN_LLVM && driverParams.lib && params.objfiles.length == 0)
{
error(Loc.initial, "no input files");
return EXIT_FAILURE;
}
if (params.addMain && !global.hasMainFunction)
{
auto mainModule = moduleWithEmptyMain();
modules.push(mainModule);
if (IN_LLVM && driverParams.oneobj && modules.length == 1)
params.objfiles.insert(0, mainModule.objfile.toChars()); // must be *first* objfile for LDC's oneobj
else if (!driverParams.oneobj || modules.length == 1)
params.objfiles.push(mainModule.objfile.toChars());
}
version (IN_LLVM)
{
import core.memory : GC;
static if (__traits(compiles, GC.stats))
{
if (params.v.verbose)
{
static int toMB(ulong size) { return cast(int) (size / 1048576.0 + 0.5); }
const stats = GC.stats;
const used = toMB(stats.usedSize);
const free = toMB(stats.freeSize);
const total = toMB(stats.usedSize + stats.freeSize);
message("GC stats %dM used, %dM free, %dM total", used, free, total);
}
}
codegenModules(modules);
}
else // !IN_LLVM
{
{
timeTraceBeginEvent(TimeTraceEventType.codegenGlobal);
scope (exit) timeTraceEndEvent(TimeTraceEventType.codegenGlobal);
generateCodeAndWrite(modules[], libmodules[], params.libname, params.objdir,
driverParams.lib, params.obj, driverParams.oneobj, params.multiobj,
params.v.verbose);
}
backend_term();
} // !IN_LLVM
if (global.errors)
fatal();
int status = EXIT_SUCCESS;
if (!params.objfiles.length)
{
if (driverParams.link)
error(Loc.initial, "no object files to link");
if (IN_LLVM && !driverParams.link && driverParams.lib)
error(Loc.initial, "no object files");
}
else
{
version (IN_LLVM)
{
if (driverParams.link)
status = linkObjToBinary();
else if (driverParams.lib)
status = createStaticLibrary();
if (status == EXIT_SUCCESS && params.cleanupObjectFiles)
{
foreach (m; modules)
{
m.deleteObjFile();
if (driverParams.oneobj)
break;
}
}
}
else // !IN_LLVM
{
if (driverParams.link)
{
timeTraceBeginEvent(TimeTraceEventType.link);
scope (exit) timeTraceEndEvent(TimeTraceEventType.link);
status = runLINK(global.params.v.verbose, global.errorSink);
}
}
if (params.run)
{
if (!status)
{
version (IN_LLVM)
{
status = runProgram();
// object files already deleted above
deleteExeFile();
}
else
{
restoreEnvVars();
status = runProgram(global.params.exefile, global.params.runargs[], global.params.v.verbose, global.errorSink);
/* Delete .obj files and .exe file
*/
foreach (m; modules)
{
m.deleteObjFile();
if (driverParams.oneobj)
break;
}
params.exefile.toCStringThen!(ef => File.remove(ef.ptr));
}
}
}
}
if (params.timeTrace)
{
import dmd.timetrace;
auto fileName = params.timeTraceFile.toDString();
if (!fileName)
{
if (global.params.objfiles.length)
{
fileName = global.params.objfiles[0].toDString() ~ ".time-trace";
}
else
{
fileName = "out.time-trace";
}
}
OutBuffer buf;
timeTraceProfiler.writeToBuffer(buf);
if (fileName == "-")
{
// Write to stdout
import core.stdc.stdio : fwrite, stdout;
size_t n = fwrite(buf[].ptr, 1, buf.length, stdout);
if (n != buf.length)
{
error(Loc.initial, "Error writing -ftime-trace profile to stdout");
}
}
else if (!File.write(fileName, buf[]))