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