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