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