Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement pragma compile on import bugzilla #24869 #16848

Open
wants to merge 1 commit into
base: stable
Choose a base branch
from
Open
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
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 @@
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;
}
Loading