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 {
|
|
134 assert(false);
|
|
135 }
|
|
136
|
72
|
137 override int inlineCost(InlineCostState* ics)
|
0
|
138 {
|
|
139 FuncDeclaration fd = ics.fd;
|
|
140 if (!ics.hdrscan)
|
|
141 if (fd.isNested() || !ics.hasthis)
|
|
142 return COST_MAX;
|
|
143
|
|
144 return 1;
|
|
145 }
|
|
146
|
72
|
147 override Expression doInline(InlineDoState ids)
|
0
|
148 {
|
|
149 //if (!ids.vthis)
|
|
150 //error("no 'this' when inlining %s", ids.parent.toChars());
|
|
151 if (!ids.vthis)
|
|
152 {
|
|
153 return this;
|
|
154 }
|
|
155
|
|
156 VarExp ve = new VarExp(loc, ids.vthis);
|
|
157 ve.type = type;
|
|
158 return ve;
|
|
159 }
|
|
160
|
72
|
161 override elem* toElem(IRState* irs)
|
0
|
162 {
|
|
163 elem* ethis;
|
|
164 FuncDeclaration fd;
|
|
165
|
|
166 //printf("ThisExp::toElem()\n");
|
|
167 assert(irs.sthis);
|
|
168
|
|
169 if (var)
|
|
170 {
|
|
171 assert(var.parent);
|
|
172 fd = var.toParent2().isFuncDeclaration();
|
|
173 assert(fd);
|
|
174 ethis = getEthis(loc, irs, fd);
|
|
175 }
|
|
176 else
|
|
177 ethis = el_var(irs.sthis);
|
|
178
|
|
179 version (STRUCTTHISREF) {
|
|
180 if (type.ty == Tstruct)
|
|
181 {
|
|
182 ethis = el_una(OPind, TYstruct, ethis);
|
|
183 ethis.Enumbytes = cast(uint)type.size();
|
|
184 }
|
|
185 }
|
|
186 el_setLoc(ethis,loc);
|
|
187 return ethis;
|
|
188 }
|
|
189 }
|
|
190
|