Skip to content

flesh out How ImportC Works section #3070

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

Merged
merged 1 commit into from
Mar 3, 2022
Merged
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
2 changes: 1 addition & 1 deletion spec/betterc.dd
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Hello betterC
be tricky, hence removing D's GC from the equation may be worthwhile sometimes.)
)

$(P BetterC and $(LINK2 https://dlang.org/spec/importc.html) are very different.
$(P BetterC and $(LINK2 https://dlang.org/spec/importc.html, ImportC) are very different.
ImportC is an actual C compiler. BetterC is a subset of D that relies only on the
existence of the C Standard library.)

Expand Down
19 changes: 16 additions & 3 deletions spec/importc.dd
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ $(HEADERNAV_TOC)
It describes the specifics of the dialect of C that
ImportC is, and how to use it effectively.)

$(P ImportC and $(LINK2 https://dlang.org/spec/betterc.html) are very different.
$(P ImportC and $(LINK2 https://dlang.org/spec/betterc.html, BetterC) are very different.
ImportC is an actual C compiler. BetterC is a subset of D that relies only on the
existence of the C Standard library.)

Expand Down Expand Up @@ -300,8 +300,21 @@ $(H2 $(LNAME2 other-solutions, Other Solutions))

$(H2 $(LNAME2 internals, How ImportC Works))

$(P This is a description of how ImportC is implemented, intended
to remove the mystery of various design choices.)
$(P ImportC's implementation is based on the idea that D's semantics are very similar
to C's. ImportC gets its own parser, which converts the C syntax into the same AST
(Abstract Syntax Tree) that D uses. The lexer for ImportC is the same as for D, but with
some modifications here and there, such as the keywords and integer literals being different.
Where the semantics of C differ from D, there are adjustments in the semantic analysis
code in the D compiler.)

$(P This co-opting of the D semantic implementation allows ImportC to be able to do things
like handle forward references, CTFE (Compile Time Function Execution), and inlining of C functions
into D code. Being able to handle forward references means it is not necessary to even
write a .h file to be able to import C declarations into D. Being able to perform CTFE is
very handy for testing that ImportC is working without needing to generate an executable.
But, in general, the strong temptation to add D features to ImportC has been resisted.)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What "strong temptation to add D features"? I thought it was very clear from the start that the goal was to import existing C code, not write new C with D features mixed in.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is clear. But I am tempted, and people will ask "why not" add some.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What kind of D features are you tempted to add to ImportC? I think this paragraph leaves some open questions to the reader, another example:

Being able to perform CTFE is very handy for testing that ImportC is working without needing to generate an executable.

Why is this handy?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What kind of D features are you tempted to add to ImportC?

Like make . work as a substitute for ->

Why is this handy?

The test suite runs much faster if it doesn't have to generate an executable.


$(P The optimizer and code generator are, of course, the same as D uses.)

$(SPEC_SUBNAV_PREV_NEXT betterc, Better C, ob, Live Functions)
)
Expand Down