annotate dmd/ArrayScopeSymbol.d @ 49:0aa7d1437ada

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