Skip to content

Commit

Permalink
Reduce size of StructLiteralExp (#20779)
Browse files Browse the repository at this point in the history
  • Loading branch information
dkorpel authored Jan 25, 2025
1 parent 63630b4 commit 0ce5951
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 32 deletions.
25 changes: 18 additions & 7 deletions compiler/src/dmd/expression.d
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,14 @@ extern (C++) abstract class Expression : ASTNode

Loc loc; // file location
const EXP op; // to minimize use of dynamic_cast
bool parens; // if this is a parenthesized expression
bool rvalue; // true if this is considered to be an rvalue, even if it is an lvalue

static struct BitFields
{
bool parens; // if this is a parenthesized expression
bool rvalue; // true if this is considered to be an rvalue, even if it is an lvalue
}
import dmd.common.bitfields;
mixin(generateBitFields!(BitFields, ubyte));

extern (D) this(const ref Loc loc, EXP op) scope @safe
{
Expand Down Expand Up @@ -2126,6 +2132,16 @@ extern (C++) final class AssocArrayLiteralExp : Expression
*/
extern (C++) final class StructLiteralExp : Expression
{
struct BitFields
{
bool useStaticInit; /// if this is true, use the StructDeclaration's init symbol
bool isOriginal = false; /// used when moving instances to indicate `this is this.origin`
OwnedBy ownedByCtfe = OwnedBy.code;
}
import dmd.common.bitfields;
mixin(generateBitFields!(BitFields, ubyte));
StageFlags stageflags;

StructDeclaration sd; /// which aggregate this is for
Expressions* elements; /// parallels sd.fields[] with null entries for fields to skip
Type stype; /// final type of result (can be different from sd's type)
Expand Down Expand Up @@ -2163,11 +2179,6 @@ extern (C++) final class StructLiteralExp : Expression
inlineScan = 0x10, /// inlineScan is running
toCBuffer = 0x20 /// toCBuffer is running
}
StageFlags stageflags;

bool useStaticInit; /// if this is true, use the StructDeclaration's init symbol
bool isOriginal = false; /// used when moving instances to indicate `this is this.origin`
OwnedBy ownedByCtfe = OwnedBy.code;

extern (D) this(const ref Loc loc, StructDeclaration sd, Expressions* elements, Type stype = null) @safe
{
Expand Down
37 changes: 24 additions & 13 deletions compiler/src/dmd/expression.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,12 @@ class Expression : public ASTNode
Type *type; // !=NULL means that semantic() has been run
Loc loc; // file location
EXP op; // to minimize use of dynamic_cast
d_bool parens; // if this is a parenthesized expression
d_bool rvalue; // consider this an rvalue, even if it is an lvalue
uint8_t bitFields;

bool parens() const;
bool parens(bool v);
bool rvalue() const;
bool rvalue(bool v);

size_t size() const;
static void _init();
Expand Down Expand Up @@ -431,6 +435,24 @@ class AssocArrayLiteralExp final : public Expression
class StructLiteralExp final : public Expression
{
public:
uint8_t bitFields;

// if this is true, use the StructDeclaration's init symbol
bool useStaticInit() const;
bool useStaticInit(bool v);
// used when moving instances to indicate `this is this.origin`
bool isOriginal() const;
bool isOriginal(bool v);
OwnedBy ownedByCtfe() const;
OwnedBy ownedByCtfe(OwnedBy v);

/** anytime when recursive function is calling, 'stageflags' marks with bit flag of
* current stage and unmarks before return from this function.
* 'inlinecopy' uses similar 'stageflags' and from multiple evaluation 'doInline'
* (with infinite recursion) of this expression.
*/
uint8_t stageflags;

StructDeclaration *sd; // which aggregate this is for
Expressions *elements; // parallels sd->fields[] with NULL entries for fields to skip
Type *stype; // final type of result (can be different from sd's type)
Expand All @@ -451,17 +473,6 @@ class StructLiteralExp final : public Expression
StructLiteralExp *origin;


/** anytime when recursive function is calling, 'stageflags' marks with bit flag of
* current stage and unmarks before return from this function.
* 'inlinecopy' uses similar 'stageflags' and from multiple evaluation 'doInline'
* (with infinite recursion) of this expression.
*/
uint8_t stageflags;

d_bool useStaticInit; // if this is true, use the StructDeclaration's init symbol
d_bool isOriginal; // used when moving instances to indicate `this is this.origin`
OwnedBy ownedByCtfe;

static StructLiteralExp *create(const Loc &loc, StructDeclaration *sd, void *elements, Type *stype = nullptr);
bool equals(const RootObject * const o) const override;
StructLiteralExp *syntaxCopy() override;
Expand Down
68 changes: 56 additions & 12 deletions compiler/src/dmd/frontend.h
Original file line number Diff line number Diff line change
Expand Up @@ -2206,8 +2206,28 @@ class Expression : public ASTNode
Type* type;
Loc loc;
const EXP op;
bool parens;
bool rvalue;
struct BitFields final
{
bool parens;
bool rvalue;
BitFields() :
parens(),
rvalue()
{
}
BitFields(bool parens, bool rvalue = false) :
parens(parens),
rvalue(rvalue)
{}
};

bool parens() const;
bool parens(bool v);
bool rvalue() const;
bool rvalue(bool v);
private:
uint8_t bitFields;
public:
size_t size() const;
static void _init();
static void deinitialize();
Expand Down Expand Up @@ -3450,6 +3470,34 @@ class StringExp final : public Expression
class StructLiteralExp final : public Expression
{
public:
struct BitFields final
{
bool useStaticInit;
bool isOriginal;
OwnedBy ownedByCtfe;
BitFields() :
useStaticInit(),
isOriginal(false),
ownedByCtfe((OwnedBy)0u)
{
}
BitFields(bool useStaticInit, bool isOriginal = false, OwnedBy ownedByCtfe = (OwnedBy)0u) :
useStaticInit(useStaticInit),
isOriginal(isOriginal),
ownedByCtfe(ownedByCtfe)
{}
};

bool useStaticInit() const;
bool useStaticInit(bool v);
bool isOriginal() const;
bool isOriginal(bool v);
OwnedBy ownedByCtfe() const;
OwnedBy ownedByCtfe(OwnedBy v);
private:
uint8_t bitFields;
public:
StageFlags stageflags;
StructDeclaration* sd;
Array<Expression* >* elements;
Type* stype;
Expand All @@ -3470,10 +3518,6 @@ class StructLiteralExp final : public Expression
toCBuffer = 32u,
};

StageFlags stageflags;
bool useStaticInit;
bool isOriginal;
OwnedBy ownedByCtfe;
static StructLiteralExp* create(const Loc& loc, StructDeclaration* sd, void* elements, Type* stype = nullptr);
bool equals(const RootObject* const o) const override;
StructLiteralExp* syntaxCopy() override;
Expand Down Expand Up @@ -5358,18 +5402,18 @@ struct UnionExp final
private:
union _AnonStruct_u
{
char exp[31LLU];
char exp[30LLU];
char integerexp[40LLU];
char errorexp[31LLU];
char errorexp[30LLU];
char realexp[48LLU];
char complexexp[64LLU];
char symoffexp[64LLU];
char stringexp[59LLU];
char arrayliteralexp[56LLU];
char stringexp[51LLU];
char arrayliteralexp[48LLU];
char assocarrayliteralexp[56LLU];
char structliteralexp[76LLU];
char structliteralexp[72LLU];
char compoundliteralexp[40LLU];
char nullexp[31LLU];
char nullexp[30LLU];
char dotvarexp[49LLU];
char addrexp[40LLU];
char indexexp[58LLU];
Expand Down

0 comments on commit 0ce5951

Please sign in to comment.