Skip to content

Commit 5a2bf3b

Browse files
Replace idgen with CTFE implementation
Since id.h is not necessary anymore it's just as easy to implement the same functionality using CTFE. This will also simplify the build process because one extra build step is removed. `extern(C++)` is removed as well because it will only be called from within D code.
1 parent f9db662 commit 5a2bf3b

6 files changed

Lines changed: 93 additions & 142 deletions

File tree

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
*.[oa]
22
*.deps
33
src/dmd
4-
src/ddmd/id.d
5-
src/ddmd/id.h
64
test/test_results
75
test/trace.def
86
test/trace.log

src/ddmd/idgen.d renamed to src/ddmd/id.d

Lines changed: 86 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,51 @@
22
* Compiler implementation of the
33
* $(LINK2 http://www.dlang.org, D programming language).
44
*
5+
* This module contains the `Id` struct with a list of predefined symbols the
6+
* compiler knows about.
7+
*
58
* Copyright: Copyright (c) 1999-2017 by Digital Mars, All Rights Reserved
69
* Authors: $(LINK2 http://www.digitalmars.com, Walter Bright)
710
* License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost License 1.0)
8-
* Source: $(DMDSRC _idgen.d)
11+
* Source: $(DMDSRC _id.d)
912
*/
13+
module ddmd.id;
1014

11-
// Program to generate string files in d data structures.
12-
// Saves much tedious typing, and eliminates typo problems.
13-
// Generates:
14-
// id.h
15-
// id.c
16-
17-
import core.stdc.stdio;
18-
import core.stdc.stdlib;
15+
import ddmd.identifier;
16+
import ddmd.tokens;
1917

20-
struct Msgtable
18+
/**
19+
* Represents a list of predefined symbols the compiler knows about.
20+
*
21+
* All static fields in this struct represents a specific predefined symbol.
22+
*/
23+
struct Id
2124
{
22-
const(char)* ident; // name to use in DMD source
23-
const(char)* name; // name in D executable
24-
};
25+
static __gshared:
26+
27+
mixin(msgtable.generate(&identifier));
28+
29+
/**
30+
* Populates the identifier pool with all predefined symbols.
31+
*
32+
* An identifier that corresponds to each static field in this struct will
33+
* be placed in the identifier pool.
34+
*/
35+
void initialize()
36+
{
37+
mixin(msgtable.generate(&initializer));
38+
}
39+
}
40+
41+
private:
2542

26-
Msgtable[] msgtable =
43+
44+
/**
45+
* Each element in this array will generate one static field in the `Id` struct
46+
* and a call to `Identifier.idPool` to populate the identifier pool in the
47+
* `Id.initialize` method.
48+
*/
49+
immutable Msgtable[] msgtable =
2750
[
2851
{ "IUnknown" },
2952
{ "Object" },
@@ -376,82 +399,60 @@ Msgtable[] msgtable =
376399
];
377400

378401

