72
|
1 module dmd.ThisExp;
|
|
2
|
114
|
3 import dmd.common;
|
72
|
4 import dmd.Expression;
|
0
|
5 import dmd.Declaration;
|
|
6 import dmd.StructDeclaration;
|
|
7 import dmd.ClassDeclaration;
|
|
8 import dmd.Dsymbol;
|
72
|
9 import dmd.FuncDeclaration;
|
0
|
10 import dmd.backend.elem;
|
72
|
11 import dmd.CSX;
|
|
12 import dmd.InterState;
|
|
13 import dmd.OutBuffer;
|
|
14 import dmd.Loc;
|
0
|
15 import dmd.Scope;
|
72
|
16 import dmd.Type;
|
|
17 import dmd.TOK;
|
|
18 import dmd.InlineCostState;
|
|
19 import dmd.InlineDoState;
|
|
20 import dmd.IRState;
|
0
|
21 import dmd.HdrGenState;
|
|
22 import dmd.VarExp;
|
|
23 import dmd.TY;
|
|
24
|
|
25 import dmd.codegen.Util;
|
|
26 import dmd.backend.TYM;
|
|
27 import dmd.backend.Util;
|
|
28 import dmd.backend.OPER;
|
72
|
29
|
0
|
30 class ThisExp : Expression
|
|
31 {
|
|
32 Declaration var;
|
|
33
|
|
34 this(Loc loc)
|
|
35 {
|
|
36 super(loc, TOK.TOKthis, ThisExp.sizeof);
|
|
37 //printf("ThisExp::ThisExp() loc = %d\n", loc.linnum);
|
|
38 }
|
|
39
|
72
|
40 override Expression semantic(Scope sc)
|
0
|
41 {
|
|
42 FuncDeclaration fd;
|
|
43 FuncDeclaration fdthis;
|
|
44
|
|
45 version (LOGSEMANTIC) {
|
|
46 printf("ThisExp::semantic()\n");
|
|
47 }
|
|
48 if (type)
|
|
49 {
|
|
50 //assert(global.errors || var);
|
|
51 return this;
|
|
52 }
|
|
53
|
|
54 /* Special case for typeof(this) and typeof(super) since both
|
|
55 * should work even if they are not inside a non-static member function
|
|
56 */
|
|
57 if (sc.intypeof)
|
|
58 {
|
|
59 // Find enclosing struct or class
|
|
60 for (Dsymbol s = sc.parent; 1; s = s.parent)
|
|
61 {
|
|
62 if (!s)
|
|
63 {
|
|
64 error("%s is not in a class or struct scope", toChars());
|
|
65 goto Lerr;
|
|
66 }
|
|
67 ClassDeclaration cd = s.isClassDeclaration();
|
|
68 if (cd)
|
|
69 {
|
|
70 type = cd.type;
|
|
71 return this;
|
|
72 }
|
|
73 StructDeclaration sd = s.isStructDeclaration();
|
|
74 if (sd)
|
|
75 {
|
|
76 version (STRUCTTHISREF) {
|
|
77 type = sd.type;
|
|
78 } else {
|
|
79 type = sd.type.pointerTo();
|
|
80 }
|
|
81 return this;
|
|
82 }
|
|
83 }
|
|
84 }
|
|
85
|
|
86 fdthis = sc.parent.isFuncDeclaration();
|
|
87 fd = hasThis(sc); // fd is the uplevel function with the 'this' variable
|
|
88 if (!fd)
|
|
89 goto Lerr;
|
|
90
|
|
91 assert(fd.vthis);
|
|
92 var = fd.vthis;
|
|
93 assert(var.parent);
|
|
94 type = var.type;
|
|
95 var.isVarDeclaration().checkNestedReference(sc, loc);
|
|
96 if (!sc.intypeof)
|
|
97 sc.callSuper |= CSXthis;
|
|
98 return this;
|
|
99
|
|
100 Lerr:
|
|
101 error("'this' is only defined in non-static member functions, not %s", sc.parent.toChars());
|
|
102 type = Type.terror;
|
|
103 return this;
|
|
104 }
|
|
105
|
72
|
106 override Expression interpret(InterState istate)
|
0
|
107 {
|
|
108 assert(false);
|
|
109 }
|
|
110
|
72
|
111 override bool isBool(bool result)
|
0
|
112 {
|
|
113 return result ? true : false;
|
|
114 }
|
|
115
|
72
|
116 override void toCBuffer(OutBuffer buf, HdrGenState* hgs)
|
0
|
117 {
|
|
118 buf.writestring("this");
|
|
119 }
|
|
120
|
|
121 version (DMDV2) {
|
72
|
122 override int isLvalue()
|
0
|
123 {
|
|
124 return 1;
|
|
125 }
|
|
126 }
|
72
|
127 override Expression toLvalue(Scope sc, Expression e)
|
0
|
128 {
|
|
129 return this;
|
|
130 }
|
|
131
|
72
|
132 override void scanForNestedRef(Scope sc)
|
0
|
133 {
|
123
|
134 assert(var);
|
|
135 var.isVarDeclaration().checkNestedReference(sc, Loc(0));
|
0
|
136 }
|
|
137
|
72
|
138 override int inlineCost(InlineCostState* ics)
|
0
|
139 {
|
|
140 FuncDeclaration fd = ics.fd;
|
|
141 if (!ics.hdrscan)
|
|
142 if (fd.isNested() || !ics.hasthis)
|
|
143 return COST_MAX;
|
|
144
|
|
145 return 1;
|
|
146 }
|
|
147
|
72
|
148 override Expression doInline(InlineDoState ids)
|
0
|
149 {
|
|
150 //if (!ids.vthis)
|
|
151 //error("no 'this' when inlining %s", ids.parent.toChars());
|
|
152 if (!ids.vthis)
|
|
153 {
|
|
154 return this;
|
|
155 }
|
|
156
|
|
157 VarExp ve = new VarExp(loc, ids.vthis);
|
|
158 ve.type = type;
|
|
159 return ve;
|
|
160 }
|
|
161
|
72
|
162 override elem* toElem(IRState* irs)
|
0
|
163 {
|
|
164 elem* ethis;
|
|
165 FuncDeclaration fd;
|
|
166
|
|
167 //printf("ThisExp::toElem()\n");
|
|
168 assert(irs.sthis);
|
|
169
|
|
170 if (var)
|
|
171 {
|
|
172 assert(var.parent);
|
|
173 fd = var.toParent2().isFuncDeclaration();
|
|
174 assert(fd);
|
|
175 ethis = getEthis(loc, irs, fd);
|
|
176 }
|
|
177 else
|
|
178 ethis = el_var(irs.sthis);
|
|
179
|
|
180 version (STRUCTTHISREF) {
|
|
181 if (type.ty == Tstruct)
|
|
182 {
|
|
183 ethis = el_una(OPind, TYstruct, ethis);
|
|
184 ethis.Enumbytes = cast(uint)type.size();
|
|
185 }
|
|
186 }
|
|
187 el_setLoc(ethis,loc);
|
|
188 return ethis;
|
|
189 }
|
|
190 }
|
|
191
|