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