annotate dmd/ExpInitializer.d @ 132:c494af1dba80

Fixes for dmd 2.037
author Eldar Insafutdinov <e.insafutdinov@gmail.com>
date Fri, 10 Sep 2010 19:14:09 +0100
parents e28b18c23469
children af724d3510d7
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
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
21 class ExpInitializer : Initializer
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
22 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
23 Expression exp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
24
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
25 this(Loc loc, Expression exp)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
26 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
27 super(loc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
28 this.exp = exp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
29 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
30
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
31 override Initializer syntaxCopy()
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
32 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
33 return new ExpInitializer(loc, exp.syntaxCopy());
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 semantic(Scope sc, Type t)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
37 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
38 //printf("ExpInitializer.semantic(%s), type = %s\n", exp.toChars(), t.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
39 exp = exp.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
40 exp = resolveProperties(sc, exp);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
41 exp = exp.optimize(WANT.WANTvalue | WANT.WANTinterpret);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
42 Type tb = t.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
43
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
44 /* Look for case of initializing a static array with a too-short
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
45 * string literal, such as:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
46 * char[5] foo = "abc";
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
47 * Allow this by doing an explicit cast, which will lengthen the string
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
48 * literal.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
49 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
50 if (exp.op == TOK.TOKstring && tb.ty == TY.Tsarray && exp.type.ty == TY.Tsarray)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
51 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
52 StringExp se = cast(StringExp)exp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
53
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
54 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
55 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
56 exp = se.castTo(sc, t);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
57 goto L1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
58 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
59 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
60
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
61 // Look for the case of statically initializing an array
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
62 // with a single member.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
63 if (tb.ty == TY.Tsarray && !tb.nextOf().equals(exp.type.toBasetype().nextOf()) && exp.implicitConvTo(tb.nextOf()))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
64 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
65 t = tb.nextOf();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
66 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
67
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
68 exp = exp.implicitCastTo(sc, t);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
69 L1:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
70 exp = exp.optimize(WANT.WANTvalue | WANT.WANTinterpret);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
71 //printf("-ExpInitializer.semantic(): "); exp.print();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
72 return this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
73 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
74
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
75 override Type inferType(Scope sc)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
76 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
77 //printf("ExpInitializer::inferType() %s\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
78 exp = exp.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
79 exp = resolveProperties(sc, exp);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
80
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
81 // Give error for overloaded function addresses
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
82 if (exp.op == TOKsymoff)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
83 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
84 SymOffExp se = cast(SymOffExp)exp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
85 if (se.hasOverloads && !se.var.isFuncDeclaration().isUnique())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
86 exp.error("cannot infer type from overloaded function symbol %s", exp.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
87 }
73
ef02e2e203c2 Updating to dmd2.033
korDen
parents: 72
diff changeset
88
ef02e2e203c2 Updating to dmd2.033
korDen
parents: 72
diff changeset
89 // Give error for overloaded function addresses
ef02e2e203c2 Updating to dmd2.033
korDen
parents: 72
diff changeset
90 if (exp.op == TOKdelegate)
ef02e2e203c2 Updating to dmd2.033
korDen
parents: 72
diff changeset
91 {
ef02e2e203c2 Updating to dmd2.033
korDen
parents: 72
diff changeset
92 DelegateExp se = cast(DelegateExp)exp;
ef02e2e203c2 Updating to dmd2.033
korDen
parents: 72
diff changeset
93 if (se.func.isFuncDeclaration() && !se.func.isFuncDeclaration().isUnique())
ef02e2e203c2 Updating to dmd2.033
korDen
parents: 72
diff changeset
94 exp.error("cannot infer type from overloaded function symbol %s", exp.toChars());
ef02e2e203c2 Updating to dmd2.033
korDen
parents: 72
diff changeset
95 }
ef02e2e203c2 Updating to dmd2.033
korDen
parents: 72
diff changeset
96
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
97
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
98 Type t = exp.type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
99 if (!t)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
100 t = Initializer.inferType(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
101
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
102 return t;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
103 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
104
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
105 override Expression toExpression()
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
106 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
107 return exp;
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 void toCBuffer(OutBuffer buf, HdrGenState* hgs)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
111 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
112 assert(false);
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 dt_t* toDt()
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
116 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
117 dt_t* dt = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
118
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
119 exp = exp.optimize(WANT.WANTvalue);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
120 exp.toDt(&dt);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
121
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
122 return dt;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
123 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
124
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
125 override ExpInitializer isExpInitializer() { return this; }
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 0
diff changeset
126 }