Skip to content

Commit 4138427

Browse files
move Type.isAssignable to typesem.d (#21602)
1 parent 6340fc7 commit 4138427

5 files changed

Lines changed: 55 additions & 64 deletions

File tree

compiler/src/dmd/arrayop.d

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import dmd.location;
3131
import dmd.mtype;
3232
import dmd.common.outbuffer;
3333
import dmd.tokens;
34+
import dmd.typesem : isAssignable;
3435
import dmd.visitor;
3536

3637
/**********************************************

compiler/src/dmd/frontend.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2126,7 +2126,6 @@ class Type : public ASTNode
21262126
virtual bool isUnsigned();
21272127
virtual bool isScopeClass();
21282128
virtual bool isString();
2129-
virtual bool isAssignable();
21302129
virtual bool isBoolean();
21312130
bool isConst() const;
21322131
bool isImmutable() const;
@@ -4588,7 +4587,6 @@ class TypeEnum final : public Type
45884587
bool isUnsigned() override;
45894588
bool isBoolean() override;
45904589
bool isString() override;
4591-
bool isAssignable() override;
45924590
bool needsDestruction() override;
45934591
bool needsCopyOrPostblit() override;
45944592
bool needsNested() override;
@@ -4857,7 +4855,6 @@ class TypeStruct final : public Type
48574855
TypeStruct* syntaxCopy() override;
48584856
structalign_t alignment() override;
48594857
Expression* defaultInitLiteral(Loc loc) override;
4860-
bool isAssignable() override;
48614858
bool isBoolean() override;
48624859
bool needsDestruction() override;
48634860
bool needsCopyOrPostblit() override;

compiler/src/dmd/mtype.d

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import dmd.dtemplate;
3030
import dmd.enumsem;
3131
import dmd.errors;
3232
import dmd.expression;
33-
import dmd.dsymbolsem : determineSize;
3433
import dmd.hdrgen;
3534
import dmd.id;
3635
import dmd.identifier;
@@ -687,19 +686,6 @@ extern (C++) abstract class Type : ASTNode
687686
return false;
688687
}
689688

690-
/**************************
691-
* When T is mutable,
692-
* Given:
693-
* T a, b;
694-
* Can we bitwise assign:
695-
* a = b;
696-
* ?
697-
*/
698-
bool isAssignable()
699-
{
700-
return true;
701-
}
702-
703689
/**************************
704690
* Returns true if T can be converted to boolean value.
705691
*/
@@ -3061,45 +3047,6 @@ extern (C++) final class TypeStruct : Type
30613047
return structinit;
30623048
}
30633049

3064-
override bool isAssignable()
3065-
{
3066-
bool assignable = true;
3067-
uint offset = ~0; // dead-store initialize to prevent spurious warning
3068-
3069-
sym.determineSize(sym.loc);
3070-
3071-
/* If any of the fields are const or immutable,
3072-
* then one cannot assign this struct.
3073-
*/
3074-
for (size_t i = 0; i < sym.fields.length; i++)
3075-
{
3076-
VarDeclaration v = sym.fields[i];
3077-
//printf("%s [%d] v = (%s) %s, v.offset = %d, v.parent = %s\n", sym.toChars(), i, v.kind(), v.toChars(), v.offset, v.parent.kind());
3078-
if (i == 0)
3079-
{
3080-
}
3081-
else if (v.offset == offset)
3082-
{
3083-
/* If any fields of anonymous union are assignable,
3084-
* then regard union as assignable.
3085-
* This is to support unsafe things like Rebindable templates.
3086-
*/
3087-
if (assignable)
3088-
continue;
3089-
}
3090-
else
3091-
{
3092-
if (!assignable)
3093-
return false;
3094-
}
3095-
assignable = v.type.isMutable() && v.type.isAssignable();
3096-
offset = v.offset;
3097-
//printf(" -> assignable = %d\n", assignable);
3098-
}
3099-
3100-
return assignable;
3101-
}
3102-
31033050
override bool isBoolean()
31043051
{
31053052
return false;
@@ -3261,11 +3208,6 @@ extern (C++) final class TypeEnum : Type
32613208
return memType().isString();
32623209
}
32633210

3264-
override bool isAssignable()
3265-
{
3266-
return memType().isAssignable();
3267-
}
3268-
32693211
override bool needsDestruction()
32703212
{
32713213
return memType().needsDestruction();

compiler/src/dmd/mtype.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,6 @@ class Type : public ASTNode
238238
virtual bool isUnsigned();
239239
virtual bool isScopeClass();
240240
virtual bool isString();
241-
virtual bool isAssignable();
242241
virtual bool isBoolean();
243242
bool isConst() const { return (mod & MODconst) != 0; }
244243
bool isImmutable() const { return (mod & MODimmutable) != 0; }
@@ -692,7 +691,6 @@ class TypeStruct final : public Type
692691
TypeStruct *syntaxCopy() override;
693692
structalign_t alignment() override;
694693
Expression *defaultInitLiteral(Loc loc) override;
695-
bool isAssignable() override;
696694
bool isBoolean() override;
697695
bool needsDestruction() override;
698696
bool needsCopyOrPostblit() override;
@@ -723,7 +721,6 @@ class TypeEnum final : public Type
723721
bool isUnsigned() override;
724722
bool isBoolean() override;
725723
bool isString() override;
726-
bool isAssignable() override;
727724
bool needsDestruction() override;
728725
bool needsCopyOrPostblit() override;
729726
bool needsNested() override;

compiler/src/dmd/typesem.d

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,60 @@ bool isCopyable(Type t)
476476
return true;
477477
}
478478

479+
/**************************
480+
* When T is mutable,
481+
* Given:
482+
* T a, b;
483+
* Can we bitwise assign:
484+
* a = b;
485+
* ?
486+
*/
487+
bool isAssignable(Type t)
488+
{
489+
if (auto te = t.isTypeEnum())
490+
t = te.memType();
491+
TypeStruct ts = t.isTypeStruct();
492+
if (!ts)
493+
return true;
494+
495+
bool assignable = true;
496+
uint offset = ~0; // dead-store initialize to prevent spurious warning
497+
498+
auto sym = ts.sym;
499+
sym.determineSize(sym.loc);
500+
501+
/* If any of the fields are const or immutable,
502+
* then one cannot assign this struct.
503+
*/
504+
for (size_t i = 0; i < sym.fields.length; i++)
505+
{
506+
VarDeclaration v = sym.fields[i];
507+
//printf("%s [%d] v = (%s) %s, v.offset = %d, v.parent = %s\n", sym.toChars(), i, v.kind(), v.toChars(), v.offset, v.parent.kind());
508+
if (i == 0)
509+
{
510+
}
511+
else if (v.offset == offset)
512+
{
513+
/* If any fields of anonymous union are assignable,
514+
* then regard union as assignable.
515+
* This is to support unsafe things like Rebindable templates.
516+
*/
517+
if (assignable)
518+
continue;
519+
}
520+
else
521+
{
522+
if (!assignable)
523+
return false;
524+
}
525+
assignable = v.type.isMutable() && v.type.isAssignable();
526+
offset = v.offset;
527+
//printf(" -> assignable = %d\n", assignable);
528+
}
529+
530+
return assignable;
531+
}
532+
479533
/************************************
480534
* Determine mutability of indirections in (ref) t.
481535
*

0 commit comments

Comments
 (0)