Mercurial > projects > ddmd
annotate dmd/UnaExp.d @ 187:b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
author | Abscissa |
---|---|
date | Tue, 07 Jun 2011 23:37:34 -0400 |
parents | b64060ab22df |
children |
rev | line source |
---|---|
72 | 1 module dmd.UnaExp; |
2 | |
114 | 3 import dmd.common; |
72 | 4 import dmd.Expression; |
5 import dmd.InterState; | |
6 import dmd.TY; | |
7 import dmd.TypeClass; | |
8 import dmd.TypeStruct; | |
9 import dmd.Dsymbol; | |
10 import dmd.AggregateDeclaration; | |
11 import dmd.Type; | |
12 import dmd.OutBuffer; | |
13 import dmd.Loc; | |
14 import dmd.TOK; | |
15 import dmd.Scope; | |
16 import dmd.InlineCostState; | |
17 import dmd.InlineDoState; | |
18 import dmd.HdrGenState; | |
19 import dmd.InlineScanState; | |
20 import dmd.DotIdExp; | |
21 import dmd.ArrayExp; | |
22 import dmd.CallExp; | |
23 import dmd.PREC; | |
24 import dmd.Token; | |
25 import dmd.expression.Util; | |
26 | |
187
b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents:
182
diff
changeset
|
27 import dmd.DDMDExtensions; |
b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents:
182
diff
changeset
|
28 |
0 | 29 class UnaExp : Expression |
30 { | |
187
b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents:
182
diff
changeset
|
31 mixin insertMemberExtension!(typeof(this)); |
b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents:
182
diff
changeset
|
32 |
0 | 33 Expression e1; |
34 | |
35 this(Loc loc, TOK op, int size, Expression e1) | |
36 { | |
178 | 37 register(); |
72 | 38 super(loc, op, size); |
0 | 39 this.e1 = e1; |
40 } | |
41 | |
72 | 42 override Expression syntaxCopy() |
0 | 43 { |
72 | 44 UnaExp e = cast(UnaExp)copy(); |
45 e.type = null; | |
46 e.e1 = e.e1.syntaxCopy(); | |
47 | |
0 | 48 return e; |
49 } | |
50 | |
72 | 51 override Expression semantic(Scope sc) |
0 | 52 { |
72 | 53 version (LOGSEMANTIC) { |
54 writef("UnaExp.semantic('%s')\n", toChars()); | |
55 } | |
56 e1 = e1.semantic(sc); | |
57 // if (!e1.type) | |
58 // error("%s has no value", e1.toChars()); | |
0 | 59 return this; |
60 } | |
61 | |
72 | 62 override void toCBuffer(OutBuffer buf, HdrGenState* hgs) |
0 | 63 { |
72 | 64 buf.writestring(Token.toChars(op)); |
0 | 65 expToCBuffer(buf, hgs, e1, precedence[op]); |
66 } | |
67 | |
72 | 68 override Expression optimize(int result) |
0 | 69 { |
72 | 70 e1 = e1.optimize(result); |
0 | 71 return this; |
72 } | |
73 | |
72 | 74 override void dump(int indent) |
0 | 75 { |
76 assert(false); | |
77 } | |
78 | |
72 | 79 override void scanForNestedRef(Scope sc) |
0 | 80 { |
64 | 81 e1.scanForNestedRef(sc); |
0 | 82 } |
83 | |
182 | 84 Expression interpretCommon(InterState istate, Expression* function(Type* a0, Expression* a1)) |
0 | 85 { |
86 assert(false); | |
87 } | |
88 | |
72 | 89 override bool canThrow() |
0 | 90 { |
91 return e1.canThrow(); | |
92 } | |
93 | |
154
14feb7ae01a6
* changed the build system to build a release version if the debug one compiles
trass3r
parents:
130
diff
changeset
|
94 override Expression resolveLoc(Loc loc, Scope sc) |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
95 { |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
96 e1 = e1.resolveLoc(loc, sc); |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
97 return this; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
98 } |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
99 |
72 | 100 override int inlineCost(InlineCostState* ics) |
0 | 101 { |
102 return 1 + e1.inlineCost(ics); | |
103 } | |
104 | |
72 | 105 override Expression doInline(InlineDoState ids) |
0 | 106 { |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
107 auto ue = cast(UnaExp)copy(); |
72 | 108 |
109 ue.e1 = e1.doInline(ids); | |
0 | 110 return ue; |
111 } | |
112 | |
72 | 113 override Expression inlineScan(InlineScanState* iss) |
0 | 114 { |
72 | 115 e1 = e1.inlineScan(iss); |
0 | 116 return this; |
72 | 117 } |
118 | |
119 /************************************ | |
120 * Operator overload. | |
121 * Check for operator overload, if so, replace | |
122 * with function call. | |
123 * Return null if not an operator overload. | |
0 | 124 */ |
125 Expression op_overload(Scope sc) | |
126 { | |
72 | 127 //printf("UnaExp.op_overload() (%s)\n", toChars()); |
128 AggregateDeclaration ad; | |
129 Dsymbol fd; | |
130 Type t1 = e1.type.toBasetype(); | |
131 | |
132 if (t1.ty == TY.Tclass) | |
133 { | |
134 ad = (cast(TypeClass)t1).sym; | |
135 goto L1; | |
136 } | |
137 else if (t1.ty == TY.Tstruct) | |
138 { | |
139 ad = (cast(TypeStruct)t1).sym; | |
140 | |
141 L1: | |
142 fd = search_function(ad, opId()); | |
143 if (fd) | |
144 { | |
145 if (op == TOK.TOKarray) | |
146 { | |
147 /* Rewrite op e1[arguments] as: | |
148 * e1.fd(arguments) | |
149 */ | |
150 Expression e = new DotIdExp(loc, e1, fd.ident); | |
151 ArrayExp ae = cast(ArrayExp)this; | |
152 e = new CallExp(loc, e, ae.arguments); | |
153 e = e.semantic(sc); | |
154 return e; | |
155 } | |
156 else | |
157 { | |
158 // Rewrite +e1 as e1.add() | |
159 return build_overload(loc, sc, e1, null, fd.ident); | |
160 } | |
161 } | |
162 | |
163 version (DMDV2) { | |
164 // Didn't find it. Forward to aliasthis | |
165 if (ad.aliasthis) | |
166 { | |
167 /* Rewrite op(e1) as: | |
168 * op(e1.aliasthis) | |
169 */ | |
170 Expression e1 = new DotIdExp(loc, this.e1, ad.aliasthis.ident); | |
171 Expression e = copy(); | |
172 (cast(UnaExp)e).e1 = e1; | |
173 e = e.semantic(sc); | |
174 return e; | |
175 } | |
176 } | |
177 } | |
0 | 178 return null; |
179 } | |
180 } | |
181 |