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