Skip to content

Commit cdb9516

Browse files
[expressionsem.d] restirct some typesem imports (#21593)
1 parent 6b64cfb commit cdb9516

4 files changed

Lines changed: 68 additions & 66 deletions

File tree

compiler/src/dmd/constfold.d

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import dmd.declaration;
2525
import dmd.dstruct;
2626
import dmd.errors;
2727
import dmd.expression;
28+
import dmd.expressionsem : getField;
2829
import dmd.globals;
2930
import dmd.location;
3031
import dmd.mtype;

compiler/src/dmd/expression.d

Lines changed: 1 addition & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ import dmd.root.string;
4949
import dmd.root.utf;
5050
import dmd.target;
5151
import dmd.tokens;
52-
import dmd.typesem : pointerTo, toHeadMutable, castMod, size, mutableOf, unSharedOf;
52+
import dmd.typesem : toHeadMutable, size, mutableOf, unSharedOf;
5353
import dmd.visitor;
5454

5555
enum LOGSEMANTIC = false;
@@ -527,20 +527,6 @@ extern (C++) abstract class Expression : ASTNode
527527
return false;
528528
}
529529

530-
/******************************
531-
* Take address of expression.
532-
*/
533-
final Expression addressOf()
534-
{
535-
//printf("Expression::addressOf()\n");
536-
debug
537-
{
538-
assert(op == EXP.error || isLvalue());
539-
}
540-
Expression e = new AddrExp(loc, this, type.pointerTo());
541-
return e;
542-
}
543-
544530
/******************************
545531
* If this is a reference, dereference it.
546532
*/
@@ -2218,56 +2204,6 @@ extern (C++) final class StructLiteralExp : Expression
22182204
return exp;
22192205
}
22202206

2221-
/**************************************
2222-
* Gets expression at offset of type.
2223-
* Returns NULL if not found.
2224-
*/
2225-
extern (D) Expression getField(Type type, uint offset)
2226-
{
2227-
//printf("StructLiteralExp::getField(this = %s, type = %s, offset = %u)\n",
2228-
// /*toChars()*/"", type.toChars(), offset);
2229-
Expression e = null;
2230-
int i = getFieldIndex(type, offset);
2231-
2232-
if (i != -1)
2233-
{
2234-
//printf("\ti = %d\n", i);
2235-
if (i >= sd.nonHiddenFields())
2236-
return null;
2237-
2238-
assert(i < elements.length);
2239-
e = (*elements)[i];
2240-
if (e)
2241-
{
2242-
//printf("e = %s, e.type = %s\n", e.toChars(), e.type.toChars());
2243-
2244-
/* If type is a static array, and e is an initializer for that array,
2245-
* then the field initializer should be an array literal of e.
2246-
*/
2247-
auto tsa = type.isTypeSArray();
2248-
if (tsa && e.type.castMod(0) != type.castMod(0))
2249-
{
2250-
const length = cast(size_t)tsa.dim.toInteger();
2251-
auto z = new Expressions(length);
2252-
foreach (ref q; *z)
2253-
q = e.copy();
2254-
e = new ArrayLiteralExp(loc, type, z);
2255-
}
2256-
else
2257-
{
2258-
e = e.copy();
2259-
e.type = type;
2260-
}
2261-
if (useStaticInit && e.type.needsNested())
2262-
if (auto se = e.isStructLiteralExp())
2263-
{
2264-
se.useStaticInit = true;
2265-
}
2266-
}
2267-
}
2268-
return e;
2269-
}
2270-
22712207
/************************************
22722208
* Get index of field.
22732209
* Returns -1 if not found.

compiler/src/dmd/expressionsem.d

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,72 @@ TupleDeclaration isAliasThisTuple(Expression e)
664664
}
665665
}
666666

667+
/******************************
668+
* Take address of expression.
669+
*/
670+
Expression addressOf(Expression e)
671+
{
672+
//printf("Expression::addressOf()\n");
673+
debug
674+
{
675+
assert(e.op == EXP.error || e.isLvalue());
676+
}
677+
return new AddrExp(e.loc, e, e.type.pointerTo());
678+
}
679+
680+
/**************************************
681+
* Gets expression at offset of type.
682+
* Returns NULL if not found.
683+
*/
684+
Expression getField(StructLiteralExp sle, Type type, uint offset)
685+
{
686+
//printf("StructLiteralExp::getField(this = %s, type = %s, offset = %u)\n",
687+
// /*toChars()*/"", type.toChars(), offset);
688+
Expression e2 = null;
689+
const int i = sle.getFieldIndex(type, offset);
690+
691+
if (i == -1)
692+
return null;
693+
694+
//printf("\ti = %d\n", i);
695+
if (i >= sle.sd.nonHiddenFields())
696+
return null;
697+
698+
assert(i < sle.elements.length);
699+
e2 = (*sle.elements)[i];
700+
if (!e2)
701+
return null;
702+
703+
//printf("e = %s, e.type = %s\n", e.toChars(), e.type.toChars());
704+
705+
/* If type is a static array, and e is an initializer for that array,
706+
* then the field initializer should be an array literal of e.
707+
*/
708+
auto tsa = type.isTypeSArray();
709+
if (tsa && e2.type.castMod(0) != type.castMod(0))
710+
{
711+
const length = cast(size_t)tsa.dim.toInteger();
712+
auto z = new Expressions(length);
713+
foreach (ref q; *z)
714+
q = e2.copy();
715+
e2 = new ArrayLiteralExp(sle.loc, type, z);
716+
}
717+
else
718+
{
719+
e2 = e2.copy();
720+
e2.type = type;
721+
}
722+
if (sle.useStaticInit && e2.type.needsNested())
723+
if (auto se = e2.isStructLiteralExp())
724+
{
725+
se.useStaticInit = true;
726+
}
727+
728+
return e2;
729+
}
730+
731+
732+
667733
/**************************************
668734
* Runs semantic on ae.arguments. Declares temporary variables
669735
* if '$' was used.

compiler/src/dmd/frontend.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2426,7 +2426,6 @@ class Expression : public ASTNode
24262426
virtual StringExp* toStringExp();
24272427
virtual bool isLvalue();
24282428
virtual bool checkType();
2429-
Expression* addressOf();
24302429
Expression* deref();
24312430
int32_t isConst();
24322431
virtual bool isIdentical(const Expression* const e) const;

0 commit comments

Comments
 (0)