Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 0 additions & 57 deletions src/dmd/compiler.d
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,6 @@ import dmd.tokens;

extern (C++) __gshared
{
/// DMD-generated module `__entrypoint` where the C main resides
Module entrypoint = null;
/// Module in which the D main is
Module rootHasMain = null;

Expand All @@ -50,61 +48,6 @@ extern (C++) __gshared
*/
extern (C++) struct Compiler
{
/**
* Generate C main() in response to seeing D main().
*
* This function will generate a module called `__entrypoint`,
* and set the globals `entrypoint` and `rootHasMain`.
*
* This used to be in druntime, but contained a reference to _Dmain
* which didn't work when druntime was made into a dll and was linked
* to a program, such as a C++ program, that didn't have a _Dmain.
*
* Params:
* sc = Scope which triggered the generation of the C main,
* used to get the module where the D main is.
*/
extern (C++) static void genCmain(Scope* sc)
{
if (entrypoint)
return;
/* The D code to be generated is provided as D source code in the form of a string.
* Note that Solaris, for unknown reasons, requires both a main() and an _main()
*/
immutable cmaincode =
q{
extern(C)
{
int _d_run_main(int argc, char **argv, void* mainFunc);
int _Dmain(char[][] args);
int main(int argc, char **argv)
{
return _d_run_main(argc, argv, &_Dmain);
}
version (Solaris) int _main(int argc, char** argv) { return main(argc, argv); }
}
};
Identifier id = Id.entrypoint;
auto m = new Module("__entrypoint.d", id, 0, 0);
scope diagnosticReporter = new StderrDiagnosticReporter(global.params.useDeprecated);
scope p = new Parser!ASTCodegen(m, cmaincode, false, diagnosticReporter);
p.scanloc = Loc.initial;
p.nextToken();
m.members = p.parseModule();
assert(p.token.value == TOK.endOfFile);
assert(!p.errors); // shouldn't have failed to parse it
bool v = global.params.verbose;
global.params.verbose = false;
m.importedFrom = m;
m.importAll(null);
m.dsymbolSemantic(null);
m.semantic2(null);
m.semantic3(null);
global.params.verbose = v;
entrypoint = m;
rootHasMain = sc._module;
}

/******************************
* Encode the given expression, which is assumed to be an rvalue literal
* as another type for use in CTFE.
Expand Down
3 changes: 0 additions & 3 deletions src/dmd/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ class Type;
struct Scope;
struct UnionExp;

// DMD-generated module `__entrypoint` where the C main resides
extern Module *entrypoint;
// Module in which the D main is
extern Module *rootHasMain;

Expand All @@ -37,6 +35,5 @@ struct Compiler
static Expression *paintAsType(UnionExp *, Expression *, Type *);
// Backend
static void loadModule(Module *);
static void genCmain(Scope *);
static bool onImport(Module *);
};
25 changes: 23 additions & 2 deletions src/dmd/dsymbolsem.d
Original file line number Diff line number Diff line change
Expand Up @@ -1685,7 +1685,6 @@ private extern(C++) final class DsymbolSemanticVisitor : Visitor
// https://issues.dlang.org/show_bug.cgi?id=11117
// https://issues.dlang.org/show_bug.cgi?id=11164
if (global.params.moduleDeps !is null && !(imp.id == Id.object && sc._module.ident == Id.object) &&
sc._module.ident != Id.entrypoint &&
strcmp(sc._module.ident.toChars(), "__main") != 0)
{
/* The grammar of the file is:
Expand Down Expand Up @@ -3992,7 +3991,29 @@ private extern(C++) final class DsymbolSemanticVisitor : Visitor
}

if (funcdecl.fbody && funcdecl.isMain() && sc._module.isRoot())
Compiler.genCmain(sc);
{
// check if `_d_cmain` is defined
bool cmainTemplateExists()
{
auto rootSymbol = sc.search(funcdecl.loc, Id.empty, null);
if (auto moduleSymbol = rootSymbol.search(funcdecl.loc, Id.object))
if (moduleSymbol.search(funcdecl.loc, Id.CMain))
return true;

return false;
}

// Only mixin `_d_cmain` if it is defined
if (cmainTemplateExists())
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose I could put this logic back into compiler.genCmain if that would be less disruptive.

{
// add `mixin _d_cmain!();` to the declaring module
auto tqual = new TypeIdentifier(funcdecl.loc, Id.CMain);
auto tm = new TemplateMixin(funcdecl.loc, null, tqual, null);
sc._module.members.push(tm);
}

rootHasMain = sc._module;
}

assert(funcdecl.type.ty != Terror || funcdecl.errors);

Expand Down
15 changes: 0 additions & 15 deletions src/dmd/glue.d
Original file line number Diff line number Diff line change
Expand Up @@ -325,21 +325,6 @@ void genObjFile(Module m, bool multiobj)

//printf("Module.genobjfile(multiobj = %d) %s\n", multiobj, m.toChars());

if (m.ident == Id.entrypoint)
{
bool v = global.params.verbose;
global.params.verbose = false;

foreach (member; *m.members)
{
//printf("toObjFile %s %s\n", member.kind(), member.toChars());
toObjFile(member, multiobj);
}

global.params.verbose = v;
return;
}

lastmname = m.srcfile.toChars();

objmod.initfile(lastmname, null, m.toPrettyChars());
Expand Down
2 changes: 1 addition & 1 deletion src/dmd/id.d
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ immutable Msgtable[] msgtable =
{ "main" },
{ "WinMain" },
{ "DllMain" },
{ "entrypoint", "__entrypoint" },
{ "CMain", "_d_cmain" },
{ "rt_init" },
{ "__cmp" },
{ "__equals"},
Expand Down
4 changes: 0 additions & 4 deletions src/dmd/mars.d
Original file line number Diff line number Diff line change
Expand Up @@ -702,8 +702,6 @@ private int tryMain(size_t argc, const(char)** argv, ref Param params)
if (params.verbose)
message("code %s", m.toChars());
genObjFile(m, false);
if (entrypoint && m == rootHasMain)
genObjFile(entrypoint, false);
}
if (!global.errors && firstm)
{
Expand All @@ -720,8 +718,6 @@ private int tryMain(size_t argc, const(char)** argv, ref Param params)
message("code %s", m.toChars());
obj_start(m.srcfile.toChars());
genObjFile(m, params.multiobj);
if (entrypoint && m == rootHasMain)
genObjFile(entrypoint, params.multiobj);
obj_end(library, m.objfile.toChars());
obj_write_deferred(library);
if (global.errors && !params.lib)
Expand Down
7 changes: 4 additions & 3 deletions test/runnable/extra-files/minimal/object.d
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module object;

private alias extern(C) int function(char[][] args) MainFunc;
private extern (C) int _d_run_main(int argc, char** argv, MainFunc mainFunc)
extern(C) void _Dmain();

extern(C) void main()
{
return mainFunc(null);
_Dmain();
}