comparison dmd/StructLiteralExp.d @ 63:cab4c37afb89

A bunch of implementations
author korDen
date Mon, 23 Aug 2010 16:52:24 +0400
parents 51605de93870
children 4290d870944a
comparison
equal deleted inserted replaced
62:6557375aff35 63:cab4c37afb89
1 module dmd.StructLiteralExp; 1 module dmd.StructLiteralExp;
2 2
3 import dmd.Expression; 3 import dmd.Expression;
4 import dmd.expression.Util;
5 import dmd.ErrorExp;
6 import dmd.Dsymbol;
7 import dmd.VarDeclaration;
4 import dmd.StructDeclaration; 8 import dmd.StructDeclaration;
5 import dmd.backend.elem; 9 import dmd.backend.elem;
6 import dmd.InterState; 10 import dmd.InterState;
7 import dmd.MATCH; 11 import dmd.MATCH;
8 import dmd.WANT; 12 import dmd.WANT;
13 import dmd.TY;
9 import dmd.Type; 14 import dmd.Type;
10 import dmd.OutBuffer; 15 import dmd.OutBuffer;
11 import dmd.Loc; 16 import dmd.Loc;
12 import dmd.Scope; 17 import dmd.Scope;
13 import dmd.InlineCostState; 18 import dmd.InlineCostState;
46 assert(false); 51 assert(false);
47 } 52 }
48 53
49 Expression semantic(Scope sc) 54 Expression semantic(Scope sc)
50 { 55 {
51 assert(false); 56 Expression e;
57 int nfields = sd.fields.dim - sd.isnested;
58
59 version (LOGSEMANTIC) {
60 printf("StructLiteralExp.semantic('%s')\n", toChars());
61 }
62 if (type)
63 return this;
64
65 // Run semantic() on each element
66 for (size_t i = 0; i < elements.dim; i++)
67 {
68 e = cast(Expression)elements.data[i];
69 if (!e)
70 continue;
71 e = e.semantic(sc);
72 elements.data[i] = cast(void*)e;
73 }
74 expandTuples(elements);
75 size_t offset = 0;
76 for (size_t i = 0; i < elements.dim; i++)
77 {
78 e = cast(Expression)elements.data[i];
79 if (!e)
80 continue;
81
82 if (!e.type)
83 error("%s has no value", e.toChars());
84 e = resolveProperties(sc, e);
85 if (i >= nfields)
86 {
87 error("more initializers than fields of %s", sd.toChars());
88 return new ErrorExp();
89 }
90 Dsymbol s = cast(Dsymbol)sd.fields.data[i];
91 VarDeclaration v = s.isVarDeclaration();
92 assert(v);
93 if (v.offset < offset)
94 error("overlapping initialization for %s", v.toChars());
95 offset = v.offset + cast(uint)v.type.size();
96
97 Type telem = v.type;
98 while (!e.implicitConvTo(telem) && telem.toBasetype().ty == Tsarray)
99 {
100 /* Static array initialization, as in:
101 * T[3][5] = e;
102 */
103 telem = telem.toBasetype().nextOf();
104 }
105
106 e = e.implicitCastTo(sc, telem);
107
108 elements.data[i] = cast(void*)e;
109 }
110
111 /* Fill out remainder of elements[] with default initializers for fields[]
112 */
113 for (size_t i = elements.dim; i < nfields; i++)
114 {
115 Dsymbol s = cast(Dsymbol)sd.fields.data[i];
116 VarDeclaration v = s.isVarDeclaration();
117 assert(v);
118 assert(!v.isThisDeclaration());
119
120 if (v.offset < offset)
121 {
122 e = null;
123 sd.hasUnions = 1;
124 }
125 else
126 {
127 if (v.init)
128 {
129 e = v.init.toExpression();
130 if (!e)
131 error("cannot make expression out of initializer for %s", v.toChars());
132 }
133 else
134 {
135 e = v.type.defaultInit(Loc(0));
136 e.loc = loc;
137 }
138 offset = v.offset + cast(uint)v.type.size();
139 }
140 elements.push(cast(void*)e);
141 }
142
143 type = sd.type;
144 return this;
52 } 145 }
53 146
54 Expression getField(Type type, uint offset) 147 Expression getField(Type type, uint offset)
55 { 148 {
56 assert(false); 149 assert(false);
100 } 193 }
101 } 194 }
102 return this; 195 return this;
103 } 196 }
104 197
105 Expression interpret(InterState* istate) 198 Expression interpret(InterState istate)
106 { 199 {
107 assert(false); 200 assert(false);
108 } 201 }
109 202
110 dt_t** toDt(dt_t** pdt) 203 dt_t** toDt(dt_t** pdt)