Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 20 additions & 4 deletions compiler/src/dmd/dtoh.d
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,13 @@ public:
}

/// Checks whether `t` is a type that can be exported to C++
private static bool containsSizeT(AST.Type t)
{
while (auto tp = t.isTypePointer())
t = tp.next;
return t == AST.Type.tsize_t || t == AST.Type.tptrdiff_t;
}

private bool isSupportedType(AST.Type t)
{
if (!t)
Expand Down Expand Up @@ -951,7 +958,7 @@ public:
return;
}

if (vd.originalType && vd.type == AST.Type.tsize_t)
if (vd.originalType && containsSizeT(vd.type))
origType = vd.originalType;
scope(exit) origType = null;

Expand Down Expand Up @@ -1131,9 +1138,10 @@ public:
return;
}

// for function pointers we need to original type
if (ad.originalType && ad.type.ty == AST.Tpointer &&
(cast(AST.TypePointer)t).nextOf.ty == AST.Tfunction)
// for function pointers and size_t/ptrdiff_t we need the original type
if (ad.originalType &&
((ad.type.ty == AST.Tpointer && (cast(AST.TypePointer)t).nextOf.ty == AST.Tfunction) ||
containsSizeT(ad.type)))
{
origType = ad.originalType;
}
Expand Down Expand Up @@ -1414,7 +1422,10 @@ public:
buf.writestring(", ");
assert(vd.type);
assert(vd.ident);
if (vd.originalType && containsSizeT(vd.type))
origType = vd.originalType;
typeToBuffer(vd.type, vd, true);
origType = null;
// Don't print default value for first parameter to not clash
// with the default ctor defined above
if (!first)
Expand Down Expand Up @@ -3012,6 +3023,11 @@ public:
*/
private void ensureDeclared(AST.Dsymbol sym)
{
// size_t and ptrdiff_t are provided by #include <stddef.h>
if (auto ad = sym.isAliasDeclaration())
if (ad.ident == Id._size_t || ad.ident == Id._ptrdiff_t)
return;

auto par = sym.toParent2();
auto ed = sym.isEnumDeclaration();

Expand Down
6 changes: 3 additions & 3 deletions compiler/src/dmd/frontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -676,22 +676,22 @@ class Dsymbol : public ASTNode

struct BitArray final
{
typedef uint64_t Chunk_t;
typedef size_t Chunk_t;
enum : uint64_t { ChunkSize = 8LLU };

enum : uint64_t { BitsPerChunk = 64LLU };

private:
~BitArray();
size_t len;
uint64_t* ptr;
size_t* ptr;
public:
BitArray() :
len(),
ptr()
{
}
BitArray(uint64_t len, uint64_t* ptr = nullptr) :
BitArray(size_t len, size_t* ptr = nullptr) :
len(len),
ptr(ptr)
{}
Expand Down
48 changes: 48 additions & 0 deletions compiler/test/compilable/dtoh_18015.d
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
REQUIRED_ARGS: -HC -c -o-
PERMUTE_ARGS:
TEST_OUTPUT:
---
// Automatically generated by Digital Mars D Compiler

#pragma once

#include <assert.h>
#include <math.h>
#include <stddef.h>
#include <stdint.h>

struct BitArray final
{
typedef size_t Chunk_t;
size_t len;
size_t* ptr;
BitArray() :
len(),
ptr()
{
}
BitArray(size_t len, size_t* ptr = nullptr) :
len(len),
ptr(ptr)
{}
};

typedef ptrdiff_t MyDiff;

---
*/

// https://github.com/dlang/dmd/issues/18015
// dtoh: Insufficient size_t/ptrdiff_t detection

extern(C++):

struct BitArray
{
alias Chunk_t = size_t;
size_t len;
size_t* ptr;
}

alias MyDiff = ptrdiff_t;
Loading