annotate dmd/NullExp.d @ 0:10317f0c89a5

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