annotate dmd/ExpInitializer.d @ 187:b0d41ff5e0df

Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
author Abscissa
date Tue, 07 Jun 2011 23:37:34 -0400
parents e3afd1303184
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1 module dmd.ExpInitializer;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
2
114
e28b18c23469 added a module dmd.common for commonly used stuff
Trass3r
parents: 73
diff changeset
3 import dmd.common;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
4 import dmd.Initializer;
73
ef02e2e203c2 Updating to dmd2.033
korDen
parents: 72
diff changeset
5 import dmd.DelegateExp;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
6 import dmd.Loc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
7 import dmd.Scope;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
8 import dmd.Type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
9 import dmd.SymOffExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
10 import dmd.Expression;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
11 import dmd.OutBuffer;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
12 import dmd.HdrGenState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
13 import dmd.WANT;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
14 import dmd.TOK;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
15 import dmd.StringExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
16 import dmd.TY;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
17 import dmd.TypeSArray;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
18
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
19 import dmd.backend.dt_t;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
20
187
b0d41ff5e0df Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents: 178
diff changeset
21 import dmd.DDMDExtensions;
b0d41ff5e0df Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents: 178
diff changeset
22
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
23 class ExpInitializer : Initializer
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
24 {
187
b0d41ff5e0df Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents: 178
diff changeset
25 mixin insertMemberExtension!(typeof(this));
b0d41ff5e0df Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents: 178
diff changeset
26
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
27 Expression exp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
28
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
29 this(Loc loc, Expression exp)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
30 {
178
e3afd1303184 Many small bugs fixed
korDen
parents: 174
diff changeset
31 register();
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
32 super(loc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
33 this.exp = exp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
34 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
35
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
36 override Initializer syntaxCopy()
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
37 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
38 return new ExpInitializer(loc, exp.syntaxCopy());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
39 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
40
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
41 override Initializer semantic(Scope sc, Type t)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
42 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
43 //printf("ExpInitializer.semantic(%s), type = %s\n", exp.toChars(), t.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
44 exp = exp.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
45 exp = resolveProperties(sc, exp);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
46 exp = exp.optimize(WANT.WANTvalue | WANT.WANTinterpret);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
47 Type tb = t.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
48
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
49 /* Look for case of initializing a static array with a too-short
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
50 * string literal, such as:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
51 * char[5] foo = "abc";
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
52 * Allow this by doing an explicit cast, which will lengthen the string
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
53 * literal.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
54 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
55 if (exp.op == TOK.TOKstring && tb.ty == TY.Tsarray && exp.type.ty == TY.Tsarray)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
56 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
57 StringExp se = cast(StringExp)exp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
58
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
59 if (!se.committed && se.type.ty == TY.Tsarray && (cast(TypeSArray)se.type).dim.toInteger() < (cast(TypeSArray)t).dim.toInteger())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
60 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
61 exp = se.castTo(sc, t);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
62 goto L1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
63 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
64 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
65
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
66 // Look for the case of statically initializing an array
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
67 // with a single member.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
68 if (tb.ty == TY.Tsarray && !tb.nextOf().equals(exp.type.toBasetype().nextOf()) && exp.implicitConvTo(tb.nextOf()))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
69 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
70 t = tb.nextOf();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
71 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
72
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
73 exp = exp.implicitCastTo(sc, t);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
74 L1:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
75 exp = exp.optimize(WANT.WANTvalue | WANT.WANTinterpret);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
76 //printf("-ExpInitializer.semantic(): "); exp.print();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
77 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
78 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
79
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
80 override Type inferType(Scope sc)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
81 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
82 //printf("ExpInitializer::inferType() %s\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
83 exp = exp.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
84 exp = resolveProperties(sc, exp);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
85
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
86 // Give error for overloaded function addresses
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
87 if (exp.op == TOKsymoff)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
88 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
89 SymOffExp se = cast(SymOffExp)exp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
90 if (se.hasOverloads && !se.var.isFuncDeclaration().isUnique())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
91 exp.error("cannot infer type from overloaded function symbol %s", exp.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
92 }
73
ef02e2e203c2 Updating to dmd2.033
korDen
parents: 72
diff changeset
93
ef02e2e203c2 Updating to dmd2.033
korDen
parents: 72
diff changeset
94 // Give error for overloaded function addresses
ef02e2e203c2 Updating to dmd2.033
korDen
parents: 72
diff changeset
95 if (exp.op == TOKdelegate)
ef02e2e203c2 Updating to dmd2.033
korDen
parents: 72
diff changeset
96 {
ef02e2e203c2 Updating to dmd2.033
korDen
parents: 72
diff changeset
97 DelegateExp se = cast(DelegateExp)exp;
ef02e2e203c2 Updating to dmd2.033
korDen
parents: 72
diff changeset
98 if (se.func.isFuncDeclaration() && !se.func.isFuncDeclaration().isUnique())
ef02e2e203c2 Updating to dmd2.033
korDen
parents: 72
diff changeset
99 exp.error("cannot infer type from overloaded function symbol %s", exp.toChars());
ef02e2e203c2 Updating to dmd2.033
korDen
parents: 72
diff changeset
100 }
ef02e2e203c2 Updating to dmd2.033
korDen
parents: 72
diff changeset
101
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
102
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
103 Type t = exp.type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
104 if (!t)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
105 t = Initializer.inferType(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
106
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
107 return t;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
108 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
109
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
110 override Expression toExpression()
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
111 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
112 return exp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
113 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
114
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
115 override void toCBuffer(OutBuffer buf, HdrGenState* hgs)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
116 {
174
af724d3510d7 lot os toCBuffer methods implemented
korDen
parents: 114
diff changeset
117 exp.toCBuffer(buf, hgs);
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
118 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
119
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
120 override dt_t* toDt()
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
121 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
122 dt_t* dt = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
123
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
124 exp = exp.optimize(WANT.WANTvalue);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
125 exp.toDt(&dt);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
126
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
127 return dt;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
128 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
129
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
130 override ExpInitializer isExpInitializer() { return this; }
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
131 }