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());
|
|
135 }
|
|
136 t = exp.type;
|
|
137 if (!t)
|
|
138 {
|
|
139 error(loc, "expression (%s) has no type", exp.toChars());
|
|
140 goto Lerr;
|
|
141 }
|
|
142 if (t.ty == TY.Ttypeof)
|
|
143 error(loc, "forward reference to %s", toChars());
|
|
144
|
|
145 /* typeof should reflect the true type,
|
|
146 * not what 'auto' would have gotten us.
|
|
147 */
|
|
148 //t = t.toHeadMutable();
|
|
149 }
|
|
150 if (idents.dim)
|
|
151 {
|
|
152 Dsymbol s = t.toDsymbol(sc);
|
|
153 for (size_t i = 0; i < idents.dim; i++)
|
|
154 {
|
|
155 if (!s)
|
|
156 break;
|
|
157 Identifier id = cast(Identifier)idents.data[i];
|
|
158 s = s.searchX(loc, sc, id);
|
|
159 }
|
|
160
|
|
161 if (s)
|
|
162 {
|
|
163 t = s.getType();
|
|
164 if (!t)
|
|
165 {
|
|
166 error(loc, "%s is not a type", s.toChars());
|
|
167 goto Lerr;
|
|
168 }
|
|
169 }
|
|
170 else
|
|
171 {
|
|
172 error(loc, "cannot resolve .property for %s", toChars());
|
|
173 goto Lerr;
|
|
174 }
|
|
175 }
|
|
176 return t;
|
|
177
|
|
178 Lerr:
|
|
179 return tvoid;
|
|
180 }
|
|
181
|
72
|
182 override ulong size(Loc loc)
|
0
|
183 {
|
|
184 assert(false);
|
|
185 }
|
72
|
186 }
|