Mercurial > projects > ddmd
annotate dmd/ArrayLengthExp.d @ 162:438eaa11eed4
updated build script to use dmd2.039
some missing methods implemented
author | korDen |
---|---|
date | Tue, 21 Sep 2010 14:59:56 +0400 |
parents | 60bb0fe4563e |
children | d237b38b5858 |
rev | line source |
---|---|
72 | 1 module dmd.ArrayLengthExp; |
2 | |
114 | 3 import dmd.common; |
72 | 4 import dmd.Expression; |
162 | 5 import dmd.GlobalExpressions; |
6 import dmd.IntegerExp; | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
7 import dmd.BinExp; |
72 | 8 import dmd.backend.elem; |
9 import dmd.UnaExp; | |
10 import dmd.InterState; | |
11 import dmd.OutBuffer; | |
12 import dmd.Loc; | |
13 import dmd.Scope; | |
14 import dmd.IRState; | |
15 import dmd.HdrGenState; | |
0 | 16 import dmd.TOK; |
17 import dmd.Type; | |
18 import dmd.WANT; | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
19 import dmd.VarExp; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
20 import dmd.VarDeclaration; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
21 import dmd.PtrExp; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
22 import dmd.Lexer; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
23 import dmd.Identifier; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
24 import dmd.ExpInitializer; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
25 import dmd.DeclarationExp; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
26 import dmd.CommaExp; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
27 import dmd.AssignExp; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
28 import dmd.AddrExp; |
0 | 29 |
30 import dmd.expression.ArrayLength; | |
31 | |
32 import dmd.backend.Util; | |
72 | 33 import dmd.backend.OPER; |
34 | |
0 | 35 class ArrayLengthExp : UnaExp |
36 { | |
37 this(Loc loc, Expression e1) | |
38 { | |
39 super(loc, TOK.TOKarraylength, ArrayLengthExp.sizeof, e1); | |
40 } | |
41 | |
72 | 42 override Expression semantic(Scope sc) |
0 | 43 { |
44 version (LOGSEMANTIC) { | |
45 printf("ArrayLengthExp::semantic('%s')\n", toChars()); | |
46 } | |
47 if (!type) | |
48 { | |
49 UnaExp.semantic(sc); | |
50 e1 = resolveProperties(sc, e1); | |
51 | |
52 type = Type.tsize_t; | |
53 } | |
54 return this; | |
55 } | |
56 | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
57 static Expression rewriteOpAssign(BinExp exp) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
58 { |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
59 Expression e; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
60 |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
61 assert(exp.e1.op == TOKarraylength); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
62 auto ale = cast(ArrayLengthExp)exp.e1; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
63 if (ale.e1.op == TOK.TOKvar) |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
64 { |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
65 e = opAssignToOp(exp.loc, exp.op, ale, exp.e2); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
66 e = new AssignExp(exp.loc, ale.syntaxCopy(), e); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
67 } |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
68 else |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
69 { |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
70 /* auto tmp = &array; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
71 * (*tmp).length = (*tmp).length op e2 |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
72 */ |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
73 Identifier id = Lexer.uniqueId("__arraylength"); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
74 ExpInitializer ei = new ExpInitializer(ale.loc, new AddrExp(ale.loc, ale.e1)); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
75 VarDeclaration tmp = new VarDeclaration(ale.loc, ale.e1.type.pointerTo(), id, ei); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
76 |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
77 Expression e1 = new ArrayLengthExp(ale.loc, new PtrExp(ale.loc, new VarExp(ale.loc, tmp))); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
78 Expression elvalue = e1.syntaxCopy(); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
79 e = opAssignToOp(exp.loc, exp.op, e1, exp.e2); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
80 e = new AssignExp(exp.loc, elvalue, e); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
81 e = new CommaExp(exp.loc, new DeclarationExp(ale.loc, tmp), e); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
82 } |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
83 return e; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
84 } |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
85 |
72 | 86 override Expression optimize(int result) |
0 | 87 { |
88 //printf("ArrayLengthExp::optimize(result = %d) %s\n", result, toChars()); | |
89 e1 = e1.optimize(WANTvalue | (result & WANTinterpret)); | |
90 Expression e = this; | |
91 if (e1.op == TOKstring || e1.op == TOKarrayliteral || e1.op == TOKassocarrayliteral) | |
92 { | |
93 e = ArrayLength(type, e1); | |
94 } | |
95 return e; | |
96 } | |
97 | |
72 | 98 override Expression interpret(InterState istate) |
0 | 99 { |
162 | 100 Expression e; |
101 Expression e1; | |
102 | |
103 version (LOG) { | |
104 printf("ArrayLengthExp.interpret() %s\n", toChars()); | |
105 } | |
106 e1 = this.e1.interpret(istate); | |
107 if (e1 is EXP_CANT_INTERPRET) | |
108 goto Lcant; | |
109 if (e1.op == TOKstring || e1.op == TOKarrayliteral || e1.op == TOKassocarrayliteral) | |
110 { | |
111 e = ArrayLength(type, e1); | |
112 } | |
113 else if (e1.op == TOKnull) | |
114 { | |
115 e = new IntegerExp(loc, 0, type); | |
116 } | |
117 else | |
118 goto Lcant; | |
119 return e; | |
120 | |
121 Lcant: | |
122 return EXP_CANT_INTERPRET; | |
0 | 123 } |
124 | |
72 | 125 override void toCBuffer(OutBuffer buf, HdrGenState* hgs) |
0 | 126 { |
127 assert(false); | |
128 } | |
129 | |
72 | 130 override elem* toElem(IRState* irs) |
0 | 131 { |
132 elem *e = e1.toElem(irs); | |
133 e = el_una(OP64_32, type.totym(), e); | |
134 el_setLoc(e,loc); | |
135 return e; | |
136 } | |
137 } | |
138 |