Skip to content

Commit 965c7b0

Browse files
[dscope.d] compress some flags (#21570)
1 parent 941545f commit 965c7b0

File tree

8 files changed

+48
-44
lines changed

8 files changed

+48
-44
lines changed

compiler/src/dmd/aggregate.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ extern (C++) abstract class AggregateDeclaration : ScopeDsymbol
169169
sc2.parent = this;
170170
sc2.inunion = isUnionDeclaration();
171171
sc2.visibility = Visibility(Visibility.Kind.public_);
172-
sc2.explicitVisibility = 0;
172+
sc2.explicitVisibility = false;
173173
sc2.aligndecl = null;
174174
sc2.userAttribDecl = null;
175175
sc2.namespace = null;

compiler/src/dmd/attrib.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ extern (C++) abstract class AttribDeclaration : Dsymbol
7878
* the scope after it used.
7979
*/
8080
extern (D) static Scope* createNewScope(Scope* sc, STC stc, LINK linkage,
81-
CPPMANGLE cppmangle, Visibility visibility, int explicitVisibility,
81+
CPPMANGLE cppmangle, Visibility visibility, bool explicitVisibility,
8282
AlignDeclaration aligndecl, PragmaDeclaration inlining)
8383
{
8484
Scope* sc2 = sc;

compiler/src/dmd/common/bitfields.d

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@ version(Has_Bitfields)
1919
* Params:
2020
* S = type of a struct with only boolean fields, which should become bit fields
2121
* T = type of bit fields variable, must have enough bits to store all booleans
22+
* field = if provided, assume it is declared and initialized elsewhere
23+
* bitOff = start using bits at the given offset
2224
* Returns: D code with a bit fields variable and getter / setter functions
2325
*/
24-
extern (D) string generateBitFields(S, T, int ID = __LINE__)()
26+
extern (D) string generateBitFields(S, T, string field = "", int bitOff = 0, int ID = __LINE__)()
2527
if (__traits(isUnsigned, T))
2628
{
2729
import core.bitop: bsr;
28-
30+
// if _fieldName provided, assume it declared and initialized elsewhere
31+
enum fieldName = field.length == 0 ? "bitFields" : field;
2932
string result = "extern (C++) pure nothrow @nogc @safe final {";
3033

3134
struct BitInfo
@@ -39,7 +42,7 @@ if (__traits(isUnsigned, T))
3942
// Iterate over members to compute bit offset and bit size for each of them
4043
enum BitInfo bitInfo = () {
4144
BitInfo result;
42-
int bitOffset = 0;
45+
int bitOffset = bitOff;
4346
foreach (size_t i, mem; __traits(allMembers, S))
4447
{
4548
alias memType = typeof(__traits(getMember, S, mem));
@@ -62,7 +65,7 @@ if (__traits(isUnsigned, T))
6265
version(Debugger_friendly)
6366
{
6467
// unique name needed to allow same name as in base class using `alias`, but without overloading
65-
string bitfieldsName = "bitfields" ~ toString!(ID);
68+
string bitfieldsName = fieldName ~ toString!(ID);
6669
string bitfieldsRead = T.stringof~" "~bitfieldsName~"() const pure { return 0";
6770
string bitfieldsWrite = "void "~bitfieldsName~"("~T.stringof~" v) {\n";
6871
}
@@ -86,11 +89,11 @@ if (__traits(isUnsigned, T))
8689
else
8790
{
8891
result ~= "
89-
"~typeName~" "~mem~"() const scope { return cast("~typeName~") ((bitFields >>> "~shift~") & "~sizeMask~"); }
92+
"~typeName~" "~mem~"() const scope { return cast("~typeName~") (("~fieldName~" >>> "~shift~") & "~sizeMask~"); }
9093
"~typeName~" "~mem~"("~typeName~" v) scope
9194
{
92-
bitFields &= ~("~sizeMask~" << "~shift~");
93-
bitFields |= v << "~shift~";
95+
"~fieldName~" &= ~("~sizeMask~" << "~shift~");
96+
"~fieldName~" |= v << "~shift~";
9497
return v;
9598
}";
9699
}
@@ -99,7 +102,8 @@ if (__traits(isUnsigned, T))
99102
{
100103
bitfieldsRead ~= ";\n}\n";
101104
bitfieldsWrite ~= "}\n";
102-
result ~= "alias bitFields = "~bitfieldsName~";\n";
105+
if (field.length == 0)
106+
result ~= "alias "~fieldName~" = "~bitfieldsName~";\n";
103107
result ~= bitfieldsRead ~ bitfieldsWrite;
104108
result ~= "\n}\n";
105109
return result;
@@ -108,7 +112,9 @@ if (__traits(isUnsigned, T))
108112
{
109113
result ~= "\n}\n";
110114
enum TP initVal = bitInfo.initialValue;
111-
return result ~ " private "~T.stringof~" bitFields = " ~ toString!(initVal) ~ ";\n";
115+
if (field.length == 0)
116+
result ~= " private "~T.stringof~" "~fieldName~" = " ~ toString!(initVal) ~ ";\n";
117+
return result;
112118
}
113119
}
114120

compiler/src/dmd/dscope.d

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ enum Contract : ubyte
5555
ensure = 3, // out contract
5656
}
5757

58-
private extern (D) struct BitFields
58+
/// Bitfield for settable/copyable flags, see `copyFlagsFrom`, `resetAllFlags`
59+
private extern (D) struct FlagBitFields
5960
{
6061
bool ctor; /// constructor type
6162
bool noAccessCheck; /// don't do access checks
@@ -74,6 +75,14 @@ private extern (D) struct BitFields
7475
bool ctfeBlock; /// inside a `if (__ctfe)` block
7576
}
7677

78+
private extern (D) struct NonFlagBitFields
79+
{
80+
ubyte intypeof; /// in typeof(exp)
81+
bool nofree; /// true if shouldn't free it
82+
bool inLoop; /// true if inside a loop (where constructor calls aren't allowed)
83+
bool inDefaultArg; /// true if inside a default argument (where __FILE__, etc are evaluated at the call site)
84+
bool explicitVisibility; /// set if in an explicit visibility attribute
85+
}
7786
/// State of -preview switches
7887
///
7988
/// By making them part of a Scope, we reduce reliance on dmd.globals,
@@ -140,10 +149,6 @@ extern (C++) struct Scope
140149
ForeachStatement fes; /// if nested function for ForeachStatement, this is it
141150
Scope* callsc; /// used for __FUNCTION__, __PRETTY_FUNCTION__ and __MODULE__
142151
Dsymbol inunion; /// != null if processing members of a union
143-
bool nofree; /// true if shouldn't free it
144-
bool inLoop; /// true if inside a loop (where constructor calls aren't allowed)
145-
bool inDefaultArg; /// true if inside a default argument (where __FILE__, etc are evaluated at the call site)
146-
int intypeof; /// in typeof(exp)
147152
VarDeclaration lastVar; /// Previous symbol used to prevent goto-skips-init
148153
ErrorSink eSink; /// sink for error messages
149154

@@ -174,15 +179,15 @@ extern (C++) struct Scope
174179

175180
/// visibility for class members
176181
Visibility visibility = Visibility(Visibility.Kind.public_);
177-
int explicitVisibility; /// set if in an explicit visibility attribute
178182

179183
STC stc; /// storage class
180184

181185
DeprecatedDeclaration depdecl; /// customized deprecation message
182186

183187
import dmd.common.bitfields : generateBitFields;
184-
mixin(generateBitFields!(BitFields, ushort));
185-
188+
mixin(generateBitFields!(FlagBitFields, ushort));
189+
private ushort bitFields2;
190+
mixin(generateBitFields!(NonFlagBitFields, ushort, "bitFields2"));
186191
Previews previews;
187192

188193
// user defined attributes

compiler/src/dmd/dsymbolsem.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ Expression resolveAliasThis(Scope* sc, Expression e, bool gag = false, bool find
351351
* so resolve it ahead.
352352
*/
353353
{
354-
int save = sc.intypeof;
354+
ubyte save = sc.intypeof;
355355
sc.intypeof = 1; // bypass "need this" error check
356356
e = resolveProperties(sc, e);
357357
sc.intypeof = save;

compiler/src/dmd/frontend.h

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7257,10 +7257,6 @@ struct Scope final
72577257
ForeachStatement* fes;
72587258
Scope* callsc;
72597259
Dsymbol* inunion;
7260-
bool nofree;
7261-
bool inLoop;
7262-
bool inDefaultArg;
7263-
int32_t intypeof;
72647260
VarDeclaration* lastVar;
72657261
ErrorSink* eSink;
72667262
Module* minst;
@@ -7272,7 +7268,6 @@ struct Scope final
72727268
CPPMANGLE cppmangle;
72737269
PragmaDeclaration* inlining;
72747270
Visibility visibility;
7275-
int32_t explicitVisibility;
72767271
STC stc;
72777272
DeprecatedDeclaration* depdecl;
72787273
bool ctor() const;
@@ -7303,7 +7298,18 @@ struct Scope final
73037298
bool ctfeBlock(bool v);
73047299
private:
73057300
uint16_t bitFields;
7306-
public:
7301+
uint16_t bitFields2;
7302+
public:
7303+
uint8_t intypeof() const;
7304+
uint8_t intypeof(uint8_t v);
7305+
bool nofree() const;
7306+
bool nofree(bool v);
7307+
bool inLoop() const;
7308+
bool inLoop(bool v);
7309+
bool inDefaultArg() const;
7310+
bool inDefaultArg(bool v);
7311+
bool explicitVisibility() const;
7312+
bool explicitVisibility(bool v);
73077313
Previews previews;
73087314
UserAttributeDeclaration* userAttribDecl;
73097315
DocComment* lastdc;
@@ -7329,10 +7335,6 @@ struct Scope final
73297335
fes(),
73307336
callsc(),
73317337
inunion(),
7332-
nofree(),
7333-
inLoop(),
7334-
inDefaultArg(),
7335-
intypeof(),
73367338
lastVar(),
73377339
eSink(),
73387340
minst(),
@@ -7344,9 +7346,9 @@ struct Scope final
73447346
cppmangle((CPPMANGLE)0u),
73457347
inlining(),
73467348
visibility(Visibility((Visibility::Kind)5u, nullptr)),
7347-
explicitVisibility(),
73487349
depdecl(),
73497350
bitFields(0u),
7351+
bitFields2(),
73507352
previews(),
73517353
userAttribDecl(),
73527354
lastdc(),
@@ -7355,7 +7357,7 @@ struct Scope final
73557357
argStruct()
73567358
{
73577359
}
7358-
Scope(Scope* enclosing, Module* _module = nullptr, ScopeDsymbol* scopesym = nullptr, FuncDeclaration* func = nullptr, VarDeclaration* varDecl = nullptr, Dsymbol* parent = nullptr, LabelStatement* slabel = nullptr, SwitchStatement* switchStatement = nullptr, Statement* tryBody = nullptr, TryFinallyStatement* tryFinally = nullptr, ScopeGuardStatement* scopeGuard = nullptr, Statement* sbreak = nullptr, Statement* scontinue = nullptr, ForeachStatement* fes = nullptr, Scope* callsc = nullptr, Dsymbol* inunion = nullptr, bool nofree = false, bool inLoop = false, bool inDefaultArg = false, int32_t intypeof = 0, VarDeclaration* lastVar = nullptr, ErrorSink* eSink = nullptr, Module* minst = nullptr, TemplateInstance* tinst = nullptr, CtorFlow ctorflow = CtorFlow(), AlignDeclaration* aligndecl = nullptr, CPPNamespaceDeclaration* namespace_ = nullptr, LINK linkage = (LINK)1u, CPPMANGLE cppmangle = (CPPMANGLE)0u, PragmaDeclaration* inlining = nullptr, Visibility visibility = Visibility((Visibility::Kind)5u, nullptr), int32_t explicitVisibility = 0, STC stc = (STC)0LLU, DeprecatedDeclaration* depdecl = nullptr, uint16_t bitFields = 0u, Previews previews = Previews(), UserAttributeDeclaration* userAttribDecl = nullptr, DocComment* lastdc = nullptr, void* anchorCounts = nullptr, Identifier* prevAnchor = nullptr, AliasDeclaration* aliasAsg = nullptr, StructDeclaration* argStruct = nullptr) :
7360+
Scope(Scope* enclosing, Module* _module = nullptr, ScopeDsymbol* scopesym = nullptr, FuncDeclaration* func = nullptr, VarDeclaration* varDecl = nullptr, Dsymbol* parent = nullptr, LabelStatement* slabel = nullptr, SwitchStatement* switchStatement = nullptr, Statement* tryBody = nullptr, TryFinallyStatement* tryFinally = nullptr, ScopeGuardStatement* scopeGuard = nullptr, Statement* sbreak = nullptr, Statement* scontinue = nullptr, ForeachStatement* fes = nullptr, Scope* callsc = nullptr, Dsymbol* inunion = nullptr, VarDeclaration* lastVar = nullptr, ErrorSink* eSink = nullptr, Module* minst = nullptr, TemplateInstance* tinst = nullptr, CtorFlow ctorflow = CtorFlow(), AlignDeclaration* aligndecl = nullptr, CPPNamespaceDeclaration* namespace_ = nullptr, LINK linkage = (LINK)1u, CPPMANGLE cppmangle = (CPPMANGLE)0u, PragmaDeclaration* inlining = nullptr, Visibility visibility = Visibility((Visibility::Kind)5u, nullptr), STC stc = (STC)0LLU, DeprecatedDeclaration* depdecl = nullptr, uint16_t bitFields = 0u, uint16_t bitFields2 = 0u, Previews previews = Previews(), UserAttributeDeclaration* userAttribDecl = nullptr, DocComment* lastdc = nullptr, void* anchorCounts = nullptr, Identifier* prevAnchor = nullptr, AliasDeclaration* aliasAsg = nullptr, StructDeclaration* argStruct = nullptr) :
73597361
enclosing(enclosing),
73607362
_module(_module),
73617363
scopesym(scopesym),
@@ -7372,10 +7374,6 @@ struct Scope final
73727374
fes(fes),
73737375
callsc(callsc),
73747376
inunion(inunion),
7375-
nofree(nofree),
7376-
inLoop(inLoop),
7377-
inDefaultArg(inDefaultArg),
7378-
intypeof(intypeof),
73797377
lastVar(lastVar),
73807378
eSink(eSink),
73817379
minst(minst),
@@ -7387,10 +7385,10 @@ struct Scope final
73877385
cppmangle(cppmangle),
73887386
inlining(inlining),
73897387
visibility(visibility),
7390-
explicitVisibility(explicitVisibility),
73917388
stc(stc),
73927389
depdecl(depdecl),
73937390
bitFields(bitFields),
7391+
bitFields2(bitFields2),
73947392
previews(previews),
73957393
userAttribDecl(userAttribDecl),
73967394
lastdc(lastdc),

compiler/src/dmd/scope.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,6 @@ struct Scope final
6767
ForeachStatement *fes; // if nested function for ForeachStatement, this is it
6868
Scope *callsc; // used for __FUNCTION__, __PRETTY_FUNCTION__ and __MODULE__
6969
Dsymbol *inunion; // !=null if processing members of a union
70-
d_bool nofree; // true if shouldn't free it
71-
d_bool inLoop; // true if inside a loop (where constructor calls aren't allowed)
72-
d_bool inDefaultArg; // true if inside a default argument (where __FILE__, etc are evaluated at the call site)
73-
int intypeof; // in typeof(exp)
7470
VarDeclaration *lastVar; // Previous symbol used to prevent goto-skips-init
7571
ErrorSink *eSink; // sink for error messages
7672

@@ -96,13 +92,12 @@ struct Scope final
9692
PragmaDeclaration *inlining; // inlining strategy for functions
9793

9894
Visibility visibility; // visibility for class members
99-
int explicitVisibility; // set if in an explicit visibility attribute
10095

10196
StorageClass stc; // storage class
10297

10398
DeprecatedDeclaration *depdecl; // customized deprecation message
10499

105-
uint16_t flags;
100+
uint16_t flags, flags2;
106101
uint16_t previews; // state of preview switches
107102

108103
bool ctor() const;

compiler/src/dmd/semantic3.d

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ private extern(C++) final class Semantic3Visitor : Visitor
345345
sc2.linkage = funcdecl.isCsymbol() ? LINK.c : LINK.d;
346346
sc2.stc &= STC.flowThruFunction;
347347
sc2.visibility = Visibility(Visibility.Kind.public_);
348-
sc2.explicitVisibility = 0;
348+
sc2.explicitVisibility = false;
349349
sc2.aligndecl = null;
350350
if (funcdecl.ident != Id.require && funcdecl.ident != Id.ensure)
351351
{

0 commit comments

Comments
 (0)