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