Mercurial > projects > ddmd
annotate dmd/TypeTypeof.d @ 178:e3afd1303184
Many small bugs fixed
Made all classes derive from TObject to detect memory leaks (functionality is disabled for now)
Began work on overriding backend memory allocations (to avoid memory leaks)
author | korDen |
---|---|
date | Sun, 17 Oct 2010 07:42:00 +0400 |
parents | 60bb0fe4563e |
children | b0d41ff5e0df |
rev | line source |
---|---|
0 | 1 module dmd.TypeTypeof; |
2 | |
114 | 3 import dmd.common; |
96 | 4 import dmd.TypeFunction; |
0 | 5 import dmd.TypeQualified; |
6 import dmd.Expression; | |
7 import dmd.Identifier; | |
8 import dmd.Scope; | |
9 import dmd.Loc; | |
10 import dmd.MOD; | |
11 import dmd.Type; | |
12 import dmd.Dsymbol; | |
13 import dmd.OutBuffer; | |
14 import dmd.HdrGenState; | |
15 import dmd.TY; | |
16 import dmd.Util; | |
17 import dmd.TOK; | |
18 | |
19 class TypeTypeof : TypeQualified | |
20 { | |
21 Expression exp; | |
22 | |
23 this(Loc loc, Expression exp) | |
24 { | |
178 | 25 register(); |
0 | 26 super(TY.Ttypeof, loc); |
27 this.exp = exp; | |
28 } | |
29 | |
72 | 30 override Type syntaxCopy() |
0 | 31 { |
51 | 32 //printf("TypeTypeof.syntaxCopy() %s\n", toChars()); |
33 TypeTypeof t; | |
34 | |
35 t = new TypeTypeof(loc, exp.syntaxCopy()); | |
36 t.syntaxCopyHelper(this); | |
37 t.mod = mod; | |
38 return t; | |
0 | 39 } |
40 | |
72 | 41 override Dsymbol toDsymbol(Scope sc) |
0 | 42 { |
43 Type t = semantic(loc, sc); | |
44 if (t is this) | |
45 return null; | |
46 | |
47 return t.toDsymbol(sc); | |
48 } | |
49 | |
72 | 50 override void toCBuffer2(OutBuffer buf, HdrGenState* hgs, MOD mod) |
0 | 51 { |
60 | 52 if (mod != this.mod) |
53 { | |
54 toCBuffer3(buf, hgs, mod); | |
55 return; | |
56 } | |
57 buf.writestring("typeof("); | |
58 exp.toCBuffer(buf, hgs); | |
59 buf.writeByte(')'); | |
60 toCBuffer2Helper(buf, hgs); | |
0 | 61 } |
62 | |
72 | 63 override Type semantic(Loc loc, Scope sc) |
0 | 64 { |
65 Expression e; | |
66 Type t; | |
67 | |
60 | 68 //printf("TypeTypeof.semantic() %.*s\n", toChars()); |
0 | 69 |
70 //static int nest; if (++nest == 50) *(char*)0=0; | |
71 | |
72 /+static if (false) { | |
73 /* Special case for typeof(this) and typeof(super) since both | |
74 * should work even if they are not inside a non-static member function | |
75 */ | |
76 if (exp.op == TOK.TOKthis || exp.op == TOK.TOKsuper) | |
77 { | |
78 / / Find enclosing struct or class | |
79 for (Dsymbol *s = sc.parent; 1; s = s.parent) | |
80 { | |
81 ClassDeclaration *cd; | |
82 StructDeclaration *sd; | |
83 | |
84 if (!s) | |
85 { | |
86 error(loc, "%s is not in a struct or class scope", exp.toChars()); | |
87 goto Lerr; | |
88 } | |
89 cd = s.isClassDeclaration(); | |
90 if (cd) | |
91 { | |
92 if (exp.op == TOK.TOKsuper) | |
93 { | |
94 cd = cd.baseClass; | |
95 if (!cd) | |
96 { error(loc, "class %s has no 'super'", s.toChars()); | |
97 goto Lerr; | |
98 } | |
99 } | |
100 t = cd.type; | |
101 break; | |
102 } | |
103 sd = s.isStructDeclaration(); | |
104 if (sd) | |
105 { | |
106 if (exp.op == TOK.TOKsuper) | |
107 { | |
108 error(loc, "struct %s has no 'super'", sd.toChars()); | |
109 goto Lerr; | |
110 } | |
111 t = sd.type.pointerTo(); | |
112 break; | |
113 } | |
114 } | |
115 } | |
116 else | |
117 }+/ | |
118 { | |
119 sc.intypeof++; | |
120 exp = exp.semantic(sc); | |
96 | 121 if (exp.type && exp.type.ty == Tfunction && |
122 (cast(TypeFunction)exp.type).isproperty) | |
123 exp = resolveProperties(sc, exp); | |
0 | 124 sc.intypeof--; |
125 if (exp.op == TOK.TOKtype) | |
126 { | |
127 error(loc, "argument %s to typeof is not an expression", exp.toChars()); | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
128 goto Lerr; |
0 | 129 } |
130 t = exp.type; | |
131 if (!t) | |
132 { | |
133 error(loc, "expression (%s) has no type", exp.toChars()); | |
134 goto Lerr; | |
135 } | |
136 if (t.ty == TY.Ttypeof) | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
137 { |
0 | 138 error(loc, "forward reference to %s", toChars()); |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
139 goto Lerr; |
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
114
diff
changeset
|
140 } |
0 | 141 |
142 /* typeof should reflect the true type, | |
143 * not what 'auto' would have gotten us. | |
144 */ | |
145 //t = t.toHeadMutable(); | |
146 } | |
147 if (idents.dim) | |
148 { | |
149 Dsymbol s = t.toDsymbol(sc); | |
150 for (size_t i = 0; i < idents.dim; i++) | |
151 { | |
152 if (!s) | |
153 break; | |
154 Identifier id = cast(Identifier)idents.data[i]; | |
155 s = s.searchX(loc, sc, id); | |
156 } | |
157 | |
158 if (s) | |
159 { | |
160 t = s.getType(); | |
161 if (!t) | |
162 { | |
163 error(loc, "%s is not a type", s.toChars()); | |
164 goto Lerr; | |
165 } | |
166 } | |
167 else | |
168 { | |
169 error(loc, "cannot resolve .property for %s", toChars()); | |
170 goto Lerr; | |
171 } | |
172 } | |
173 return t; | |
174 | |
175 Lerr: | |
176 return tvoid; | |
177 } | |
178 | |
72 | 179 override ulong size(Loc loc) |
0 | 180 { |
181 assert(false); | |
182 } | |
72 | 183 } |