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