annotate dmd/ArrayScopeSymbol.d @ 114:e28b18c23469

added a module dmd.common for commonly used stuff it currently holds code for consistency checking of predefined versions also added a VisualD project file
author Trass3r
date Wed, 01 Sep 2010 18:21:58 +0200
parents 2e2a5c3f943a
children e3afd1303184
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
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
29 class ArrayScopeSymbol : ScopeDsymbol
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
30 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
31 Expression exp; // IndexExp or SliceExp
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
32 TypeTuple type; // for tuple[length]
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
33 TupleDeclaration td; // for tuples of objects
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
34 Scope sc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
35
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
36 this(Scope sc, Expression e)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
37 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
38 super();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
39 assert(e.op == TOKindex || e.op == TOKslice);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
40 this.exp = e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
41 this.sc = sc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
42 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
43
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
44 this(Scope sc, TypeTuple t)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
45 {
49
0aa7d1437ada AttribDeclaration.oneMember
korDen
parents: 38
diff changeset
46 exp = null;
0aa7d1437ada AttribDeclaration.oneMember
korDen
parents: 38
diff changeset
47 type = t;
0aa7d1437ada AttribDeclaration.oneMember
korDen
parents: 38
diff changeset
48 td = null;
0aa7d1437ada AttribDeclaration.oneMember
korDen
parents: 38
diff changeset
49 this.sc = sc;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
50 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
51
38
4caad35a6ceb ArrayScopeSymbol ctor implemented
korDen
parents: 0
diff changeset
52 this(Scope sc, TupleDeclaration s)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
53 {
38
4caad35a6ceb ArrayScopeSymbol ctor implemented
korDen
parents: 0
diff changeset
54 exp = null;
4caad35a6ceb ArrayScopeSymbol ctor implemented
korDen
parents: 0
diff changeset
55 type = null;
4caad35a6ceb ArrayScopeSymbol ctor implemented
korDen
parents: 0
diff changeset
56 td = s;
4caad35a6ceb ArrayScopeSymbol ctor implemented
korDen
parents: 0
diff changeset
57 this.sc = sc;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
58 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
59
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 49
diff changeset
60 override Dsymbol search(Loc loc, Identifier ident, int flags)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
61 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
62 //printf("ArrayScopeSymbol.search('%s', flags = %d)\n", ident.toChars(), flags);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
63 if (ident == Id.length || ident == Id.dollar)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
64 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
65 VarDeclaration* pvar;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
66 Expression ce;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
67
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
68 L1:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
69 if (td)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
70 { /* $ gives the number of elements in the tuple
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
71 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
72 VarDeclaration v = new VarDeclaration(loc, Type.tsize_t, Id.dollar, null);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
73 Expression e = new IntegerExp(Loc(0), td.objects.dim, Type.tsize_t);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
74 v.init = new ExpInitializer(Loc(0), e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
75 v.storage_class |= STCstatic | STCconst;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
76 v.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
77 return v;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
78 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
79
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
80 if (type)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
81 { /* $ gives the number of type entries in the type tuple
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
82 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
83 VarDeclaration v = new VarDeclaration(loc, Type.tsize_t, Id.dollar, null);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
84 Expression e = new IntegerExp(Loc(0), type.arguments.dim, Type.tsize_t);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
85 v.init = new ExpInitializer(Loc(0), e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
86 v.storage_class |= STCstatic | STCconst;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
87 v.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
88 return v;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
89 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
90
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
91 if (exp.op == TOKindex)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
92 { /* array[index] where index is some function of $
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
93 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
94 IndexExp ie = cast(IndexExp)exp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
95
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
96 pvar = &ie.lengthVar;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
97 ce = ie.e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
98 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
99 else if (exp.op == TOKslice)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
100 { /* array[lwr .. upr] where lwr or upr is some function of $
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
101 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
102 SliceExp se = cast(SliceExp)exp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
103
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
104 pvar = &se.lengthVar;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
105 ce = se.e1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
106 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
107 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
108 /* Didn't find $, look in enclosing scope(s).
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
109 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
110 return null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
111
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
112 /* If we are indexing into an array that is really a type
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
113 * tuple, rewrite this as an index into a type tuple and
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
114 * try again.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
115 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
116 if (ce.op == TOKtype)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
117 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
118 Type t = (cast(TypeExp)ce).type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
119 if (t.ty == Ttuple)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
120 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
121 type = cast(TypeTuple)t;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
122 goto L1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
123 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
124 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
125
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
126 /* *pvar is lazily initialized, so if we refer to $
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
127 * multiple times, it gets set only once.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
128 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
129 if (!*pvar) // if not already initialized
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
130 { /* Create variable v and set it to the value of $,
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
131 * which will be a constant.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
132 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
133 VarDeclaration v = new VarDeclaration(loc, Type.tsize_t, Id.dollar, null);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
134
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
135 if (ce.op == TOKvar)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
136 { // if ce is const, get its initializer
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
137 ce = fromConstInitializer(WANTvalue | WANTinterpret, ce);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
138 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
139
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
140 if (ce.op == TOKstring)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
141 { /* It is for a string literal, so the
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
142 * length will be a const.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
143 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
144 Expression e = new IntegerExp(Loc(0), (cast(StringExp)ce).len, Type.tsize_t);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
145 v.init = new ExpInitializer(Loc(0), e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
146 v.storage_class |= STCstatic | STCconst;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
147 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
148 else if (ce.op == TOKarrayliteral)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
149 { /* It is for an array 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(ArrayLiteralExp)ce).elements.dim, 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 == TOKtuple)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
157 { /* It is for an expression tuple, 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(TupleExp)ce).exps.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 *pvar = v;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
165 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
166 (*pvar).semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
167 return (*pvar);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
168 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
169 return null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
170 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
171
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 49
diff changeset
172 override ArrayScopeSymbol isArrayScopeSymbol() { return this; }
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 49
diff changeset
173 }