annotate dmd/StructDeclaration.d @ 13:427f8aa74d28

On the road to make Phobos compilable
author korDen
date Mon, 12 Apr 2010 16:29:33 +0400
parents 10317f0c89a5
children 0aa7d1437ada
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1 module dmd.StructDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
2
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
3 import dmd.AggregateDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
4 import dmd.FuncDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
5 import dmd.DeclarationExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
6 import dmd.VoidInitializer;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
7 import dmd.Initializer;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
8 import dmd.ExpInitializer;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
9 import dmd.TOK;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
10 import dmd.Statement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
11 import dmd.VarExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
12 import dmd.CompoundStatement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
13 import dmd.AssignExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
14 import dmd.DotVarExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
15 import dmd.AddrExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
16 import dmd.CastExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
17 import dmd.PostBlitDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
18 import dmd.Lexer;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
19 import dmd.ExpStatement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
20 import dmd.DotIdExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
21 import dmd.TypeSArray;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
22 import dmd.ThisExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
23 import dmd.ThisDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
24 import dmd.TypeFunction;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
25 import dmd.Argument;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
26 import dmd.Id;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
27 import dmd.TY;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
28 import dmd.LINK;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
29 import dmd.Type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
30 import dmd.DsymbolTable;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
31 import dmd.ArrayTypes;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
32 import dmd.Loc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
33 import dmd.STC;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
34 import dmd.Identifier;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
35 import dmd.TemplateInstance;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
36 import dmd.Dsymbol;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
37 import dmd.Scope;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
38 import dmd.OutBuffer;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
39 import dmd.HdrGenState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
40 import dmd.PROT;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
41 import dmd.TypeStruct;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
42 import dmd.expression.Util;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
43 import dmd.Expression;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
44 import dmd.IdentifierExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
45 import dmd.PtrExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
46 import dmd.CallExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
47 import dmd.ReturnStatement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
48 import dmd.ScopeDsymbol;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
49 import dmd.Module;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
50 import dmd.VarDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
51 import dmd.InvariantDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
52 import dmd.NewDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
53 import dmd.DeleteDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
54 import dmd.Global;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
55
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
56 import dmd.backend.dt_t;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
57 import dmd.backend.Util;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
58 import dmd.backend.SC;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
59 import dmd.backend.DT;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
60 import dmd.backend.FL;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
61 import dmd.backend.glue;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
62
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
63 class StructDeclaration : AggregateDeclaration
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
64 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
65 bool zeroInit; // true if initialize with 0 fill
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
66
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
67 version (DMDV2) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
68 int hasIdentityAssign; // !=0 if has identity opAssign
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
69 FuncDeclaration cpctor; // generated copy-constructor, if any
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
70
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
71 FuncDeclarations postblits; // Array of postblit functions
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
72 FuncDeclaration postblit; // aggregate postblit
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
73 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
74
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
75 this(Loc loc, Identifier id)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
76 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
77 super(loc, id);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
78
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
79 // For forward references
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
80 type = new TypeStruct(this);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
81
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
82 postblits = new FuncDeclarations(); ///
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
83 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
84
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
85 Dsymbol syntaxCopy(Dsymbol s)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
86 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
87 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
88 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
89
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
90 void semantic(Scope sc)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
91 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
92 int i;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
93 Scope sc2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
94
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
95 //printf("+StructDeclaration.semantic(this=%p, '%s')\n", this, toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
96
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
97 //static int count; if (++count == 20) *(char*)0=0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
98
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
99 assert(type);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
100 if (!members) // if forward reference
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
101 return;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
102
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
103 if (symtab)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
104 { if (!scope_)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
105 return; // semantic() already completed
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
106 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
107 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
108 symtab = new DsymbolTable();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
109
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
110 Scope scx = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
111 if (scope_)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
112 { sc = scope_;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
113 scx = scope_; // save so we don't make redundant copies
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
114 scope_ = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
115 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
116
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
117 parent = sc.parent;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
118 type = type.semantic(loc, sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
119 version (STRUCTTHISREF) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
120 handle = type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
121 } else {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
122 handle = type.pointerTo();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
123 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
124 structalign = sc.structalign;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
125 protection = sc.protection;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
126 storage_class |= sc.stc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
127 if (sc.stc & STC.STCdeprecated)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
128 isdeprecated = 1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
129 assert(!isAnonymous());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
130 if (sc.stc & STC.STCabstract)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
131 error("structs, unions cannot be abstract");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
132 version (DMDV2) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
133 if (storage_class & STC.STCimmutable)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
134 type = type.invariantOf();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
135 else if (storage_class & STC.STCconst)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
136 type = type.constOf();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
137 else if (storage_class & STC.STCshared)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
138 type = type.sharedOf();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
139 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
140
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
141 if (sizeok == 0) // if not already done the addMember step
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
142 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
143 int hasfunctions = 0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
144 for (i = 0; i < members.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
145 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
146 Dsymbol s = cast(Dsymbol)members.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
147 //printf("adding member '%s' to '%s'\n", s.toChars(), this.toChars());
13
427f8aa74d28 On the road to make Phobos compilable
korDen
parents: 0
diff changeset
148 s.addMember(sc, this, true);
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
149 if (s.isFuncDeclaration())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
150 hasfunctions = 1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
151 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
152
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
153 // If nested struct, add in hidden 'this' pointer to outer scope
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
154 if (hasfunctions && !(storage_class & STC.STCstatic))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
155 { Dsymbol s = toParent2();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
156 if (s)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
157 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
158 AggregateDeclaration ad = s.isAggregateDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
159 FuncDeclaration fd = s.isFuncDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
160
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
161 TemplateInstance ti;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
162 if (ad && (ti = ad.parent.isTemplateInstance()) !is null && ti.isnested || fd)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
163 { isnested = true;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
164 Type t;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
165 if (ad)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
166 t = ad.handle;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
167 else if (fd)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
168 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
169 AggregateDeclaration add = fd.isMember2();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
170 if (add)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
171 t = add.handle;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
172 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
173 t = Type.tvoidptr;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
174 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
175 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
176 assert(0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
177 if (t.ty == TY.Tstruct)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
178 t = Type.tvoidptr; // t should not be a ref type
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
179 assert(!vthis);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
180 vthis = new ThisDeclaration(loc, t);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
181 //vthis.storage_class |= STC.STCref;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
182 members.push(cast(void*)vthis);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
183 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
184 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
185 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
186 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
187
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
188 sizeok = 0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
189 sc2 = sc.push(this);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
190 sc2.stc &= storage_class & STC.STC_TYPECTOR;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
191 sc2.parent = this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
192 if (isUnionDeclaration())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
193 sc2.inunion = 1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
194 sc2.protection = PROT.PROTpublic;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
195 sc2.explicitProtection = 0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
196
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
197 int members_dim = members.dim;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
198 for (i = 0; i < members_dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
199 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
200 Dsymbol s = cast(Dsymbol)members.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
201 s.semantic(sc2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
202 if (isUnionDeclaration())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
203 sc2.offset = 0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
204 static if (false) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
205 if (sizeok == 2)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
206 { //printf("forward reference\n");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
207 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
208 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
209 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
210 Type t;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
211 if (s.isDeclaration() &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
212 (t = s.isDeclaration().type) !is null &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
213 t.toBasetype().ty == TY.Tstruct)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
214 { StructDeclaration sd = cast(StructDeclaration)t.toDsymbol(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
215 if (sd.isnested)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
216 error("inner struct %s cannot be a field", sd.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
217 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
218 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
219
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
220 /* The TypeInfo_Struct is expecting an opEquals and opCmp with
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
221 * a parameter that is a pointer to the struct. But if there
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
222 * isn't one, but is an opEquals or opCmp with a value, write
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
223 * another that is a shell around the value:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
224 * int opCmp(struct *p) { return opCmp(*p); }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
225 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
226
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
227 TypeFunction tfeqptr;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
228 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
229 Arguments arguments = new Arguments;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
230 Argument arg = new Argument(STC.STCin, handle, Id.p, null);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
231
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
232 arguments.push(cast(void*)arg);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
233 tfeqptr = new TypeFunction(arguments, Type.tint32, 0, LINK.LINKd);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
234 tfeqptr = cast(TypeFunction)tfeqptr.semantic(Loc(0), sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
235 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
236
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
237 TypeFunction tfeq;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
238 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
239 Arguments arguments = new Arguments;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
240 Argument arg = new Argument(STC.STCin, type, null, null);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
241
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
242 arguments.push(cast(void*)arg);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
243 tfeq = new TypeFunction(arguments, Type.tint32, 0, LINK.LINKd);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
244 tfeq = cast(TypeFunction)tfeq.semantic(Loc(0), sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
245 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
246
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
247 Identifier id = Id.eq;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
248 for (int j = 0; j < 2; j++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
249 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
250 Dsymbol s = search_function(this, id);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
251 FuncDeclaration fdx = s ? s.isFuncDeclaration() : null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
252 if (fdx)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
253 { FuncDeclaration fd = fdx.overloadExactMatch(tfeqptr);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
254 if (!fd)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
255 { fd = fdx.overloadExactMatch(tfeq);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
256 if (fd)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
257 { // Create the thunk, fdptr
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
258 FuncDeclaration fdptr = new FuncDeclaration(loc, loc, fdx.ident, STC.STCundefined, tfeqptr);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
259 Expression e = new IdentifierExp(loc, Id.p);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
260 e = new PtrExp(loc, e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
261 Expressions args = new Expressions();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
262 args.push(cast(void*)e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
263 e = new IdentifierExp(loc, id);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
264 e = new CallExp(loc, e, args);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
265 fdptr.fbody = new ReturnStatement(loc, e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
266 ScopeDsymbol ss = fdx.parent.isScopeDsymbol();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
267 assert(ss);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
268 ss.members.push(cast(void*)fdptr);
13
427f8aa74d28 On the road to make Phobos compilable
korDen
parents: 0
diff changeset
269 fdptr.addMember(sc, ss, true);
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
270 fdptr.semantic(sc2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
271 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
272 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
273 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
274
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
275 id = Id.cmp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
276 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
277 version (DMDV2) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
278 dtor = buildDtor(sc2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
279 postblit = buildPostBlit(sc2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
280 cpctor = buildCpCtor(sc2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
281 buildOpAssign(sc2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
282 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
283
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
284 sc2.pop();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
285
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
286 if (sizeok == 2)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
287 { // semantic() failed because of forward references.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
288 // Unwind what we did, and defer it for later
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
289 fields.setDim(0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
290 structsize = 0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
291 alignsize = 0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
292 structalign = 0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
293
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
294 scope_ = scx ? scx : new Scope(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
295 scope_.setNoFree();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
296 scope_.module_.addDeferredSemantic(this);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
297 //printf("\tdeferring %s\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
298 return;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
299 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
300
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
301 // 0 sized struct's are set to 1 byte
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
302 if (structsize == 0)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
303 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
304 structsize = 1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
305 alignsize = 1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
306 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
307
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
308 // Round struct size up to next alignsize boundary.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
309 // This will ensure that arrays of structs will get their internals
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
310 // aligned properly.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
311 structsize = (structsize + alignsize - 1) & ~(alignsize - 1);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
312
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
313 sizeok = 1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
314 Module.dprogress++;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
315
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
316 //printf("-StructDeclaration.semantic(this=%p, '%s')\n", this, toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
317
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
318 // Determine if struct is all zeros or not
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
319 zeroInit = true;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
320 for (i = 0; i < fields.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
321 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
322 Dsymbol s = cast(Dsymbol)fields.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
323 VarDeclaration vd = s.isVarDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
324 if (vd && !vd.isDataseg())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
325 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
326 if (vd.init)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
327 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
328 // Should examine init to see if it is really all 0's
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
329 zeroInit = false;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
330 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
331 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
332 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
333 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
334 if (!vd.type.isZeroInit(loc))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
335 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
336 zeroInit = false;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
337 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
338 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
339 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
340 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
341 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
342
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
343 /* Look for special member functions.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
344 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
345 version (DMDV2) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
346 ctor = search(Loc(0), Id.ctor, 0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
347 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
348 inv = cast(InvariantDeclaration)search(Loc(0), Id.classInvariant, 0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
349 aggNew = cast(NewDeclaration)search(Loc(0), Id.classNew, 0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
350 aggDelete = cast(DeleteDeclaration)search(Loc(0), Id.classDelete, 0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
351
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
352 if (sc.func)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
353 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
354 semantic2(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
355 semantic3(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
356 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
357 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
358
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
359 void toCBuffer(OutBuffer buf, HdrGenState* hgs)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
360 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
361 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
362 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
363
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
364 string mangle()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
365 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
366 //printf("StructDeclaration.mangle() '%s'\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
367 return Dsymbol.mangle();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
368 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
369
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
370 string kind()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
371 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
372 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
373 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
374
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
375 /*******************************************
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
376 * We need an opAssign for the struct if
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
377 * it has a destructor or a postblit.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
378 * We need to generate one if a user-specified one does not exist.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
379 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
380 bool needOpAssign()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
381 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
382 static if (false) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
383 printf("StructDeclaration.needOpAssign() %s\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
384 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
385 if (hasIdentityAssign)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
386 goto Ldontneed;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
387
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
388 if (dtor || postblit)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
389 goto Lneed;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
390
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
391 /* If any of the fields need an opAssign, then we
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
392 * need it too.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
393 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
394 for (size_t i = 0; i < fields.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
395 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
396 Dsymbol s = cast(Dsymbol)fields.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
397 VarDeclaration v = s.isVarDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
398 assert(v && v.storage_class & STC.STCfield);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
399 if (v.storage_class & STC.STCref)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
400 continue;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
401 Type tv = v.type.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
402 while (tv.ty == TY.Tsarray)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
403 { TypeSArray ta = cast(TypeSArray)tv;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
404 tv = tv.nextOf().toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
405 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
406 if (tv.ty == TY.Tstruct)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
407 { TypeStruct ts = cast(TypeStruct)tv;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
408 StructDeclaration sd = ts.sym;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
409 if (sd.needOpAssign())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
410 goto Lneed;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
411 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
412 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
413 Ldontneed:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
414 static if (false) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
415 printf("\tdontneed\n");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
416 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
417 return false;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
418
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
419 Lneed:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
420 static if (false) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
421 printf("\tneed\n");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
422 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
423 return true;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
424 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
425
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
426 /******************************************
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
427 * Build opAssign for struct.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
428 * S* opAssign(S s) { ... }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
429 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
430 FuncDeclaration buildOpAssign(Scope sc)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
431 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
432 if (!needOpAssign())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
433 return null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
434
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
435 //printf("StructDeclaration.buildOpAssign() %s\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
436
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
437 FuncDeclaration fop = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
438
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
439 Argument param = new Argument(STC.STCnodtor, type, Id.p, null);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
440 Arguments fparams = new Arguments;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
441 fparams.push(cast(void*)param);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
442 Type ftype = new TypeFunction(fparams, handle, false, LINK.LINKd);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
443 version (STRUCTTHISREF) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
444 (cast(TypeFunction)ftype).isref = 1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
445 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
446
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
447 fop = new FuncDeclaration(Loc(0), Loc(0), Id.assign, STC.STCundefined, ftype);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
448
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
449 Expression e = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
450 if (postblit)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
451 { /* Swap:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
452 * tmp = *this; *this = s; tmp.dtor();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
453 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
454 //printf("\tswap copy\n");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
455 Identifier idtmp = Lexer.uniqueId("__tmp");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
456 VarDeclaration tmp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
457 AssignExp ec = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
458 if (dtor)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
459 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
460 tmp = new VarDeclaration(Loc(0), type, idtmp, new VoidInitializer(Loc(0)));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
461 tmp.noauto = true;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
462 e = new DeclarationExp(Loc(0), tmp);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
463
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
464 Expression e2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
465 version (STRUCTTHISREF) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
466 e2 = new ThisExp(Loc(0));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
467 } else {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
468 e2 = new PtrExp(Loc(0), new ThisExp(Loc(0)));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
469 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
470 ec = new AssignExp(Loc(0), new VarExp(Loc(0), tmp), e2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
471 ec.op = TOK.TOKblit;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
472 e = Expression.combine(e, ec);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
473 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
474 Expression e2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
475 version (STRUCTTHISREF) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
476 e2 = new ThisExp(Loc(0));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
477 } else {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
478 e2 = new PtrExp(Loc(0), new ThisExp(Loc(0)));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
479 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
480
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
481 ec = new AssignExp(Loc(0), e2, new IdentifierExp(Loc(0), Id.p));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
482 ec.op = TOK.TOKblit;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
483 e = Expression.combine(e, ec);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
484 if (dtor)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
485 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
486 /* Instead of running the destructor on s, run it
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
487 * on tmp. This avoids needing to copy tmp back in to s.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
488 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
489 Expression ecc = new DotVarExp(Loc(0), new VarExp(Loc(0), tmp), dtor, 0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
490 ecc = new CallExp(Loc(0), ecc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
491 e = Expression.combine(e, ecc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
492 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
493 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
494 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
495 { /* Do memberwise copy
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
496 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
497 //printf("\tmemberwise copy\n");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
498 for (size_t i = 0; i < fields.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
499 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
500 Dsymbol s = cast(Dsymbol)fields.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
501 VarDeclaration v = s.isVarDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
502 assert(v && v.storage_class & STC.STCfield);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
503 // this.v = s.v;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
504 AssignExp ec = new AssignExp(Loc(0), new DotVarExp(Loc(0), new ThisExp(Loc(0)), v, 0), new DotVarExp(Loc(0), new IdentifierExp(Loc(0), Id.p), v, 0));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
505 ec.op = TOK.TOKblit;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
506 e = Expression.combine(e, ec);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
507 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
508 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
509 Statement s1 = new ExpStatement(Loc(0), e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
510
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
511 /* Add:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
512 * return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
513 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
514 e = new ThisExp(Loc(0));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
515 Statement s2 = new ReturnStatement(Loc(0), e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
516
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
517 fop.fbody = new CompoundStatement(Loc(0), s1, s2);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
518
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
519 members.push(cast(void*)fop);
13
427f8aa74d28 On the road to make Phobos compilable
korDen
parents: 0
diff changeset
520 fop.addMember(sc, this, true);
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
521
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
522 sc = sc.push();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
523 sc.stc = STC.STCundefined;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
524 sc.linkage = LINK.LINKd;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
525
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
526 fop.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
527
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
528 sc.pop();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
529
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
530 //printf("-StructDeclaration.buildOpAssign() %s\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
531
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
532 return fop;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
533 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
534
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
535 /*****************************************
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
536 * Create inclusive postblit for struct by aggregating
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
537 * all the postblits in postblits[] with the postblits for
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
538 * all the members.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
539 * Note the close similarity with AggregateDeclaration.buildDtor(),
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
540 * and the ordering changes (runs forward instead of backwards).
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
541 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
542
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
543 version (DMDV2) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
544 FuncDeclaration buildPostBlit(Scope sc)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
545 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
546 //printf("StructDeclaration.buildPostBlit() %s\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
547 Expression e = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
548
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
549 for (size_t i = 0; i < fields.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
550 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
551 Dsymbol s = cast(Dsymbol)fields.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
552 VarDeclaration v = s.isVarDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
553 assert(v && v.storage_class & STC.STCfield);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
554 if (v.storage_class & STC.STCref)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
555 continue;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
556 Type tv = v.type.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
557 size_t dim = 1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
558 while (tv.ty == TY.Tsarray)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
559 { TypeSArray ta = cast(TypeSArray)tv;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
560 dim *= (cast(TypeSArray)tv).dim.toInteger();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
561 tv = tv.nextOf().toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
562 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
563 if (tv.ty == TY.Tstruct)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
564 { TypeStruct ts = cast(TypeStruct)tv;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
565 StructDeclaration sd = ts.sym;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
566 if (sd.postblit)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
567 { Expression ex;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
568
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
569 // this.v
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
570 ex = new ThisExp(Loc(0));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
571 ex = new DotVarExp(Loc(0), ex, v, 0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
572
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
573 if (dim == 1)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
574 { // this.v.postblit()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
575 ex = new DotVarExp(Loc(0), ex, sd.postblit, 0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
576 ex = new CallExp(Loc(0), ex);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
577 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
578 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
579 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
580 // Typeinfo.postblit(cast(void*)&this.v);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
581 Expression ea = new AddrExp(Loc(0), ex);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
582 ea = new CastExp(Loc(0), ea, Type.tvoid.pointerTo());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
583
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
584 Expression et = v.type.getTypeInfo(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
585 et = new DotIdExp(Loc(0), et, Id.postblit);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
586
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
587 ex = new CallExp(Loc(0), et, ea);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
588 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
589 e = Expression.combine(e, ex); // combine in forward order
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
590 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
591 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
592 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
593
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
594 /* Build our own "postblit" which executes e
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
595 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
596 if (e)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
597 { //printf("Building __fieldPostBlit()\n");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
598 PostBlitDeclaration dd = new PostBlitDeclaration(Loc(0), Loc(0), Lexer.idPool("__fieldPostBlit"));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
599 dd.fbody = new ExpStatement(Loc(0), e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
600 postblits.shift(cast(void*)dd);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
601 members.push(cast(void*)dd);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
602 dd.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
603 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
604
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
605 switch (postblits.dim)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
606 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
607 case 0:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
608 return null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
609
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
610 case 1:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
611 return cast(FuncDeclaration)postblits.data[0];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
612
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
613 default:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
614 e = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
615 for (size_t i = 0; i < postblits.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
616 { FuncDeclaration fd = cast(FuncDeclaration)postblits.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
617 Expression ex = new ThisExp(Loc(0));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
618 ex = new DotVarExp(Loc(0), ex, fd, 0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
619 ex = new CallExp(Loc(0), ex);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
620 e = Expression.combine(e, ex);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
621 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
622 PostBlitDeclaration dd = new PostBlitDeclaration(Loc(0), Loc(0), Lexer.idPool("__aggrPostBlit"));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
623 dd.fbody = new ExpStatement(Loc(0), e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
624 members.push(cast(void*)dd);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
625 dd.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
626 return dd;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
627 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
628 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
629 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
630
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
631 /*******************************************
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
632 * Build copy constructor for struct.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
633 * Copy constructors are compiler generated only, and are only
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
634 * callable from the compiler. They are not user accessible.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
635 * A copy constructor is:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
636 * void cpctpr(ref S s)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
637 * {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
638 * *this = s;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
639 * this.postBlit();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
640 * }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
641 * This is done so:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
642 * - postBlit() never sees uninitialized data
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
643 * - memcpy can be much more efficient than memberwise copy
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
644 * - no fields are overlooked
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
645 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
646 FuncDeclaration buildCpCtor(Scope sc)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
647 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
648 //printf("StructDeclaration.buildCpCtor() %s\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
649 FuncDeclaration fcp = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
650
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
651 /* Copy constructor is only necessary if there is a postblit function,
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
652 * otherwise the code generator will just do a bit copy.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
653 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
654 if (postblit)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
655 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
656 //printf("generating cpctor\n");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
657
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
658 Argument param = new Argument(STC.STCref, type, Id.p, null);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
659 Arguments fparams = new Arguments;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
660 fparams.push(cast(void*)param);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
661 Type ftype = new TypeFunction(fparams, Type.tvoid, false, LINK.LINKd);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
662
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
663 fcp = new FuncDeclaration(Loc(0), Loc(0), Id.cpctor, STC.STCundefined, ftype);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
664
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
665 // Build *this = p;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
666 Expression e = new ThisExp(Loc(0));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
667 version (STRUCTTHISREF) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
668 } else {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
669 e = new PtrExp(Loc(0), e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
670 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
671 AssignExp ea = new AssignExp(Loc(0), e, new IdentifierExp(Loc(0), Id.p));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
672 ea.op = TOK.TOKblit;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
673 Statement s = new ExpStatement(Loc(0), ea);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
674
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
675 // Build postBlit();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
676 e = new VarExp(Loc(0), postblit, 0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
677 e = new CallExp(Loc(0), e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
678
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
679 s = new CompoundStatement(Loc(0), s, new ExpStatement(Loc(0), e));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
680 fcp.fbody = s;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
681
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
682 members.push(cast(void*)fcp);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
683
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
684 sc = sc.push();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
685 sc.stc = STC.STCundefined;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
686 sc.linkage = LINK.LINKd;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
687
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
688 fcp.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
689
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
690 sc.pop();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
691 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
692
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
693 return fcp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
694 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
695
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
696 void toDocBuffer(OutBuffer buf)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
697 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
698 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
699 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
700
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
701 PROT getAccess(Dsymbol smember) // determine access to smember
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
702 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
703 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
704 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
705
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
706 void toObjFile(int multiobj) // compile to .obj file
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
707 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
708 //printf("StructDeclaration.toObjFile('%s')\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
709
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
710 if (multiobj)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
711 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
712 obj_append(this);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
713 return;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
714 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
715
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
716 // Anonymous structs/unions only exist as part of others,
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
717 // do not output forward referenced structs's
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
718 if (!isAnonymous() && members)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
719 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
720 if (global.params.symdebug) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
721 toDebug();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
722 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
723
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
724 type.getTypeInfo(null); // generate TypeInfo
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
725
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
726 if (true)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
727 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
728 // Generate static initializer
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
729 toInitializer();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
730
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
731 static if (false) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
732 sinit.Sclass = SC.SCcomdat;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
733 } else {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
734 if (inTemplateInstance())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
735 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
736 sinit.Sclass = SC.SCcomdat;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
737 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
738 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
739 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
740 sinit.Sclass = SC.SCglobal;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
741 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
742 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
743 sinit.Sfl = FL.FLdata;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
744
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
745 toDt(&sinit.Sdt);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
746
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
747 version (OMFOBJ) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
748 /* For OMF, common blocks aren't pulled in from the library.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
749 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
750 /* ELF comdef's generate multiple
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
751 * definition errors for them from the gnu linker.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
752 * Need to figure out how to generate proper comdef's for ELF.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
753 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
754 // See if we can convert a comdat to a comdef,
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
755 // which saves on exe file space.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
756 if (sinit.Sclass == SCcomdat &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
757 sinit.Sdt &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
758 sinit.Sdt.dt == DT.DT_azeros &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
759 sinit.Sdt.DTnext == null &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
760 !global.params.multiobj)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
761 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
762 sinit.Sclass = SC.SCglobal;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
763 sinit.Sdt.dt = DT.DT_common;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
764 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
765 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
766
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
767 version (ELFOBJ) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
768 sinit.Sseg = Segment.CDATA;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
769 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
770 version (MACHOBJ) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
771 sinit.Sseg = Segment.DATA;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
772 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
773 outdata(sinit);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
774 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
775
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
776 // Put out the members
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
777 for (uint i = 0; i < members.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
778 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
779 Dsymbol member = cast(Dsymbol)members.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
780 member.toObjFile(0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
781 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
782 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
783 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
784
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
785 void toDt(dt_t** pdt)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
786 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
787 uint offset;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
788 uint i;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
789 dt_t* dt;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
790
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
791 //printf("StructDeclaration.toDt(), this='%s'\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
792 offset = 0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
793
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
794 // Note equivalence of this loop to class's
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
795 for (i = 0; i < fields.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
796 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
797 VarDeclaration v = cast(VarDeclaration)fields.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
798 //printf("\tfield '%s' voffset %d, offset = %d\n", v.toChars(), v.offset, offset);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
799 dt = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
800 int sz;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
801
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
802 if (v.storage_class & STC.STCref)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
803 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
804 sz = PTRSIZE;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
805 if (v.offset >= offset)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
806 dtnzeros(&dt, sz);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
807 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
808 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
809 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
810 sz = cast(uint)v.type.size();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
811 Initializer init = v.init;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
812 if (init)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
813 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
814 //printf("\t\thas initializer %s\n", init.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
815 ExpInitializer ei = init.isExpInitializer();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
816 Type tb = v.type.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
817 if (ei && tb.ty == TY.Tsarray)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
818 (cast(TypeSArray)tb).toDtElem(&dt, ei.exp);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
819 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
820 dt = init.toDt();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
821 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
822 else if (v.offset >= offset)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
823 v.type.toDt(&dt);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
824 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
825 if (dt)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
826 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
827 if (v.offset < offset)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
828 error("overlapping initialization for struct %s.%s", toChars(), v.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
829 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
830 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
831 if (offset < v.offset)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
832 dtnzeros(pdt, v.offset - offset);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
833 dtcat(pdt, dt);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
834 offset = v.offset + sz;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
835 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
836 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
837 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
838
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
839 if (offset < structsize)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
840 dtnzeros(pdt, structsize - offset);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
841
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
842 dt_optimize(*pdt);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
843 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
844
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
845 void toDebug() // to symbolic debug info
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
846 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
847 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
848 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
849
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
850 StructDeclaration isStructDeclaration() { return this; }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
851 }