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