annotate dmd/ArrayScopeSymbol.d @ 0:10317f0c89a5

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