annotate dmd/UnaExp.d @ 64:4290d870944a

More fixes
author korDen
date Mon, 23 Aug 2010 20:29:15 +0400
parents cab4c37afb89
children 2e2a5c3f943a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1 module dmd.UnaExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
2
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
3 import dmd.Expression;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
4 import dmd.InterState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
5 import dmd.TY;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
6 import dmd.TypeClass;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
7 import dmd.TypeStruct;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
8 import dmd.Dsymbol;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
9 import dmd.AggregateDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
10 import dmd.Type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
11 import dmd.OutBuffer;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
12 import dmd.Loc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
13 import dmd.TOK;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
14 import dmd.Scope;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
15 import dmd.InlineCostState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
16 import dmd.InlineDoState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
17 import dmd.HdrGenState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
18 import dmd.InlineScanState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
19 import dmd.DotIdExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
20 import dmd.ArrayExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
21 import dmd.CallExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
22 import dmd.PREC;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
23 import dmd.Token;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
24 import dmd.expression.Util;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
25
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
26 class UnaExp : Expression
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
27 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
28 Expression e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
29
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
30 this(Loc loc, TOK op, int size, Expression e1)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
31 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
32 super(loc, op, size);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
33 this.e1 = e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
34 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
35
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
36 Expression syntaxCopy()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
37 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
38 UnaExp e = cast(UnaExp)copy();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
39 e.type = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
40 e.e1 = e.e1.syntaxCopy();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
41
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
42 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
43 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
44
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
45 Expression semantic(Scope sc)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
46 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
47 version (LOGSEMANTIC) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
48 writef("UnaExp.semantic('%s')\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
49 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
50 e1 = e1.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
51 // if (!e1.type)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
52 // error("%s has no value", e1.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
53 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
54 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
55
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
56 void toCBuffer(OutBuffer buf, HdrGenState* hgs)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
57 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
58 buf.writestring(Token.toChars(op));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
59 expToCBuffer(buf, hgs, e1, precedence[op]);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
60 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
61
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
62 Expression optimize(int result)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
63 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
64 e1 = e1.optimize(result);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
65 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
66 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
67
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
68 void dump(int indent)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
69 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
70 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
71 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
72
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
73 void scanForNestedRef(Scope sc)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
74 {
64
4290d870944a More fixes
korDen
parents: 63
diff changeset
75 e1.scanForNestedRef(sc);
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
76 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
77
63
cab4c37afb89 A bunch of implementations
korDen
parents: 0
diff changeset
78 Expression interpretCommon(InterState istate, Expression *(*fp)(Type* a0, Expression* a1))
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
79 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
80 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
81 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
82
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
83 bool canThrow()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
84 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
85 return e1.canThrow();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
86 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
87
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
88 int inlineCost(InlineCostState* ics)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
89 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
90 return 1 + e1.inlineCost(ics);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
91 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
92
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
93 Expression doInline(InlineDoState ids)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
94 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
95 UnaExp ue = cast(UnaExp)copy();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
96
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
97 ue.e1 = e1.doInline(ids);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
98 return ue;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
99 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
100
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
101 Expression inlineScan(InlineScanState* iss)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
102 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
103 e1 = e1.inlineScan(iss);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
104 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
105 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
106
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
107 /************************************
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
108 * Operator overload.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
109 * Check for operator overload, if so, replace
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
110 * with function call.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
111 * Return null if not an operator overload.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
112 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
113 Expression op_overload(Scope sc)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
114 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
115 //printf("UnaExp.op_overload() (%s)\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
116 AggregateDeclaration ad;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
117 Dsymbol fd;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
118 Type t1 = e1.type.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
119
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
120 if (t1.ty == TY.Tclass)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
121 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
122 ad = (cast(TypeClass)t1).sym;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
123 goto L1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
124 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
125 else if (t1.ty == TY.Tstruct)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
126 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
127 ad = (cast(TypeStruct)t1).sym;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
128
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
129 L1:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
130 fd = search_function(ad, opId());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
131 if (fd)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
132 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
133 if (op == TOK.TOKarray)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
134 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
135 /* Rewrite op e1[arguments] as:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
136 * e1.fd(arguments)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
137 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
138 Expression e = new DotIdExp(loc, e1, fd.ident);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
139 ArrayExp ae = cast(ArrayExp)this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
140 e = new CallExp(loc, e, ae.arguments);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
141 e = e.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
142 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
143 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
144 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
145 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
146 // Rewrite +e1 as e1.add()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
147 return build_overload(loc, sc, e1, null, fd.ident);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
148 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
149 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
150
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
151 version (DMDV2) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
152 // Didn't find it. Forward to aliasthis
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
153 if (ad.aliasthis)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
154 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
155 /* Rewrite op(e1) as:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
156 * op(e1.aliasthis)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
157 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
158 Expression e1 = new DotIdExp(loc, this.e1, ad.aliasthis.ident);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
159 Expression e = copy();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
160 (cast(UnaExp)e).e1 = e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
161 e = e.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
162 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
163 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
164 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
165 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
166 return null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
167 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
168 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
169