Skip to content

Commit

Permalink
Fix #20889 - ImportC: Usage of typedef types decays to original type (
Browse files Browse the repository at this point in the history
  • Loading branch information
dkorpel authored Feb 20, 2025
1 parent 54a1424 commit 1d8658d
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 4 deletions.
6 changes: 6 additions & 0 deletions compiler/src/dmd/cparse.d
Original file line number Diff line number Diff line change
Expand Up @@ -5526,6 +5526,12 @@ final class CParser(AST) : Parser!AST
if (pt && *pt)
t = *pt;
}
if (t.mcache && t.mcache.typedefIdent)
{
t = t.copy();
t.mcache = null;
}
t.getMcache().typedefIdent = id;
auto tab = cast(void*[void*])(typedefTab[$ - 1]);
tab[cast(void*)id] = cast(void*)t;
typedefTab[$ - 1] = cast(void*)tab;
Expand Down
14 changes: 10 additions & 4 deletions compiler/src/dmd/frontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -1830,6 +1830,7 @@ class Type : public ASTNode
Type* pto;
Type* rto;
Type* arrayof;
Identifier* typedefIdent;
Mcache() :
cto(),
ito(),
Expand All @@ -1841,10 +1842,11 @@ class Type : public ASTNode
swcto(),
pto(),
rto(),
arrayof()
arrayof(),
typedefIdent()
{
}
Mcache(Type* cto, Type* ito = nullptr, Type* sto = nullptr, Type* scto = nullptr, Type* wto = nullptr, Type* wcto = nullptr, Type* swto = nullptr, Type* swcto = nullptr, Type* pto = nullptr, Type* rto = nullptr, Type* arrayof = nullptr) :
Mcache(Type* cto, Type* ito = nullptr, Type* sto = nullptr, Type* scto = nullptr, Type* wto = nullptr, Type* wcto = nullptr, Type* swto = nullptr, Type* swcto = nullptr, Type* pto = nullptr, Type* rto = nullptr, Type* arrayof = nullptr, Identifier* typedefIdent = nullptr) :
cto(cto),
ito(ito),
sto(sto),
Expand All @@ -1855,7 +1857,8 @@ class Type : public ASTNode
swcto(swcto),
pto(pto),
rto(rto),
arrayof(arrayof)
arrayof(arrayof),
typedefIdent(typedefIdent)
{}
};

Expand Down Expand Up @@ -4066,6 +4069,7 @@ struct HdrGenState final
bool ddoc;
bool fullDump;
bool importcHdr;
bool inCAlias;
bool doFuncBodies;
bool vcg_ast;
bool skipConstraints;
Expand All @@ -4084,6 +4088,7 @@ struct HdrGenState final
ddoc(),
fullDump(),
importcHdr(),
inCAlias(),
doFuncBodies(),
vcg_ast(),
skipConstraints(),
Expand All @@ -4099,11 +4104,12 @@ struct HdrGenState final
inEnumDecl()
{
}
HdrGenState(bool hdrgen, bool ddoc = false, bool fullDump = false, bool importcHdr = false, bool doFuncBodies = false, bool vcg_ast = false, bool skipConstraints = false, bool showOneMember = true, bool errorMsg = false, bool fullQual = false, int32_t tpltMember = 0, int32_t autoMember = 0, int32_t forStmtInit = 0, int32_t insideFuncBody = 0, int32_t insideAggregate = 0, bool declstring = false, EnumDeclaration* inEnumDecl = nullptr) :
HdrGenState(bool hdrgen, bool ddoc = false, bool fullDump = false, bool importcHdr = false, bool inCAlias = false, bool doFuncBodies = false, bool vcg_ast = false, bool skipConstraints = false, bool showOneMember = true, bool errorMsg = false, bool fullQual = false, int32_t tpltMember = 0, int32_t autoMember = 0, int32_t forStmtInit = 0, int32_t insideFuncBody = 0, int32_t insideAggregate = 0, bool declstring = false, EnumDeclaration* inEnumDecl = nullptr) :
hdrgen(hdrgen),
ddoc(ddoc),
fullDump(fullDump),
importcHdr(importcHdr),
inCAlias(inCAlias),
doFuncBodies(doFuncBodies),
vcg_ast(vcg_ast),
skipConstraints(skipConstraints),
Expand Down
8 changes: 8 additions & 0 deletions compiler/src/dmd/hdrgen.d
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ struct HdrGenState
bool ddoc; /// true if generating Ddoc file
bool fullDump; /// true if generating a full AST dump file
bool importcHdr; /// true if generating a .di file from an ImportC file
bool inCAlias; /// Set to prevent ImportC translating typedefs as `alias X = X`
bool doFuncBodies; /// include function bodies in output
bool vcg_ast; /// write out codegen-ast
bool skipConstraints; // skip constraints when doing templates
Expand Down Expand Up @@ -1777,7 +1778,9 @@ void toCBuffer(Dsymbol s, ref OutBuffer buf, ref HdrGenState hgs)
buf.writestring(" = ");
if (stcToBuffer(buf, d.storage_class))
buf.writeByte(' ');
hgs.inCAlias = hgs.importcHdr;
typeToBuffer(d.type, null, buf, hgs);
hgs.inCAlias = false;
hgs.declstring = false;
}
buf.writeByte(';');
Expand Down Expand Up @@ -4449,6 +4452,11 @@ private void typeToBufferx(Type t, ref OutBuffer buf, ref HdrGenState hgs)
buf.writestring("noreturn");
}

if (hgs.importcHdr && !hgs.inCAlias && t.mcache && t.mcache.typedefIdent)
{
buf.writestring(t.mcache.typedefIdent.toString());
return;
}

switch (t.ty)
{
Expand Down
4 changes: 4 additions & 0 deletions compiler/src/dmd/mtype.d
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,10 @@ extern (C++) abstract class Type : ASTNode
Type pto; // merged pointer to this type
Type rto; // reference to this type
Type arrayof; // array of this type

// ImportC: store the name of the typedef resolving to this type
// So `uint8_t x;` will be printed as `uint8_t x;` and not as the resolved `ubyte x;`
Identifier typedefIdent;
}
Mcache* mcache;

Expand Down
28 changes: 28 additions & 0 deletions compiler/test/compilable/ctod.i
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,18 @@ extern (C)
int x = void;
}
alias weird = int[(cast(foo*)cast(void*)0).x.sizeof];
alias ULONG = ulong;
alias ULONG_Deluxe = ulong;
alias ULONG_PTR = ulong*;
alias Callback = void* function();
struct Test
{
ULONG_Deluxe d = void;
ULONG_Deluxe* p = void;
ULONG_PTR q = void;
Callback cb = void;
}
extern __gshared int[cast(ULONG)3] arr;
/+enum int __DATE__ = 1+/;
/+enum int __TIME__ = 1+/;
/+enum int __TIMESTAMP__ = 1+/;
Expand Down Expand Up @@ -96,3 +108,19 @@ struct foo {
int x;
};
typedef int weird[sizeof(((struct foo *)((void*)0))->x)];

// https://github.com/dlang/dmd/issues/20889
typedef unsigned long long ULONG;
typedef ULONG ULONG_Deluxe;
typedef ULONG_Deluxe *ULONG_PTR;
typedef void *(*Callback)();

struct Test
{
ULONG_Deluxe d;
ULONG_Deluxe *p;
ULONG_PTR q;
Callback cb;
};

int arr[(ULONG) 3];

0 comments on commit 1d8658d

Please sign in to comment.