379-
int main()
402+
/*
403+
* Tuple of DMD source code identifier and symbol in the D executable.
404+
*
405+
* The first element of the tuple is the identifier to use in the DMD source
406+
* code and the second element, if present, is the name to use in the D
407+
* executable. If second element, `name`, is not present the identifier,
408+
* `ident`, will be used instead
409+
*/
410+
struct Msgtable
380411
{
412+
// The identifier to use in the DMD source.
413+
string ident;
414+
415+
// The name to use in the D executable
416+
private string name_;
417+
418+
/*
419+
* Returns: the name to use in the D executable, `name_` if non-empty,
420+
* otherwise `ident`
421+
*/
422+
string name()
381423
{
382-
auto fp = fopen("ddmd/id.h","wb");
383-
if (!fp)
384-
{
385-
printf("can't open ddmd/id.h\n");
386-
exit(EXIT_FAILURE);
387-
}
388-
389-
fprintf(fp, "// File generated by idgen.d\n");
390-
fprintf(fp, "#ifndef DMD_ID_H\n");
391-
fprintf(fp, "#define DMD_ID_H 1\n");
392-
fprintf(fp, "class Identifier;\n");
393-
fprintf(fp, "struct Id\n");
394-
fprintf(fp, "{\n");
395-
396-
foreach(e; msgtable)
397-
{
398-
auto id = e.ident;
399-
fprintf(fp," static Identifier *%s;\n", id);
400-
}
401-
402-
fprintf(fp, " static void initialize();\n");
403-
fprintf(fp, "};\n");
404-
fprintf(fp, "#endif\n");
405-
406-
fclose(fp);
424+
return name_.length == 0 ? ident : name_;
407425
}
426+
}
427+
428+
/*
429+
* Iterates the given Msgtable array, passes each element to the given lambda
430+
* and accumulates a string from each return value of calling the lambda.
431+
* Appends a newline character after each call to the lambda.
432+
*/
433+
string generate(immutable(Msgtable)[] msgtable, string function(Msgtable) dg)
434+
{
435+
string code;
408436

437+
foreach (i, m ; msgtable)
409438
{
410-
auto fp = fopen("ddmd/id.d","wb");
411-
if (!fp)
412-
{
413-
printf("can't open ddmd.id.d\n");
414-
exit(EXIT_FAILURE);
415-
}
416-
417-
fprintf(fp, "// File generated by idgen.d\n");
418-
fprintf(fp, "\n");
419-
fprintf(fp, "module ddmd.id;\n");
420-
fprintf(fp, "\n");
421-
fprintf(fp, "import ddmd.identifier, ddmd.tokens;\n");
422-
fprintf(fp, "\n");
423-
fprintf(fp, "struct Id\n");
424-
fprintf(fp, "{\n");
425-
426-
foreach(e; msgtable)
427-
{
428-
auto id = e.ident;
429-
auto p = e.name;
430-
431-
if (!p)
432-
p = id;
433-
fprintf(fp, " extern (C++) static __gshared Identifier %s;\n", id);
434-
}
435-
436-
fprintf(fp, "\n");
437-
fprintf(fp, " extern (C++) static void initialize()\n");
438-
fprintf(fp, " {\n");
439-
440-
foreach(e; msgtable)
441-
{
442-
auto id = e.ident;
443-
auto p = e.name;
444-
445-
if (!p)
446-
p = id;
447-
fprintf(fp," %s = Identifier.idPool(\"%s\");\n", id, p);
448-
}
449-
450-
fprintf(fp, " }\n");
451-
fprintf(fp, "}\n");
452-
453-
fclose(fp);
439+
if (i != 0)
440+
code ~= '\n';
441+
442+
code ~= dg(m);
454443
}
455444

456-
return EXIT_SUCCESS;
445+
return code;
446+
}
447+
448+
// Used to generate the code for each identifier.
449+
string identifier(Msgtable m)
450+
{
451+
return "Identifier " ~ m.ident ~ ";";
452+
}
453+
454+
// Used to generate the code for each initializer.
455+
string initializer(Msgtable m)
456+
{
457+
return m.ident ~ ` = Identifier.idPool("` ~ m.name ~ `");`;
457458
}

src/posix.mak

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ FRONT_SRCS=$(addsuffix .d, $(addprefix $D/,access aggregate aliasthis apply argt
227227
cppmangle ctfeexpr dcast dclass declaration delegatize denum dimport \
228228
dinifile dinterpret dmacro dmangle dmodule doc dscope dstruct dsymbol \
229229
dtemplate dversion escape expression func \
230-
hdrgen impcnvtab imphint init inline inlinecost intrange \
230+
hdrgen id impcnvtab imphint init inline inlinecost intrange \
231231
json lib link mars mtype nogc nspace objc opover optimize parse sapply \
232232
sideeffect statement staticassert target traits visitor \
233233
typinf utils statement_rewrite_walker statementsem staticcond safe blockexit asttypename))
@@ -285,7 +285,7 @@ endif
285285

