annotate dmd/AnonDeclaration.d @ 53:a8b50ff7f201

ForeachStatement.syntaxCopy SliceExp.syntaxCopy AnonDeclaration.syntaxCopy SwitchStatement.syntaxCopy CaseStatement.syntaxCopy BreakStatement.syntaxCopy ThrowStatement.syntaxCopy NewExp.syntaxCopy DefaultStatement.syntaxCopy AssertExp.syntaxCopy ClassDeclaration.syntaxCopy TypeTypedef.constConv eval_builtin ComplexExp.isConst DVCondition.syntaxCopy OrExp.getIntRange AndExp.getIntRange getMask IntegerExp.getIntRange Type.sizemask CastExp.getIntRange Expression.getIntRange
author korDen
date Sat, 21 Aug 2010 12:15:47 +0400
parents 10317f0c89a5
children 2e2a5c3f943a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1 module dmd.AnonDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
2
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
3 import dmd.OutBuffer;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
4 import dmd.Loc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
5 import dmd.Scope;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
6 import dmd.Array;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
7 import dmd.AttribDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
8 import dmd.HdrGenState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
9 import dmd.Dsymbol;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
10 import dmd.AggregateDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
11 import dmd.AnonymousAggregateDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
12 import dmd.STC;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
13 import dmd.Module;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
14 import dmd.VarDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
15
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
16 class AnonDeclaration : AttribDeclaration
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
17 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
18 int isunion;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
19 int sem = 0; // 1 if successful semantic()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
20
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
21 this(Loc loc, int isunion, Array decl)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
22 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
23 super(decl);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
24 this.loc = loc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
25 this.isunion = isunion;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
26 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
27
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
28 Dsymbol syntaxCopy(Dsymbol s)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
29 {
53
a8b50ff7f201 ForeachStatement.syntaxCopy
korDen
parents: 0
diff changeset
30 AnonDeclaration ad;
a8b50ff7f201 ForeachStatement.syntaxCopy
korDen
parents: 0
diff changeset
31
a8b50ff7f201 ForeachStatement.syntaxCopy
korDen
parents: 0
diff changeset
32 assert(!s);
a8b50ff7f201 ForeachStatement.syntaxCopy
korDen
parents: 0
diff changeset
33 ad = new AnonDeclaration(loc, isunion, Dsymbol.arraySyntaxCopy(decl));
a8b50ff7f201 ForeachStatement.syntaxCopy
korDen
parents: 0
diff changeset
34 return ad;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
35 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
36
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
37 void semantic(Scope sc)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
38 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
39 //printf("\tAnonDeclaration::semantic %s %p\n", isunion ? "union" : "struct", this);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
40
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
41 Scope scx = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
42 if (scope_)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
43 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
44 sc = scope_;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
45 scx = scope_;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
46 scope_ = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
47 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
48
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
49 assert(sc.parent);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
50
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
51 Dsymbol parent = sc.parent.pastMixin();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
52 AggregateDeclaration ad = parent.isAggregateDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
53
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
54 if (!ad || (!ad.isStructDeclaration() && !ad.isClassDeclaration()))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
55 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
56 error("can only be a part of an aggregate");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
57 return;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
58 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
59
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
60 if (decl)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
61 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
62 AnonymousAggregateDeclaration aad = new AnonymousAggregateDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
63 int adisunion;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
64
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
65 if (sc.anonAgg)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
66 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
67 ad = sc.anonAgg;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
68 adisunion = sc.inunion;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
69 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
70 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
71 adisunion = ad.isUnionDeclaration() !is null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
72
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
73 // printf("\tsc.anonAgg = %p\n", sc.anonAgg);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
74 // printf("\tad = %p\n", ad);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
75 // printf("\taad = %p\n", &aad);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
76
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
77 sc = sc.push();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
78 sc.anonAgg = aad;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
79 sc.stc &= ~(STCauto | STCscope | STCstatic | STCtls | STCgshared);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
80 sc.inunion = isunion;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
81 sc.offset = 0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
82 sc.flags = cast(SCOPE)0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
83 aad.structalign = sc.structalign;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
84 aad.parent = ad;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
85
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
86 for (uint i = 0; i < decl.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
87 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
88 Dsymbol s = cast(Dsymbol)decl.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
89
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
90 s.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
91 if (isunion)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
92 sc.offset = 0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
93 if (aad.sizeok == 2)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
94 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
95 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
96 sc = sc.pop();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
97
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
98 // If failed due to forward references, unwind and try again later
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
99 if (aad.sizeok == 2)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
100 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
101 ad.sizeok = 2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
102 //printf("\tsetting ad.sizeok %p to 2\n", ad);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
103 if (!sc.anonAgg)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
104 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
105 scope_ = scx ? scx : new Scope(sc); ///<
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
106 scope_.setNoFree();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
107 scope_.module_.addDeferredSemantic(this);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
108 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
109 //printf("\tforward reference %p\n", this);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
110 return;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
111 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
112 if (sem == 0)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
113 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
114 Module.dprogress++;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
115 sem = 1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
116 //printf("\tcompleted %p\n", this);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
117 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
118 else {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
119 ;//printf("\talready completed %p\n", this);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
120 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
121
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
122 // 0 sized structs are set to 1 byte
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
123 if (aad.structsize == 0)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
124 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
125 aad.structsize = 1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
126 aad.alignsize = 1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
127 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
128
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
129 // Align size of anonymous aggregate
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
130 //printf("aad.structalign = %d, aad.alignsize = %d, sc.offset = %d\n", aad.structalign, aad.alignsize, sc.offset);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
131 ad.alignmember(aad.structalign, aad.alignsize, &sc.offset);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
132 //ad.structsize = sc.offset;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
133 //printf("sc.offset = %d\n", sc.offset);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
134
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
135 // Add members of aad to ad
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
136 //printf("\tadding members of aad to '%s'\n", ad.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
137 for (uint i = 0; i < aad.fields.dim; i++)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
138 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
139 VarDeclaration v = cast(VarDeclaration)aad.fields.data[i];
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
140
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
141 v.offset += sc.offset;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
142 ad.fields.push(cast(void*)v);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
143 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
144
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
145 // Add size of aad to ad
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
146 if (adisunion)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
147 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
148 if (aad.structsize > ad.structsize)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
149 ad.structsize = aad.structsize;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
150 sc.offset = 0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
151 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
152 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
153 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
154 ad.structsize = sc.offset + aad.structsize;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
155 sc.offset = ad.structsize;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
156 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
157
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
158 if (ad.alignsize < aad.alignsize)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
159 ad.alignsize = aad.alignsize;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
160 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
161 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
162
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
163 void toCBuffer(OutBuffer buf, HdrGenState* hgs)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
164 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
165 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
166 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
167
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
168 string kind()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
169 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
170 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
171 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
172 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
173