Mercurial > projects > ddmd
view dmd/ArrayLengthExp.d @ 130:60bb0fe4563e
dmdfe 2.037 first main iteration
author | Eldar Insafutdinov <e.insafutdinov@gmail.com> |
---|---|
date | Thu, 09 Sep 2010 22:51:44 +0100 |
parents | e28b18c23469 |
children | 438eaa11eed4 |
line wrap: on
line source
module dmd.ArrayLengthExp; import dmd.common; import dmd.Expression; import dmd.BinExp; import dmd.backend.elem; import dmd.UnaExp; import dmd.InterState; import dmd.OutBuffer; import dmd.Loc; import dmd.Scope; import dmd.IRState; import dmd.HdrGenState; import dmd.TOK; import dmd.Type; import dmd.WANT; import dmd.VarExp; import dmd.VarDeclaration; import dmd.PtrExp; import dmd.Lexer; import dmd.Identifier; import dmd.ExpInitializer; import dmd.DeclarationExp; import dmd.CommaExp; import dmd.AssignExp; import dmd.AddrExp; import dmd.expression.ArrayLength; import dmd.backend.Util; import dmd.backend.OPER; class ArrayLengthExp : UnaExp { this(Loc loc, Expression e1) { super(loc, TOK.TOKarraylength, ArrayLengthExp.sizeof, e1); } override Expression semantic(Scope sc) { version (LOGSEMANTIC) { printf("ArrayLengthExp::semantic('%s')\n", toChars()); } if (!type) { UnaExp.semantic(sc); e1 = resolveProperties(sc, e1); type = Type.tsize_t; } return this; } static Expression rewriteOpAssign(BinExp exp) { Expression e; assert(exp.e1.op == TOKarraylength); auto ale = cast(ArrayLengthExp)exp.e1; if (ale.e1.op == TOK.TOKvar) { e = opAssignToOp(exp.loc, exp.op, ale, exp.e2); e = new AssignExp(exp.loc, ale.syntaxCopy(), e); } else { /* auto tmp = &array; * (*tmp).length = (*tmp).length op e2 */ Identifier id = Lexer.uniqueId("__arraylength"); ExpInitializer ei = new ExpInitializer(ale.loc, new AddrExp(ale.loc, ale.e1)); VarDeclaration tmp = new VarDeclaration(ale.loc, ale.e1.type.pointerTo(), id, ei); Expression e1 = new ArrayLengthExp(ale.loc, new PtrExp(ale.loc, new VarExp(ale.loc, tmp))); Expression elvalue = e1.syntaxCopy(); e = opAssignToOp(exp.loc, exp.op, e1, exp.e2); e = new AssignExp(exp.loc, elvalue, e); e = new CommaExp(exp.loc, new DeclarationExp(ale.loc, tmp), e); } return e; } override Expression optimize(int result) { //printf("ArrayLengthExp::optimize(result = %d) %s\n", result, toChars()); e1 = e1.optimize(WANTvalue | (result & WANTinterpret)); Expression e = this; if (e1.op == TOKstring || e1.op == TOKarrayliteral || e1.op == TOKassocarrayliteral) { e = ArrayLength(type, e1); } return e; } override Expression interpret(InterState istate) { assert(false); } override void toCBuffer(OutBuffer buf, HdrGenState* hgs) { assert(false); } override elem* toElem(IRState* irs) { elem *e = e1.toElem(irs); e = el_una(OP64_32, type.totym(), e); el_setLoc(e,loc); return e; } }