annotate dmd/AggregateDeclaration.d @ 96:acd69f84627e

further work
author Trass3r
date Tue, 31 Aug 2010 02:12:15 +0200
parents 37b95c347975
children e28b18c23469
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1 module dmd.AggregateDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
2
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
3 import dmd.ScopeDsymbol;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
4 import dmd.Type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
5 import dmd.Id;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
6 import dmd.ExpStatement;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
7 import dmd.AddrExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
8 import dmd.CastExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
9 import dmd.TypeSArray;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
10 import dmd.DotVarExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
11 import dmd.TypeStruct;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
12 import dmd.StructDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
13 import dmd.Declaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
14 import dmd.TypeClass;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
15 import dmd.TOK;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
16 import dmd.ThisExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
17 import dmd.PROT;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
18 import dmd.Expression;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
19 import dmd.STC;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
20 import dmd.DotIdExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
21 import dmd.CallExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
22 import dmd.DtorDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
23 import dmd.Lexer;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
24 import dmd.TY;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
25 import dmd.Array;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
26 import dmd.ArrayTypes;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
27 import dmd.VarDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
28 import dmd.InvariantDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
29 import dmd.NewDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
30 import dmd.DeleteDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
31 import dmd.CtorDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
32 import dmd.FuncDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
33 import dmd.Identifier;
79
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
34 import dmd.Json;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
35 import dmd.Loc;
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.ClassDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
40 import dmd.BaseClass;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
41 import dmd.Util;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
42
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
43 import dmd.backend.Symbol;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
44 import dmd.backend.Classsym;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
45 import dmd.backend.Util;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
46 import dmd.backend.LIST;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
47 import dmd.backend.SC;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
48 import dmd.backend.FL;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
49 import dmd.backend.SFL;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
50 import dmd.codegen.Util;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
51
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
52 /****************************************
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
53 * Determine if scope sc has package level access to s.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
54 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
55
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
56 bool hasPackageAccess(Scope sc, Dsymbol s)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
57 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
58 version (LOG) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
59 printf("hasPackageAccess(s = '%s', sc = '%p')\n", s.toChars(), sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
60 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
61
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
62 for (; s; s = s.parent)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
63 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
64 if (s.isPackage() && !s.isModule())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
65 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
66 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
67 version (LOG) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
68 if (s)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
69 printf("\tthis is in package '%s'\n", s.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
70 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
71
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
72 if (s && s == sc.module_.parent)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
73 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
74 version (LOG) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
75 printf("\ts is in same package as sc\n");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
76 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
77 return true;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
78 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
79
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
80
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
81 version (LOG) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
82 printf("\tno package access\n");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
83 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
84
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
85 return false;
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 * Helper function for ClassDeclaration.accessCheck()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
90 * Returns:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
91 * 0 no access
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
92 * 1 access
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
93 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
94
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
95 bool accessCheckX(Dsymbol smember, Dsymbol sfunc, AggregateDeclaration dthis, AggregateDeclaration cdscope)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
96 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
97 assert(dthis);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
98
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
99 static if (false) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
100 writef("accessCheckX for %s.%s in function %s() in scope %s\n", dthis.toChars(), smember.toChars(), sfunc ? sfunc.toChars() : "null", cdscope ? cdscope.toChars() : "null");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
101 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
102 if (dthis.hasPrivateAccess(sfunc) || dthis.isFriendOf(cdscope))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
103 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
104 if (smember.toParent() == dthis)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
105 return true;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
106 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
107 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
108 ClassDeclaration cdthis = dthis.isClassDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
109 if (cdthis)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
110 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
111 for (int i = 0; i < cdthis.baseclasses.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
112 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
113 BaseClass b = cast(BaseClass)cdthis.baseclasses.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
114 PROT access = b.base.getAccess(smember);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
115
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
116 if (access >= PROT.PROTprotected || accessCheckX(smember, sfunc, b.base, cdscope))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
117 return true;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
118 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
119 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
120 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
121 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
122 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
123 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
124 if (smember.toParent() != dthis)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
125 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
126 ClassDeclaration cdthis = dthis.isClassDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
127 if (cdthis)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
128 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
129 for (int i = 0; i < cdthis.baseclasses.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
130 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
131 BaseClass b = cast(BaseClass)cdthis.baseclasses.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
132
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
133 if (accessCheckX(smember, sfunc, b.base, cdscope))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
134 return true;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
135 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
136 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
137 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
138 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
139
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
140 return 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 class AggregateDeclaration : ScopeDsymbol
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
144 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
145 Type type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
146 uint storage_class;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
147 PROT protection = PROT.PROTpublic;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
148 Type handle; // 'this' type
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
149 uint structsize; // size of struct
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
150 uint alignsize; // size of struct for alignment purposes
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
151 uint structalign; // struct member alignment in effect
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
152 int hasUnions; // set if aggregate has overlapping fields
84
be2ab491772e Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 79
diff changeset
153 VarDeclarations fields; // VarDeclaration fields
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
154 uint sizeok; // set when structsize contains valid data
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
155 // 0: no size
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
156 // 1: size is correct
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
157 // 2: cannot determine size; fwd referenced
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
158 bool isdeprecated; // true if deprecated
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
159
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
160 bool isnested; // true if is nested
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
161 VarDeclaration vthis; // 'this' parameter if this aggregate is nested
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
162
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
163 // Special member functions
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
164 InvariantDeclaration inv; // invariant
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
165 NewDeclaration aggNew; // allocator
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
166 DeleteDeclaration aggDelete; // deallocator
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
167
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
168 version (DMDV2) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
169 //CtorDeclaration *ctor;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
170 Dsymbol ctor; // CtorDeclaration or TemplateDeclaration
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
171 CtorDeclaration defaultCtor; // default constructor
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
172 Dsymbol aliasthis; // forward unresolved lookups to aliasthis
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
173 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
174
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
175 FuncDeclarations dtors; // Array of destructors
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
176 FuncDeclaration dtor; // aggregate destructor
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
177
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
178 version (IN_GCC) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
179 Array methods; // flat list of all methods for debug information
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
180 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
181
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
182 this(Loc loc, Identifier id)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
183 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
184 super(id);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
185 this.loc = loc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
186
84
be2ab491772e Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 79
diff changeset
187 fields = new VarDeclarations(); ///
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
188 dtors = new FuncDeclarations();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
189 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
190
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 16
diff changeset
191 override void semantic2(Scope sc)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
192 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
193 //printf("AggregateDeclaration.semantic2(%s)\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
194 if (scope_ && members)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
195 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
196 error("has forward references");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
197 return;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
198 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
199 if (members)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
200 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
201 sc = sc.push(this);
77
ad4792a1cfd6 more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 74
diff changeset
202 foreach(Dsymbol s; members)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
203 s.semantic2(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
204 sc.pop();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
205 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
206 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
207
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 16
diff changeset
208 override void semantic3(Scope sc)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
209 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
210 //printf("AggregateDeclaration.semantic3(%s)\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
211 if (members)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
212 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
213 sc = sc.push(this);
77
ad4792a1cfd6 more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 74
diff changeset
214 foreach(Dsymbol s; members)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
215 s.semantic3(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
216 sc.pop();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
217 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
218 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
219
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 16
diff changeset
220 override void inlineScan()
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
221 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
222 //printf("AggregateDeclaration.inlineScan(%s)\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
223 if (members)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
224 {
77
ad4792a1cfd6 more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 74
diff changeset
225 foreach(Dsymbol s; members)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
226 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
227 //printf("inline scan aggregate symbol '%s'\n", s.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
228 s.inlineScan();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
229 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
230 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
231 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
232
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 16
diff changeset
233 override uint size(Loc loc)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
234 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
235 //printf("AggregateDeclaration.size() = %d\n", structsize);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
236 if (!members)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
237 error(loc, "unknown size");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
238
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
239 if (sizeok != 1)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
240 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
241 error(loc, "no size yet for forward reference");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
242 //*(char*)0=0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
243 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
244
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
245 return structsize;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
246 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
247
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
248 /****************************
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
249 * Do byte or word alignment as necessary.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
250 * Align sizes of 0, as we may not know array sizes yet.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
251 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
252 static void alignmember(uint salign, uint size, uint* poffset)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
253 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
254 //printf("salign = %d, size = %d, offset = %d\n",salign,size,offset);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
255 if (salign > 1)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
256 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
257 assert(size != 3);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
258 int sa = size;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
259 if (sa == 0 || salign < sa)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
260 sa = salign;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
261 *poffset = (*poffset + sa - 1) & ~(sa - 1);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
262 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
263 //printf("result = %d\n",offset);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
264 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
265
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 16
diff changeset
266 override Type getType()
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
267 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
268 return type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
269 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
270
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
271 void addField(Scope sc, VarDeclaration v)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
272 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
273 uint memsize; // size of member
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
274 uint memalignsize; // size of member for alignment purposes
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
275 uint xalign; // alignment boundaries
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
276
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
277 //printf("AggregateDeclaration.addField('%s') %s\n", v.toChars(), toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
278 assert(!(v.storage_class & (STC.STCstatic | STC.STCextern | STC.STCparameter | STC.STCtls)));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
279
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
280 // Check for forward referenced types which will fail the size() call
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
281 Type t = v.type.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
282 if (v.storage_class & STC.STCref)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
283 { // References are the size of a pointer
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
284 t = Type.tvoidptr;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
285 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
286 if (t.ty == TY.Tstruct /*&& isStructDeclaration()*/)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
287 { TypeStruct ts = cast(TypeStruct)t;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
288 version (DMDV2) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
289 if (ts.sym == this)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
290 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
291 error("cannot have field %s with same struct type", v.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
292 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
293 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
294
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
295 if (ts.sym.sizeok != 1)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
296 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
297 sizeok = 2; // cannot finish; flag as forward referenced
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 if (t.ty == TY.Tident)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
302 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
303 sizeok = 2; // cannot finish; flag as forward referenced
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
304 return;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
305 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
306
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
307 memsize = cast(uint)t.size(loc); ///
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
308 memalignsize = t.alignsize();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
309 xalign = t.memalign(sc.structalign);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
310 alignmember(xalign, memalignsize, &sc.offset);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
311 v.offset = sc.offset;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
312 sc.offset += memsize;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
313 if (sc.offset > structsize)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
314 structsize = sc.offset;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
315 if (sc.structalign < memalignsize)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
316 memalignsize = sc.structalign;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
317 if (alignsize < memalignsize)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
318 alignsize = memalignsize;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
319 //printf("\talignsize = %d\n", alignsize);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
320
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
321 v.storage_class |= STC.STCfield;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
322 //printf(" addField '%s' to '%s' at offset %d, size = %d\n", v.toChars(), toChars(), v.offset, memsize);
79
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
323 fields.push(v);
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
324 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
325
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 16
diff changeset
326 override bool isDeprecated() // is aggregate deprecated?
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
327 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
328 return isdeprecated;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
329 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
330
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
331 /*****************************************
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
332 * Create inclusive destructor for struct/class by aggregating
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
333 * all the destructors in dtors[] with the destructors for
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
334 * all the members.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
335 * Note the close similarity with StructDeclaration.buildPostBlit(),
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
336 * and the ordering changes (runs backward instead of forwards).
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
337 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
338 FuncDeclaration buildDtor(Scope sc)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
339 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
340 //printf("AggregateDeclaration.buildDtor() %s\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
341 Expression e = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
342
79
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
343 version (DMDV2)
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
344 {
84
be2ab491772e Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 79
diff changeset
345 foreach (size_t i, VarDeclaration v; fields)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
346 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
347 assert(v && v.storage_class & STC.STCfield);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
348 if (v.storage_class & STC.STCref)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
349 continue;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
350 Type tv = v.type.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
351 size_t dim = 1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
352 while (tv.ty == TY.Tsarray)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
353 { TypeSArray ta = cast(TypeSArray)tv;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
354 dim *= (cast(TypeSArray)tv).dim.toInteger();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
355 tv = tv.nextOf().toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
356 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
357 if (tv.ty == TY.Tstruct)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
358 { TypeStruct ts = cast(TypeStruct)tv;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
359 StructDeclaration sd = ts.sym;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
360 if (sd.dtor)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
361 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
362 Expression ex;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
363
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
364 // this.v
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
365 ex = new ThisExp(Loc(0));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
366 ex = new DotVarExp(Loc(0), ex, v, 0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
367
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
368 if (dim == 1)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
369 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
370 // this.v.dtor()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
371 ex = new DotVarExp(Loc(0), ex, sd.dtor, 0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
372 ex = new CallExp(Loc(0), ex);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
373 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
374 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
375 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
376 // Typeinfo.destroy(cast(void*)&this.v);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
377 Expression ea = new AddrExp(Loc(0), ex);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
378 ea = new CastExp(Loc(0), ea, Type.tvoid.pointerTo());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
379
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
380 Expression et = v.type.getTypeInfo(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
381 et = new DotIdExp(Loc(0), et, Id.destroy);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
382
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
383 ex = new CallExp(Loc(0), et, ea);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
384 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
385 e = Expression.combine(ex, e); // combine in reverse order
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
386 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
387 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
388 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
389
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
390 /* Build our own "destructor" which executes e
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
391 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
392 if (e)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
393 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
394 //printf("Building __fieldDtor()\n");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
395 DtorDeclaration dd = new DtorDeclaration(Loc(0), Loc(0), Lexer.idPool("__fieldDtor"));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
396 dd.fbody = new ExpStatement(Loc(0), e);
74
7e0d548de9e6 Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 72
diff changeset
397 dtors.shift(dd);
7e0d548de9e6 Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 72
diff changeset
398 members.push(dd);
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
399 dd.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
400 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
401 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
402
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
403 switch (dtors.dim)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
404 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
405 case 0:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
406 return null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
407
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
408 case 1:
77
ad4792a1cfd6 more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 74
diff changeset
409 return cast(FuncDeclaration)dtors[0];
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
410
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
411 default:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
412 e = null;
77
ad4792a1cfd6 more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 74
diff changeset
413 foreach(FuncDeclaration fd; dtors)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
414 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
415 Expression ex = new ThisExp(Loc(0));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
416 ex = new DotVarExp(Loc(0), ex, fd, 0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
417 ex = new CallExp(Loc(0), ex);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
418 e = Expression.combine(ex, e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
419 }
77
ad4792a1cfd6 more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 74
diff changeset
420 auto dd = new DtorDeclaration(Loc(0), Loc(0), Lexer.idPool("__aggrDtor"));
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
421 dd.fbody = new ExpStatement(Loc(0), e);
74
7e0d548de9e6 Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 72
diff changeset
422 members.push(dd);
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
423 dd.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
424 return dd;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
425 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
426 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
427
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
428 /****************************************
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
429 * Returns true if there's an extra member which is the 'this'
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
430 * pointer to the enclosing context (enclosing aggregate or function)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
431 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
432 bool isNested()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
433 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
434 return isnested;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
435 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
436
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 16
diff changeset
437 override void emitComment(Scope sc)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
438 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
439 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
440 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
441
79
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
442 override void toJsonBuffer(OutBuffer buf)
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
443 {
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
444 //writef("AggregateDeclaration.toJsonBuffer()\n");
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
445 buf.writestring("{\n");
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
446
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
447 JsonProperty(buf, Pname, toChars());
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
448 JsonProperty(buf, Pkind, kind());
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
449 if (comment)
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
450 JsonProperty(buf, Pcomment, comment);
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
451 if (loc.linnum)
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
452 JsonProperty(buf, Pline, loc.linnum);
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
453
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
454 ClassDeclaration cd = isClassDeclaration();
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
455 if (cd)
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
456 {
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
457 if (cd.baseClass)
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
458 {
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
459 JsonProperty(buf, "base", cd.baseClass.toChars());
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
460 }
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
461 if (cd.interfaces_dim)
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
462 {
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
463 JsonString(buf, "interfaces");
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
464 buf.writestring(" : [\n");
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
465 size_t offset = buf.offset;
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
466 for (int i = 0; i < cd.interfaces_dim; i++)
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
467 {
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
468 BaseClass b = cd.interfaces[i];
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
469 if (offset != buf.offset)
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
470 {
96
acd69f84627e further work
Trass3r
parents: 89
diff changeset
471 buf.writestring(",\n");
79
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
472 offset = buf.offset;
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
473 }
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
474 JsonString(buf, b.base.toChars());
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
475 }
96
acd69f84627e further work
Trass3r
parents: 89
diff changeset
476 JsonRemoveComma(buf);
79
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
477 buf.writestring("],\n");
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
478 }
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
479 }
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
480
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
481 JsonString(buf, Pmembers);
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
482 buf.writestring(" : [\n");
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
483 size_t offset = buf.offset;
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
484 foreach (Dsymbol s; members)
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
485 {
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
486 if (offset != buf.offset)
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
487 {
96
acd69f84627e further work
Trass3r
parents: 89
diff changeset
488 buf.writestring(",\n");
79
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
489 offset = buf.offset;
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
490 }
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
491 s.toJsonBuffer(buf);
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
492 }
96
acd69f84627e further work
Trass3r
parents: 89
diff changeset
493 JsonRemoveComma(buf);
79
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
494 buf.writestring("]\n");
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
495
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
496 buf.writestring("}\n");
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
497 }
43073c7c7769 updated to 2.035
Trass3r
parents: 77
diff changeset
498
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 16
diff changeset
499 override void toDocBuffer(OutBuffer buf)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
500 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
501 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
502 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
503
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
504 // For access checking
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
505 PROT getAccess(Dsymbol smember) // determine access to smember
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
506 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
507 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
508 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
509
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
510 /****************************************
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
511 * Determine if this is the same or friend of cd.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
512 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
513 bool isFriendOf(AggregateDeclaration cd)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
514 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
515 version (LOG) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
516 printf("AggregateDeclaration.isFriendOf(this = '%s', cd = '%s')\n", toChars(), cd ? cd.toChars() : "null");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
517 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
518 if (this is cd)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
519 return true;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
520
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
521 // Friends if both are in the same module
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
522 //if (toParent() == cd.toParent())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
523 if (cd && getModule() == cd.getModule())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
524 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
525 version (LOG) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
526 printf("\tin same module\n");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
527 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
528 return true;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
529 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
530
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
531 version (LOG) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
532 printf("\tnot friend\n");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
533 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
534 return false;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
535 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
536
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
537 /**********************************
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
538 * Determine if smember has access to private members of this declaration.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
539 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
540 bool hasPrivateAccess(Dsymbol smember) // does smember have private access to members of this class?
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
541 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
542 if (smember)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
543 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
544 AggregateDeclaration cd = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
545 Dsymbol smemberparent = smember.toParent();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
546 if (smemberparent)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
547 cd = smemberparent.isAggregateDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
548
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
549 version (LOG) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
550 printf("AggregateDeclaration::hasPrivateAccess(class %s, member %s)\n", toChars(), smember.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
551 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
552
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
553 if (this == cd) // smember is a member of this class
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
554 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
555 version (LOG) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
556 printf("\tyes 1\n");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
557 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
558 return true; // so we get private access
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
559 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
560
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
561 // If both are members of the same module, grant access
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
562 while (true)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
563 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
564 Dsymbol sp = smember.toParent();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
565 if (sp.isFuncDeclaration() && smember.isFuncDeclaration())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
566 smember = sp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
567 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
568 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
569 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
570 if (!cd && toParent() == smember.toParent())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
571 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
572 version (LOG) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
573 printf("\tyes 2\n");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
574 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
575 return true;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
576 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
577 if (!cd && getModule() == smember.getModule())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
578 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
579 version (LOG) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
580 printf("\tyes 3\n");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
581 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
582 return true;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
583 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
584 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
585 version (LOG) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
586 printf("\tno\n");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
587 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
588 return false;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
589 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
590
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
591 /*******************************
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
592 * Do access check for member of this class, this class being the
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
593 * type of the 'this' pointer used to access smember.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
594 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
595 void accessCheck(Loc loc, Scope sc, Dsymbol smember)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
596 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
597 bool result;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
598
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
599 FuncDeclaration f = sc.func;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
600 AggregateDeclaration cdscope = sc.getStructClassScope();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
601 PROT access;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
602
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
603 version (LOG) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
604 printf("AggregateDeclaration.accessCheck() for %s.%s in function %s() in scope %s\n", toChars(), smember.toChars(), f ? f.toChars() : null, cdscope ? cdscope.toChars() : null);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
605 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
606
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
607 Dsymbol smemberparent = smember.toParent();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
608 if (!smemberparent || !smemberparent.isAggregateDeclaration())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
609 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
610 version (LOG) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
611 printf("not an aggregate member\n");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
612 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
613 return; // then it is accessible
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
614 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
615
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
616 // BUG: should enable this check
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
617 //assert(smember.parent.isBaseOf(this, null));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
618
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
619 if (smemberparent == this)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
620 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
621 PROT access2 = smember.prot();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
622
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
623 result = access2 >= PROT.PROTpublic ||
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
624 hasPrivateAccess(f) ||
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
625 isFriendOf(cdscope) ||
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
626 (access2 == PROT.PROTpackage && hasPackageAccess(sc, this));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
627
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
628 version (LOG) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
629 printf("result1 = %d\n", result);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
630 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
631 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
632 else if ((access = this.getAccess(smember)) >= PROT.PROTpublic)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
633 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
634 result = true;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
635 version (LOG) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
636 printf("result2 = %d\n", result);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
637 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
638 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
639 else if (access == PROT.PROTpackage && hasPackageAccess(sc, this))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
640 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
641 result = true;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
642 version (LOG) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
643 printf("result3 = %d\n", result);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
644 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
645 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
646 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
647 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
648 result = accessCheckX(smember, f, this, cdscope);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
649 version (LOG) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
650 printf("result4 = %d\n", result);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
651 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
652 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
653 if (!result)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
654 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
655 error(loc, "member %s is not accessible", smember.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
656 halt();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
657 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
658 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
659
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 16
diff changeset
660 override PROT prot()
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
661 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
662 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
663 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
664
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
665 // Back end
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
666 Symbol* stag; // tag symbol for debug data
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
667 Symbol* sinit;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
668
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
669 Symbol* toInitializer()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
670 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
671 Symbol* s;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
672 Classsym* stag;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
673
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
674 if (!sinit)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
675 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
676 stag = fake_classsym(Id.ClassInfo);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
677 s = toSymbolX("__init", SC.SCextern, stag.Stype, "Z");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
678 s.Sfl = FL.FLextern;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
679 s.Sflags |= SFL.SFLnodebug;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
680 slist_add(s);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
681 sinit = s;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
682 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
683
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
684 return sinit;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
685 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
686
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 16
diff changeset
687 override AggregateDeclaration isAggregateDeclaration() { return this; }
16
5c9b78899f5d Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
688 }