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