Mercurial > projects > ddmd
annotate dmd/ThisExp.d @ 191:52188e7e3fb5
Fixed deprecated features, now compiles with DMD2.058
Also changed Array allocation policy:
Now doesn't reallocate but malloc's, followed by a memcpy (no free).
(this fixes a crash while compiling druntime. Same bug in dmd)
author | korDen@korDen-pc |
---|---|
date | Sun, 25 Mar 2012 03:11:12 +0400 |
parents | b0d41ff5e0df |
children |
rev | line source |
---|---|
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 |
187
b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents:
178
diff
changeset
|
30 import dmd.DDMDExtensions; |
b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents:
178
diff
changeset
|
31 |
0 | 32 class ThisExp : Expression |
33 { | |
187
b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents:
178
diff
changeset
|
34 mixin insertMemberExtension!(typeof(this)); |
b0d41ff5e0df
Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents:
178
diff
changeset
|
35 |
0 | 36 Declaration var; |
37 | |
38 this(Loc loc) | |
39 { | |
178 | 40 register(); |
0 | 41 super(loc, TOK.TOKthis, ThisExp.sizeof); |
42 //printf("ThisExp::ThisExp() loc = %d\n", loc.linnum); | |
43 } | |
44 | |
72 | 45 override Expression semantic(Scope sc) |
0 | 46 { |
47 FuncDeclaration fd; | |
48 FuncDeclaration fdthis; | |
49 | |
50 version (LOGSEMANTIC) { | |
51 printf("ThisExp::semantic()\n"); | |
52 } | |
53 if (type) | |
54 { | |
55 //assert(global.errors || var); | |
56 return this; | |
57 } | |
58 | |
59 /* Special case for typeof(this) and typeof(super) since both | |
60 * should work even if they are not inside a non-static member function | |
61 */ | |
62 if (sc.intypeof) | |
63 { | |
64 // Find enclosing struct or class | |
65 for (Dsymbol s = sc.parent; 1; s = s.parent) | |
66 { | |
67 if (!s) | |
68 { | |
69 error("%s is not in a class or struct scope", toChars()); | |
70 goto Lerr; | |
71 } | |
72 ClassDeclaration cd = s.isClassDeclaration(); | |
73 if (cd) | |
74 { | |
75 type = cd.type; | |
76 return this; | |
77 } | |
78 StructDeclaration sd = s.isStructDeclaration(); | |
79 if (sd) | |
80 { | |
81 version (STRUCTTHISREF) { | |
82 type = sd.type; | |
83 } else { | |
84 type = sd.type.pointerTo(); | |
85 } | |
86 return this; | |
87 } | |
88 } | |
89 } | |
90 | |
91 fdthis = sc.parent.isFuncDeclaration(); | |
92 fd = hasThis(sc); // fd is the uplevel function with the 'this' variable | |
93 if (!fd) | |
94 goto Lerr; | |
95 | |
96 assert(fd.vthis); | |
97 var = fd.vthis; | |
98 assert(var.parent); | |
99 type = var.type; | |
100 var.isVarDeclaration().checkNestedReference(sc, loc); | |
101 if (!sc.intypeof) | |
102 sc.callSuper |= CSXthis; | |
103 return this; | |
104 | |
105 Lerr: | |
106 error("'this' is only defined in non-static member functions, not %s", sc.parent.toChars()); | |
107 type = Type.terror; | |
108 return this; | |
109 } | |
110 | |
72 | 111 override Expression interpret(InterState istate) |
0 | 112 { |
113 assert(false); | |
114 } | |
115 | |
72 | 116 override bool isBool(bool result) |
0 | 117 { |
118 return result ? true : false; | |
119 } | |
120 | |
72 | 121 override void toCBuffer(OutBuffer buf, HdrGenState* hgs) |
0 | 122 { |
123 buf.writestring("this"); | |
124 } | |
125 | |
126 version (DMDV2) { | |
191
52188e7e3fb5
Fixed deprecated features, now compiles with DMD2.058
korDen@korDen-pc
parents:
187
diff
changeset
|
127 override bool isLvalue() |
0 | 128 { |
191
52188e7e3fb5
Fixed deprecated features, now compiles with DMD2.058
korDen@korDen-pc
parents:
187
diff
changeset
|
129 return true; |
0 | 130 } |
131 } | |
72 | 132 override Expression toLvalue(Scope sc, Expression e) |
0 | 133 { |
134 return this; | |
135 } | |
136 | |
72 | 137 override void scanForNestedRef(Scope sc) |
0 | 138 { |
123 | 139 assert(var); |
140 var.isVarDeclaration().checkNestedReference(sc, Loc(0)); | |
0 | 141 } |
142 | |
72 | 143 override int inlineCost(InlineCostState* ics) |
0 | 144 { |
145 FuncDeclaration fd = ics.fd; | |
146 if (!ics.hdrscan) | |
147 if (fd.isNested() || !ics.hasthis) | |
148 return COST_MAX; | |
149 | |
150 return 1; | |
151 } | |
152 | |
72 | 153 override Expression doInline(InlineDoState ids) |
0 | 154 { |
155 //if (!ids.vthis) | |
156 //error("no 'this' when inlining %s", ids.parent.toChars()); | |
157 if (!ids.vthis) | |
158 { | |
159 return this; | |
160 } | |
161 | |
162 VarExp ve = new VarExp(loc, ids.vthis); | |
163 ve.type = type; | |
164 return ve; | |
165 } | |
166 | |
72 | 167 override elem* toElem(IRState* irs) |
0 | 168 { |
169 elem* ethis; | |
170 FuncDeclaration fd; | |
171 | |
172 //printf("ThisExp::toElem()\n"); | |
173 assert(irs.sthis); | |
174 | |
175 if (var) | |
176 { | |
177 assert(var.parent); | |
178 fd = var.toParent2().isFuncDeclaration(); | |
179 assert(fd); | |
180 ethis = getEthis(loc, irs, fd); | |
181 } | |
182 else | |
183 ethis = el_var(irs.sthis); | |
184 | |
185 version (STRUCTTHISREF) { | |
186 if (type.ty == Tstruct) | |
187 { | |
188 ethis = el_una(OPind, TYstruct, ethis); | |
189 ethis.Enumbytes = cast(uint)type.size(); | |
190 } | |
191 } | |
192 el_setLoc(ethis,loc); | |
193 return ethis; | |
194 } | |
195 } | |
196 |