286286
SRC = $(addprefix $D/, win32.mak posix.mak osmodel.mak aggregate.h aliasthis.h arraytypes.h \
287287
attrib.h complex_t.h cond.h ctfe.h ctfe.h declaration.h dsymbol.h \
288-
enum.h errors.h expression.h globals.h hdrgen.h identifier.h idgen.d \
288+
enum.h errors.h expression.h globals.h hdrgen.h identifier.h \
289289
import.h init.h intrange.h json.h lexer.h \
290290
mars.h module.h mtype.h nspace.h objc.h \
291291
scope.h statement.h staticassert.h target.h template.h tokens.h \
@@ -383,7 +383,6 @@ endif
383383

384384
clean:
385385
rm -R $(GENERATED)
386-
rm -f dmd $(idgen_output)
387386
rm -f $(addprefix $D/backend/, $(optabgen_output))
388387
@[ ! -d ${PGO_DIR} ] || echo You should issue manually: rm -rf ${PGO_DIR}
389388

@@ -444,15 +443,6 @@ $(optabgen_files): optabgen.out
444443
.INTERMEDIATE: optabgen.out
445444
optabgen.out : $G/optabgen
446445

447-
######## idgen generates some source
448-
449-
idgen_output = $D/id.h $D/id.d
450-
$(idgen_output) : $G/idgen
451-
452-
$G/idgen: $D/idgen.d $(HOST_DMD_PATH)
453-
CC=$(HOST_CXX) $(HOST_DMD_RUN) -of$@ $<
454-
$G/idgen
455-
456446
#########
457447
# STRING_IMPORT_FILES
458448
#

src/vcbuild/dmd.visualdproj

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -412,28 +412,6 @@
412412
<Folder name="gen">
413413
<File tool="Custom" path="..\..\VERSION" customcmd="copy ..\..\VERSION $(OutDir)\VERSION" uptodateWithSameTime="true" outfile="$(OutDir)\VERSION" />
414414
</Folder>
415-
<Folder name="generated">
416-
<Folder name="Debug_Win32">
417-
<File tool="None" path="..\..\generated\Windows\Debug\Win32\generated\ddmd\id.d" perConfig="true">
418-
<Config name="Debug|Win32" />
419-
</File>
420-
</Folder>
421-
<Folder name="Debug_x64">
422-
<File tool="None" path="..\..\generated\Windows\Debug\x64\generated\ddmd\id.d" perConfig="true">
423-
<Config name="Debug|x64" />
424-
</File>
425-
</Folder>
426-
<Folder name="Release_Win32">
427-
<File tool="None" path="..\..\generated\Windows\Release\Win32\generated\ddmd\id.d" perConfig="true">
428-
<Config name="Release|Win32" />
429-
</File>
430-
</Folder>
431-
<Folder name="Release_x64">
432-
<File tool="None" path="..\..\generated\Windows\Release\x64\generated\ddmd\id.d" perConfig="true">
433-
<Config name="Release|x64" />
434-
</File>
435-
</Folder>
436-
</Folder>
437415
<Folder name="ddmd">
438416
<Folder name="backend">
439417
<File path="..\ddmd\backend\bcomplex.d" />
@@ -526,6 +504,7 @@
526504
<File path="..\ddmd\gluelayer.d" />
527505
<File path="..\ddmd\hdrgen.d" />
528506
<File path="..\ddmd\iasm.d" />
507+
<File path="..\ddmd\id.d" />
529508
<File path="..\ddmd\identifier.d" />
530509
<File path="..\ddmd\impcnvtab.d" />
531510
<File path="..\ddmd\imphint.d" />

src/vcbuild/dmd_backend.vcxproj

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -158,21 +158,9 @@
158158
</ClCompile>
159159
<ClCompile Include="..\ddmd\root\longdouble.c" />
160160
<ClCompile Include="..\ddmd\root\newdelete.c" />
161-
<CustomBuild Include="..\ddmd\idgen.d">
162-
<Message>Building and running $(IntDir)%(Filename).exe</Message>
163-
<Outputs>$(IntDir)generated\ddmd\id.d;$(IntDir)generated\ddmd\id.c;$(IntDir)generated\ddmd\id.h;$(IntDir)generated\%(Filename).exe;%(Outputs)</Outputs>
164-
<Command>set DMDDir=$(DMDBinDir)
165-
set PATH=%DMDDir%;%PATH%
166-
dmd -od$(IntDir)generated -of$(IntDir)generated\%(Filename).exe %(Identity)
167-
if errorlevel 1 exit /B %ERRORLEVEL%
168-
pushd $(IntDir)generated
169-
%(Filename).exe
170-
if errorlevel 1 exit /B %ERRORLEVEL%
171-
popd</Command>
172-
</CustomBuild>
173161
<CustomBuild Include="..\ddmd\backend\optabgen.c">
174162
<Message>Building and running $(IntDir)%(Filename).exe</Message>
175-
<Command>cl /TP /I..\ddmd\tk /I..\ddmd\root /I. /I.. /FIwarnings.h /Fo"$(IntDir)%(Filename).obj" /Fe"$(IntDir)generated\%(Filename).exe" "%(FullPath)"
163+
<Command>cl /TP /I..\ddmd\tk /I..\ddmd\root /I. /I.. /FIwarnings.h /Fo"$(IntDir)%(Filename).obj" /Fe"$(IntDir)generated\%(Filename).exe" "%(FullPath)"
176164
if errorlevel 1 exit /B %ERRORLEVEL%
177165
pushd $(IntDir)generated
178166
"%(Filename).exe"
@@ -267,4 +255,4 @@ popd</Command>
267255
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
268256
<ImportGroup Label="ExtensionTargets">
269257
</ImportGroup>
270-
</Project>
258+
</Project>

src/win32.mak

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ FRONT_SRCS=$D/access.d $D/aggregate.d $D/aliasthis.d $D/apply.d $D/argtypes.d $D
159159
$D/declaration.d $D/delegatize.d $D/denum.d $D/dimport.d $D/dinifile.d $D/dinterpret.d \
160160
$D/dmacro.d $D/dmangle.d $D/dmodule.d $D/doc.d $D/dscope.d $D/dstruct.d $D/dsymbol.d \
161161
$D/dtemplate.d $D/dversion.d $D/escape.d \
162-
$D/expression.d $D/func.d $D/hdrgen.d $D/imphint.d \
162+
$D/expression.d $D/func.d $D/hdrgen.d $D/id.d $D/imphint.d \
163163
$D/impcnvtab.d $D/init.d $D/inline.d $D/inlinecost.d $D/intrange.d $D/json.d $D/lib.d $D/link.d \
164164
$D/mars.d $D/mtype.d $D/nogc.d $D/nspace.d $D/objc.d $D/opover.d $D/optimize.d $D/parse.d \
165165
$D/sapply.d $D/sideeffect.d $D/statement.d $D/staticassert.d $D/target.d \
@@ -334,7 +334,7 @@ clean:
334334
$(RD) /s /q $(GEN)
335335
$(DEL) $D\msgs.h $D\msgs.c
336336
$(DEL) optabgen.exe
337-
$(DEL) $(TARGETEXE) $(DMDFRONTENDEXE) $(IDGENOUTPUT) *.map *.obj
337+
$(DEL) $(TARGETEXE) $(DMDFRONTENDEXE) *.map *.obj
338338

339339
install: detab install-copy
340340

@@ -404,7 +404,6 @@ $(TOOLS_DIR)\checkwhitespace.d:
404404

405405
############################## Generated Source ##############################
406406
OPTABGENOUTPUT = $G\elxxx.c $G\cdxxx.c $G\optab.c $G\debtab.c $G\fltables.c $G\tytab.c
407-
IDGENOUTPUT = $D/id.d $D/id.h
408407

409408
$(OPTABGENOUTPUT) : \
410409
$C\cdef.h $C\cc.h $C\oper.h $C\ty.h $C\optabgen.c
@@ -413,10 +412,6 @@ $(OPTABGENOUTPUT) : \
413412
copy *.c "$G\"
414413
$(DEL) *.c
415414

416-
$(IDGENOUTPUT) : $D\idgen.d
417-
$(HOST_DC) -of$G\idgen $D\idgen.d
418-
$G/idgen
419-
420415
$G\VERSION : ..\VERSION
421416
copy ..\VERSION $@
422417

0 commit comments

Comments
 (0)