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 Oct 30, 2024
1 parent 219b4f0 commit 02bf9ae
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 @@
A new pragma is added that will compile a module 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());

Check warning on line 579 in compiler/src/dmd/main.d

View check run for this annotation

Codecov / codecov/patch

compiler/src/dmd/main.d#L579

Added line #L579 was not covered by tests
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 02bf9ae

Please sign in to comment.