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 {
|
|
57 assert(false);
|
|
58 }
|
|
59
|
|
60 Type semantic(Loc loc, Scope sc)
|
|
61 {
|
|
62 Expression e;
|
|
63 Type t;
|
|
64
|
|
65 //printf("TypeTypeof.semantic() %p\n", this);
|
|
66
|
|
67 //static int nest; if (++nest == 50) *(char*)0=0;
|
|
68
|
|
69 /+static if (false) {
|
|
70 /* Special case for typeof(this) and typeof(super) since both
|
|
71 * should work even if they are not inside a non-static member function
|
|
72 */
|
|
73 if (exp.op == TOK.TOKthis || exp.op == TOK.TOKsuper)
|
|
74 {
|
|
75 / / Find enclosing struct or class
|
|
76 for (Dsymbol *s = sc.parent; 1; s = s.parent)
|
|
77 {
|
|
78 ClassDeclaration *cd;
|
|
79 StructDeclaration *sd;
|
|
80
|
|
81 if (!s)
|
|
82 {
|
|
83 error(loc, "%s is not in a struct or class scope", exp.toChars());
|
|
84 goto Lerr;
|
|
85 }
|
|
86 cd = s.isClassDeclaration();
|
|
87 if (cd)
|
|
88 {
|
|
89 if (exp.op == TOK.TOKsuper)
|
|
90 {
|
|
91 cd = cd.baseClass;
|
|
92 if (!cd)
|
|
93 { error(loc, "class %s has no 'super'", s.toChars());
|
|
94 goto Lerr;
|
|
95 }
|
|
96 }
|
|
97 t = cd.type;
|
|
98 break;
|
|
99 }
|
|
100 sd = s.isStructDeclaration();
|
|
101 if (sd)
|
|
102 {
|
|
103 if (exp.op == TOK.TOKsuper)
|
|
104 {
|
|
105 error(loc, "struct %s has no 'super'", sd.toChars());
|
|
106 goto Lerr;
|
|
107 }
|
|
108 t = sd.type.pointerTo();
|
|
109 break;
|
|
110 }
|
|
111 }
|
|
112 }
|
|
113 else
|
|
114 }+/
|
|
115 {
|
|
116 sc.intypeof++;
|
|
117 exp = exp.semantic(sc);
|
|
118 sc.intypeof--;
|
|
119 if (exp.op == TOK.TOKtype)
|
|
120 {
|
|
121 error(loc, "argument %s to typeof is not an expression", exp.toChars());
|
|
122 }
|
|
123 t = exp.type;
|
|
124 if (!t)
|
|
125 {
|
|
126 error(loc, "expression (%s) has no type", exp.toChars());
|
|
127 goto Lerr;
|
|
128 }
|
|
129 if (t.ty == TY.Ttypeof)
|
|
130 error(loc, "forward reference to %s", toChars());
|
|
131
|
|
132 /* typeof should reflect the true type,
|
|
133 * not what 'auto' would have gotten us.
|
|
134 */
|
|
135 //t = t.toHeadMutable();
|
|
136 }
|
|
137 if (idents.dim)
|
|
138 {
|
|
139 Dsymbol s = t.toDsymbol(sc);
|
|
140 for (size_t i = 0; i < idents.dim; i++)
|
|
141 {
|
|
142 if (!s)
|
|
143 break;
|
|
144 Identifier id = cast(Identifier)idents.data[i];
|
|
145 s = s.searchX(loc, sc, id);
|
|
146 }
|
|
147
|
|
148 if (s)
|
|
149 {
|
|
150 t = s.getType();
|
|
151 if (!t)
|
|
152 {
|
|
153 error(loc, "%s is not a type", s.toChars());
|
|
154 goto Lerr;
|
|
155 }
|
|
156 }
|
|
157 else
|
|
158 {
|
|
159 error(loc, "cannot resolve .property for %s", toChars());
|
|
160 goto Lerr;
|
|
161 }
|
|
162 }
|
|
163 return t;
|
|
164
|
|
165 Lerr:
|
|
166 return tvoid;
|
|
167 }
|
|
168
|
|
169 ulong size(Loc loc)
|
|
170 {
|
|
171 assert(false);
|
|
172 }
|
|
173 } |