annotate dmd/ArrayScopeSymbol.d @ 187:b0d41ff5e0df

Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
author Abscissa
date Tue, 07 Jun 2011 23:37:34 -0400
parents e3afd1303184
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1 module dmd.ArrayScopeSymbol;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
2
114
e28b18c23469 added a module dmd.common for commonly used stuff
Trass3r
parents: 72
diff changeset
3 import dmd.common;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
4 import dmd.ScopeDsymbol;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
5 import dmd.Expression;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
6 import dmd.TypeTuple;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
7 import dmd.TupleDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
8 import dmd.Scope;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
9 import dmd.Dsymbol;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
10 import dmd.Loc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
11 import dmd.TOK;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
12 import dmd.Identifier;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
13 import dmd.Id;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
14 import dmd.TY;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
15 import dmd.TupleExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
16 import dmd.StringExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
17 import dmd.WANT;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
18 import dmd.TypeExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
19 import dmd.Type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
20 import dmd.SliceExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
21 import dmd.IndexExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
22 import dmd.IntegerExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
23 import dmd.STC;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
24 import dmd.ExpInitializer;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
25 import dmd.VarDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
26 import dmd.ArrayLiteralExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
27 import dmd.expression.Util;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
28
187
b0d41ff5e0df Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents: 178
diff changeset
29 import dmd.DDMDExtensions;
b0d41ff5e0df Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents: 178
diff changeset
30
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
31 class ArrayScopeSymbol : ScopeDsymbol
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
32 {
187
b0d41ff5e0df Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents: 178
diff changeset
33 mixin insertMemberExtension!(typeof(this));
b0d41ff5e0df Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents: 178
diff changeset
34
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
35 Expression exp; // IndexExp or SliceExp
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
36 TypeTuple type; // for tuple[length]
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
37 TupleDeclaration td; // for tuples of objects
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
38 Scope sc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
39
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
40 this(Scope sc, Expression e)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
41 {
178
e3afd1303184 Many small bugs fixed
korDen
parents: 114
diff changeset
42 register();
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
43 super();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
44 assert(e.op == TOKindex || e.op == TOKslice);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
45 this.exp = e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
46 this.sc = sc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
47 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
48
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
49 this(Scope sc, TypeTuple t)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
50 {
178
e3afd1303184 Many small bugs fixed
korDen
parents: 114
diff changeset
51 register();
49
0aa7d1437ada AttribDeclaration.oneMember
korDen
parents: 38
diff changeset
52 exp = null;
0aa7d1437ada AttribDeclaration.oneMember
korDen
parents: 38
diff changeset
53 type = t;
0aa7d1437ada AttribDeclaration.oneMember
korDen
parents: 38
diff changeset
54 td = null;
0aa7d1437ada AttribDeclaration.oneMember
korDen
parents: 38
diff changeset
55 this.sc = sc;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
56 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
57
38
4caad35a6ceb ArrayScopeSymbol ctor implemented
korDen
parents: 0
diff changeset
58 this(Scope sc, TupleDeclaration s)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
59 {
178
e3afd1303184 Many small bugs fixed
korDen
parents: 114
diff changeset
60 register();
e3afd1303184 Many small bugs fixed
korDen
parents: 114
diff changeset
61
38
4caad35a6ceb ArrayScopeSymbol ctor implemented
korDen
parents: 0
diff changeset
62 exp = null;
4caad35a6ceb ArrayScopeSymbol ctor implemented
korDen
parents: 0
diff changeset
63 type = null;
4caad35a6ceb ArrayScopeSymbol ctor implemented
korDen
parents: 0
diff changeset
64 td = s;
4caad35a6ceb ArrayScopeSymbol ctor implemented
korDen
parents: 0
diff changeset
65 this.sc = sc;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
66 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
67
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 49
diff changeset
68 override Dsymbol search(Loc loc, Identifier ident, int flags)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
69 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
70 //printf("ArrayScopeSymbol.search('%s', flags = %d)\n", ident.toChars(), flags);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
71 if (ident == Id.length || ident == Id.dollar)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
72 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
73 VarDeclaration* pvar;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
74 Expression ce;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
75
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
76 L1:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
77 if (td)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
78 { /* $ gives the number of elements in the tuple
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
79 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
80 VarDeclaration v = new VarDeclaration(loc, Type.tsize_t, Id.dollar, null);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
81 Expression e = new IntegerExp(Loc(0), td.objects.dim, Type.tsize_t);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
82 v.init = new ExpInitializer(Loc(0), e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
83 v.storage_class |= STCstatic | STCconst;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
84 v.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
85 return v;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
86 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
87
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
88 if (type)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
89 { /* $ gives the number of type entries in the type tuple
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
90 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
91 VarDeclaration v = new VarDeclaration(loc, Type.tsize_t, Id.dollar, null);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
92 Expression e = new IntegerExp(Loc(0), type.arguments.dim, Type.tsize_t);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
93 v.init = new ExpInitializer(Loc(0), e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
94 v.storage_class |= STCstatic | STCconst;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
95 v.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
96 return v;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
97 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
98
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
99 if (exp.op == TOKindex)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
100 { /* array[index] where index is some function of $
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
101 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
102 IndexExp ie = cast(IndexExp)exp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
103
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
104 pvar = &ie.lengthVar;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
105 ce = ie.e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
106 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
107 else if (exp.op == TOKslice)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
108 { /* array[lwr .. upr] where lwr or upr is some function of $
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
109 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
110 SliceExp se = cast(SliceExp)exp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
111
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
112 pvar = &se.lengthVar;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
113 ce = se.e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
114 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
115 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
116 /* Didn't find $, look in enclosing scope(s).
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
117 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
118 return null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
119
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
120 /* If we are indexing into an array that is really a type
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
121 * tuple, rewrite this as an index into a type tuple and
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
122 * try again.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
123 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
124 if (ce.op == TOKtype)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
125 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
126 Type t = (cast(TypeExp)ce).type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
127 if (t.ty == Ttuple)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
128 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
129 type = cast(TypeTuple)t;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
130 goto L1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
131 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
132 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
133
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
134 /* *pvar is lazily initialized, so if we refer to $
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
135 * multiple times, it gets set only once.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
136 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
137 if (!*pvar) // if not already initialized
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
138 { /* Create variable v and set it to the value of $,
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
139 * which will be a constant.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
140 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
141 VarDeclaration v = new VarDeclaration(loc, Type.tsize_t, Id.dollar, null);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
142
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
143 if (ce.op == TOKvar)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
144 { // if ce is const, get its initializer
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
145 ce = fromConstInitializer(WANTvalue | WANTinterpret, ce);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
146 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
147
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
148 if (ce.op == TOKstring)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
149 { /* It is for a string literal, so the
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
150 * length will be a const.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
151 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
152 Expression e = new IntegerExp(Loc(0), (cast(StringExp)ce).len, Type.tsize_t);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
153 v.init = new ExpInitializer(Loc(0), e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
154 v.storage_class |= STCstatic | STCconst;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
155 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
156 else if (ce.op == TOKarrayliteral)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
157 { /* It is for an array literal, so the
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
158 * length will be a const.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
159 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
160 Expression e = new IntegerExp(Loc(0), (cast(ArrayLiteralExp)ce).elements.dim, Type.tsize_t);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
161 v.init = new ExpInitializer(Loc(0), e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
162 v.storage_class |= STCstatic | STCconst;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
163 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
164 else if (ce.op == TOKtuple)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
165 { /* It is for an expression tuple, so the
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
166 * length will be a const.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
167 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
168 Expression e = new IntegerExp(Loc(0), (cast(TupleExp)ce).exps.dim, Type.tsize_t);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
169 v.init = new ExpInitializer(Loc(0), e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
170 v.storage_class |= STCstatic | STCconst;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
171 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
172 *pvar = v;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
173 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
174 (*pvar).semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
175 return (*pvar);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
176 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
177 return null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
178 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
179
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 49
diff changeset
180 override ArrayScopeSymbol isArrayScopeSymbol() { return this; }
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 49
diff changeset
181 }