annotate dmd/AggregateDeclaration.d @ 0:10317f0c89a5

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