diff --git a/src/dmd/compiler.d b/src/dmd/compiler.d index eca29e2a5584..adc5853f2ae1 100644 --- a/src/dmd/compiler.d +++ b/src/dmd/compiler.d @@ -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; @@ -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. diff --git a/src/dmd/compiler.h b/src/dmd/compiler.h index 2c7ef0838e38..76be583eed19 100644 --- a/src/dmd/compiler.h +++ b/src/dmd/compiler.h @@ -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; @@ -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 *); }; diff --git a/src/dmd/dsymbolsem.d b/src/dmd/dsymbolsem.d index edb3e6655440..1474776eb496 100644 --- a/src/dmd/dsymbolsem.d +++ b/src/dmd/dsymbolsem.d @@ -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: @@ -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()) + { + // 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); diff --git a/src/dmd/glue.d b/src/dmd/glue.d index f9808b197314..364a6c5ad69e 100644 --- a/src/dmd/glue.d +++ b/src/dmd/glue.d @@ -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()); diff --git a/src/dmd/id.d b/src/dmd/id.d index 9881e4444786..8c66c4c15dfb 100644 --- a/src/dmd/id.d +++ b/src/dmd/id.d @@ -339,7 +339,7 @@ immutable Msgtable[] msgtable = { "main" }, { "WinMain" }, { "DllMain" }, - { "entrypoint", "__entrypoint" }, + { "CMain", "_d_cmain" }, { "rt_init" }, { "__cmp" }, { "__equals"}, diff --git a/src/dmd/mars.d b/src/dmd/mars.d index 32672128dfaf..62955cb1a35a 100644 --- a/src/dmd/mars.d +++ b/src/dmd/mars.d @@ -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) { @@ -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) diff --git a/test/runnable/extra-files/minimal/object.d b/test/runnable/extra-files/minimal/object.d index a03961c491b6..bc8b3729739e 100644 --- a/test/runnable/extra-files/minimal/object.d +++ b/test/runnable/extra-files/minimal/object.d @@ -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(); }