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