Skip to content

Commit

Permalink
Fix #287: Add support for adding public imports
Browse files Browse the repository at this point in the history
  • Loading branch information
jacob-carlborg committed Aug 5, 2023
1 parent 0ae1b9b commit 2cb53c1
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 1 deletion.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Automatically detect macOS SDK path ([Issue 227](https://github.com/jacob-carlborg/dstep/issues/227))
* Add support for FreeBSD
* Drop support for Windows 32 bit
* [Issue 287](https://github.com/jacob-carlborg/dstep/issues/287): Add support for adding public imports

### Bugs Fixed

Expand Down
5 changes: 5 additions & 0 deletions dstep/Configuration.d
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ struct Configuration
@("global-import", "Add <import> as a global import.")
string[] globalImports;

/// add global imports
@("public-global-import", "Add <import> as a public global import.")
string[] publicGlobalImports;

Options toOptions(string inputFile, string outputFile) const
{
Options options = toOptions();
Expand Down Expand Up @@ -145,6 +149,7 @@ struct Configuration
options.collisionAction = collisionAction;
options.globalAttributes = globalAttributes;
options.globalImports = globalImports;
options.publicGlobalImports = publicGlobalImports;

return options;
}
Expand Down
3 changes: 3 additions & 0 deletions dstep/translator/Context.d
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ class Context
foreach (item; options.globalImports)
includeHandler_.addImport(item);

foreach (item; options.publicGlobalImports)
includeHandler_.addImport(item, Visibility.public_);

this.options = options;

if (options.enableComments)
Expand Down
24 changes: 23 additions & 1 deletion dstep/translator/IncludeHandler.d
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,18 @@ import dstep.translator.HeaderIndex;
import dstep.translator.Options;
import dstep.translator.Output;

enum Visibility
{
public_ = "public"
}

class IncludeHandler
{
private Options options;
private string[string] submodules;
private bool[string] includes;
private bool[string] imports;
private Set!string publicImports;
private HeaderIndex headerIndex;
immutable static string[string] knownIncludes;

Expand Down Expand Up @@ -149,6 +155,16 @@ class IncludeHandler
imports[imp] = true;
}

void addImport (string imp, Visibility visibility)
{
final switch (visibility)
{
case Visibility.public_:
publicImports.add(imp);
break;
}
}

void addCompatible ()
{
includes["config.h"] = true;
Expand All @@ -173,9 +189,10 @@ class IncludeHandler
unhandled.add(format(`/+ #include "%s" +/`, entry));
}

const publicExtra = publicImports.byKey.map!(e => toImport(e, Visibility.public_)).array;
auto extra = imports.byKey.map!(e => toImport(e)).array;

importsBlock(output, standard.keys ~ extra.array);
importsBlock(output, standard.keys ~ publicExtra ~ extra.array);
importsBlock(output, package_.keys);

if (options.keepUntranslatable)
Expand Down Expand Up @@ -216,6 +233,11 @@ private:
return "import " ~ str ~ ";";
}

string toImport (string str, Visibility visibility)
{
return visibility ~ " " ~ toImport(str);
}

string toSubmoduleImport (string str)
{
if (options.publicSubmodules)
Expand Down
1 change: 1 addition & 0 deletions dstep/translator/Options.d
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ struct Options
CollisionAction collisionAction = CollisionAction.rename;
const(string)[] globalAttributes;
const(string)[] globalImports;
const(string)[] publicGlobalImports;
bool delegate(ref const(Cursor)) isWantedCursorForTypedefs;

string toString() const
Expand Down
19 changes: 19 additions & 0 deletions tests/unit/UnitTests.d
Original file line number Diff line number Diff line change
Expand Up @@ -1054,3 +1054,22 @@ enum some_minus_0700L = -octal!700L;
D");

}

@("public global import")
unittest
{
Options options;
options.publicGlobalImports = ["foo.a", "foo.b"];

assertTranslates(q"C
void foo();
C", q"D
public import foo.a;
public import foo.b;
extern (C):
void foo ();
D", options);

}

0 comments on commit 2cb53c1

Please sign in to comment.