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