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