72
|
1 module dmd.NullExp;
|
|
2
|
114
|
3 import dmd.common;
|
72
|
4 import dmd.Expression;
|
|
5 import dmd.backend.elem;
|
|
6 import dmd.InterState;
|
|
7 import dmd.MATCH;
|
|
8 import dmd.Type;
|
|
9 import dmd.TY;
|
|
10 import dmd.TypeTypedef;
|
|
11 import dmd.OutBuffer;
|
|
12 import dmd.Loc;
|
|
13 import dmd.Scope;
|
|
14 import dmd.IRState;
|
|
15 import dmd.HdrGenState;
|
|
16 import dmd.TOK;
|
|
17 import dmd.backend.dt_t;
|
|
18 import dmd.backend.Util;
|
|
19
|
0
|
20 class NullExp : Expression
|
|
21 {
|
|
22 ubyte committed;
|
|
23
|
135
|
24 this(Loc loc, Type type = null)
|
72
|
25 {
|
0
|
26 super(loc, TOK.TOKnull, NullExp.sizeof);
|
135
|
27 this.type = type;
|
0
|
28 }
|
|
29
|
72
|
30 override Expression semantic(Scope sc)
|
0
|
31 {
|
72
|
32 version (LOGSEMANTIC) {
|
|
33 printf("NullExp.semantic('%s')\n", toChars());
|
|
34 }
|
|
35 // null is the same as (void*)0
|
|
36 if (!type)
|
|
37 type = Type.tvoid.pointerTo();
|
|
38
|
0
|
39 return this;
|
|
40 }
|
|
41
|
72
|
42 override bool isBool(bool result)
|
0
|
43 {
|
|
44 assert(false);
|
|
45 }
|
|
46
|
72
|
47 override int isConst()
|
0
|
48 {
|
63
|
49 return 0;
|
0
|
50 }
|
|
51
|
72
|
52 override void toCBuffer(OutBuffer buf, HdrGenState* hgs)
|
0
|
53 {
|
|
54 buf.writestring("null");
|
|
55 }
|
|
56
|
72
|
57 override void toMangleBuffer(OutBuffer buf)
|
0
|
58 {
|
64
|
59 buf.writeByte('n');
|
0
|
60 }
|
|
61
|
72
|
62 override MATCH implicitConvTo(Type t)
|
0
|
63 {
|
72
|
64 static if (false) {
|
|
65 printf("NullExp.implicitConvTo(this=%s, type=%s, t=%s, committed = %d)\n",
|
|
66 toChars(), type.toChars(), t.toChars(), committed);
|
|
67 }
|
|
68 if (this.type.equals(t))
|
|
69 return MATCH.MATCHexact;
|
|
70
|
|
71 /* Allow implicit conversions from invariant to mutable|const,
|
|
72 * and mutable to invariant. It works because, after all, a null
|
|
73 * doesn't actually point to anything.
|
|
74 */
|
|
75 if (t.invariantOf().equals(type.invariantOf()))
|
|
76 return MATCH.MATCHconst;
|
|
77
|
|
78 // null implicitly converts to any pointer type or dynamic array
|
|
79 if (type.ty == TY.Tpointer && type.nextOf().ty == TY.Tvoid)
|
|
80 {
|
|
81 if (t.ty == TY.Ttypedef)
|
|
82 t = (cast(TypeTypedef)t).sym.basetype;
|
|
83 if (t.ty == TY.Tpointer || t.ty == TY.Tarray ||
|
|
84 t.ty == TY.Taarray || t.ty == TY.Tclass ||
|
|
85 t.ty == TY.Tdelegate)
|
|
86 {
|
|
87 return committed ? MATCH.MATCHconvert : MATCH.MATCHexact;
|
|
88 }
|
|
89 }
|
|
90
|
0
|
91 return Expression.implicitConvTo(t);
|
|
92 }
|
|
93
|
72
|
94 override Expression castTo(Scope sc, Type t)
|
0
|
95 {
|
72
|
96 NullExp e;
|
|
97 Type tb;
|
|
98
|
|
99 //printf("NullExp::castTo(t = %p)\n", t);
|
|
100 if (type is t)
|
|
101 {
|
|
102 committed = 1;
|
|
103 return this;
|
|
104 }
|
|
105
|
|
106 e = cast(NullExp)copy();
|
|
107 e.committed = 1;
|
|
108 tb = t.toBasetype();
|
|
109 e.type = type.toBasetype();
|
|
110
|
|
111 if (tb !is e.type)
|
|
112 {
|
|
113 // null implicitly converts to any pointer type or dynamic array
|
|
114 if (e.type.ty == TY.Tpointer && e.type.nextOf().ty == TY.Tvoid &&
|
|
115 (tb.ty == TY.Tpointer || tb.ty == TY.Tarray || tb.ty == TY.Taarray ||
|
|
116 tb.ty == TY.Tdelegate))
|
|
117 {
|
|
118 static if (false) {
|
|
119 if (tb.ty == TY.Tdelegate)
|
|
120 {
|
|
121 TypeDelegate td = cast(TypeDelegate)tb;
|
|
122 TypeFunction tf = cast(TypeFunction)td.nextOf();
|
|
123
|
|
124 if (!tf.varargs && !(tf.arguments && tf.arguments.dim))
|
|
125 {
|
|
126 return Expression.castTo(sc, t);
|
|
127 }
|
|
128 }
|
|
129 }
|
|
130 }
|
|
131 else
|
|
132 {
|
|
133 return e.Expression.castTo(sc, t);
|
|
134 }
|
|
135 }
|
|
136 e.type = t;
|
0
|
137 return e;
|
|
138 }
|
|
139
|
72
|
140 override Expression interpret(InterState istate)
|
0
|
141 {
|
123
|
142 return this;
|
0
|
143 }
|
|
144
|
72
|
145 override elem* toElem(IRState* irs)
|
0
|
146 {
|
|
147 return el_long(type.totym(), 0);
|
|
148 }
|
|
149
|
72
|
150 override dt_t** toDt(dt_t** pdt)
|
0
|
151 {
|
72
|
152 assert(type);
|
0
|
153 return dtnzeros(pdt, cast(uint)type.size());
|
|
154 }
|
|
155 }
|
|
156
|