Skip to content

Commit

Permalink
Implement pragma compile on import bugzilla #24869
Browse files Browse the repository at this point in the history
  • Loading branch information
rikkimax committed Sep 18, 2024
1 parent 219b4f0 commit 468d3bf
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 12 deletions.
30 changes: 30 additions & 0 deletions changelog/dmd.pragma.compileonimport.dd
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Add a new pragma to compile on import

The pragma ``compileOnImport``, will convert an imported module into one that will be compiled into your binary.

This is the same behavior as the ``-i`` switch, except it is a pragma in a module.

A library module:

```d
module mylib;
pragma(compileOnImport);

void callMe() {
import std.stdio;
writeln("Hello!");
}
```

The user module:

```d
module theuser;
import mylib;

void main() {
callMe(); // Hello!
}
```

Compile with ``dmd -I. theuser.d``
1 change: 1 addition & 0 deletions compiler/src/dmd/frontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -8583,6 +8583,7 @@ struct Id final
static Identifier* startaddress;
static Identifier* crt_constructor;
static Identifier* crt_destructor;
static Identifier* compileOnImport;
static Identifier* tohash;
static Identifier* tostring;
static Identifier* getmembers;
Expand Down
1 change: 1 addition & 0 deletions compiler/src/dmd/id.d
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ immutable Msgtable[] msgtable =
{ "startaddress" },
{ "crt_constructor" },
{ "crt_destructor" },
{ "compileOnImport" },

// For special functions
{ "tohash", "toHash" },
Expand Down
24 changes: 12 additions & 12 deletions compiler/src/dmd/main.d
Original file line number Diff line number Diff line change
Expand Up @@ -567,20 +567,20 @@ private int tryMain(size_t argc, const(char)** argv, ref Param params)
message("semantic3 %s", m.toChars());
m.semantic3(null);
}
if (includeImports)

// Will be empty if no module has pragma(compileOnImport) or -i is passed to compiler.
// 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++)
{
// 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);
}
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);
Expand Down
12 changes: 12 additions & 0 deletions compiler/src/dmd/pragmasem.d
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,18 @@ void pragmaDeclSemantic(PragmaDeclaration pd, Scope* sc)
.error(pd.loc, "%s `%s` takes no argument", pd.kind, pd.toPrettyChars);
return declarations();
}
else if (pd.ident == Id.compileOnImport)
{
import dmd.compiler;

if (!sc._module.isRoot())
{
sc._module.importedFrom = sc._module;
compiledImports.push(sc._module);
}

return declarations();
}
else if (!global.params.ignoreUnsupportedPragmas)
{
error(pd.loc, "unrecognized `pragma(%s)`", pd.ident.toChars());
Expand Down
10 changes: 10 additions & 0 deletions compiler/test/runnable/compileonimport.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
REQUIRED_ARGS: -Irunnable/imports
*/
module compileonimport;
import compileonimportlib;

void main() {
bool v = runMe();
assert(v);
}
7 changes: 7 additions & 0 deletions compiler/test/runnable/imports/compileonimportlib.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module compileonimportlib;

pragma(compileOnImport);

bool runMe() {
return true;
}

0 comments on commit 468d3bf

Please sign in to comment.