Skip to content

Implement partial support for #define objects#302

Open
davispuh wants to merge 1 commit intojacob-carlborg:masterfrom
davispuh:objects
Open

Implement partial support for #define objects#302
davispuh wants to merge 1 commit intojacob-carlborg:masterfrom
davispuh:objects

Conversation

@davispuh
Copy link
Copy Markdown

Consider common C pattern like:

struct S
{
  int a;
  int b;
};

#define S_INIT { 1, 2 }

int main()
{
   struct S s = S_INIT;
}

Currently dstep will ignore S_INIT which loses this "init" information.
This PR implements partial support so that

enum S_INIT = null; // FIXME: {1,2};

Will be produced, which allows to create simple shell script with simple sed command to afterwards fix up these case with something like this:

$ sed -iE 's|null; // FIXME: {\([^}]*\)}|S(\1)|g' file.d

Which will make final version to be:

enum S_INIT = S(1,2);

And that will work correctly when S_INIT is used.
Unfortunately this 2nd step with sed or another processing tool is needed because implementing usage-aware processing inside dstep is not trivial and would require significant effort to also parse macro usages so that's why I'm settling on this partial support.

Copilot AI review requested due to automatic review settings March 19, 2026 17:03
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds partial handling for object-like #define macros that expand to C braced initializer lists (e.g. {1,2}), so dstep can preserve that initializer information in generated D output (as a placeholder with a FIXME marker).

Changes:

  • Extend the macro expression AST with a new braced-initializer expression node (ObjectExpr).
  • Parse { ... } token sequences as a standalone expression in MacroParser.
  • Translate this expression into a placeholder D value while retaining the original braced text in a FIXME comment.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.

File Description
dstep/translator/TypeInference.d Treats the new braced-initializer expression as Generic during type inference.
dstep/translator/MacroParser.d Introduces ObjectExpr and parsing logic for { ... } object-style macro bodies.
dstep/translator/MacroDefinition.d Adds translation support for ObjectExpr into a placeholder value with a FIXME marker.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.


string translate(ObjectExpr expr, ExpressionContext context)
{
return "null; // FIXME: " ~ expr.expr;
Comment on lines +506 to +512
struct ObjectExpr
{
string expr;

this (string expr)
{
this.expr = expr;
Comment on lines +1055 to +1063
import std.algorithm.iteration : fold;

if (!tokens.empty &&
tokens.front.kind == TokenKind.punctuation &&
tokens.front.spelling == "{" &&
tokens.back.kind == TokenKind.punctuation &&
tokens.back.spelling == "}")
{
auto expr = Expression(ObjectExpr(tokens.fold!((str, token) => str ~= token.spelling)("")));
Comment on lines +1468 to +1471
auto objExpr = parseObjectExpr(tokens);
if (objExpr.hasValue)
return objExpr;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants