annotate dmd/ArrayScopeSymbol.d @ 178:e3afd1303184

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