0
|
1 module dmd.ExpInitializer;
|
|
2
|
|
3 import dmd.Initializer;
|
73
|
4 import dmd.DelegateExp;
|
0
|
5 import dmd.Loc;
|
|
6 import dmd.Scope;
|
|
7 import dmd.Type;
|
|
8 import dmd.SymOffExp;
|
|
9 import dmd.Expression;
|
|
10 import dmd.OutBuffer;
|
|
11 import dmd.HdrGenState;
|
|
12 import dmd.WANT;
|
|
13 import dmd.TOK;
|
|
14 import dmd.StringExp;
|
|
15 import dmd.TY;
|
|
16 import dmd.TypeSArray;
|
|
17
|
|
18 import dmd.backend.dt_t;
|
|
19
|
|
20 class ExpInitializer : Initializer
|
|
21 {
|
|
22 Expression exp;
|
|
23
|
|
24 this(Loc loc, Expression exp)
|
|
25 {
|
|
26 super(loc);
|
|
27 this.exp = exp;
|
|
28 }
|
|
29
|
72
|
30 override Initializer syntaxCopy()
|
0
|
31 {
|
|
32 return new ExpInitializer(loc, exp.syntaxCopy());
|
|
33 }
|
|
34
|
72
|
35 override Initializer semantic(Scope sc, Type t)
|
0
|
36 {
|
|
37 //printf("ExpInitializer.semantic(%s), type = %s\n", exp.toChars(), t.toChars());
|
|
38 exp = exp.semantic(sc);
|
|
39 exp = resolveProperties(sc, exp);
|
|
40 exp = exp.optimize(WANT.WANTvalue | WANT.WANTinterpret);
|
|
41 Type tb = t.toBasetype();
|
|
42
|
|
43 /* Look for case of initializing a static array with a too-short
|
|
44 * string literal, such as:
|
|
45 * char[5] foo = "abc";
|
|
46 * Allow this by doing an explicit cast, which will lengthen the string
|
|
47 * literal.
|
|
48 */
|
|
49 if (exp.op == TOK.TOKstring && tb.ty == TY.Tsarray && exp.type.ty == TY.Tsarray)
|
|
50 {
|
|
51 StringExp se = cast(StringExp)exp;
|
|
52
|
|
53 if (!se.committed && se.type.ty == TY.Tsarray && (cast(TypeSArray)se.type).dim.toInteger() < (cast(TypeSArray)t).dim.toInteger())
|
|
54 {
|
|
55 exp = se.castTo(sc, t);
|
|
56 goto L1;
|
|
57 }
|
|
58 }
|
|
59
|
|
60 // Look for the case of statically initializing an array
|
|
61 // with a single member.
|
|
62 if (tb.ty == TY.Tsarray && !tb.nextOf().equals(exp.type.toBasetype().nextOf()) && exp.implicitConvTo(tb.nextOf()))
|
|
63 {
|
|
64 t = tb.nextOf();
|
|
65 }
|
|
66
|
|
67 exp = exp.implicitCastTo(sc, t);
|
|
68 L1:
|
|
69 exp = exp.optimize(WANT.WANTvalue | WANT.WANTinterpret);
|
|
70 //printf("-ExpInitializer.semantic(): "); exp.print();
|
|
71 return this;
|
|
72 }
|
|
73
|
72
|
74 override Type inferType(Scope sc)
|
0
|
75 {
|
|
76 //printf("ExpInitializer::inferType() %s\n", toChars());
|
|
77 exp = exp.semantic(sc);
|
|
78 exp = resolveProperties(sc, exp);
|
|
79
|
|
80 // Give error for overloaded function addresses
|
|
81 if (exp.op == TOKsymoff)
|
|
82 {
|
|
83 SymOffExp se = cast(SymOffExp)exp;
|
|
84 if (se.hasOverloads && !se.var.isFuncDeclaration().isUnique())
|
|
85 exp.error("cannot infer type from overloaded function symbol %s", exp.toChars());
|
|
86 }
|
73
|
87
|
|
88 // Give error for overloaded function addresses
|
|
89 if (exp.op == TOKdelegate)
|
|
90 {
|
|
91 DelegateExp se = cast(DelegateExp)exp;
|
|
92 if (se.func.isFuncDeclaration() && !se.func.isFuncDeclaration().isUnique())
|
|
93 exp.error("cannot infer type from overloaded function symbol %s", exp.toChars());
|
|
94 }
|
|
95
|
0
|
96
|
|
97 Type t = exp.type;
|
|
98 if (!t)
|
|
99 t = Initializer.inferType(sc);
|
|
100
|
|
101 return t;
|
|
102 }
|
|
103
|
72
|
104 override Expression toExpression()
|
0
|
105 {
|
|
106 return exp;
|
|
107 }
|
|
108
|
72
|
109 override void toCBuffer(OutBuffer buf, HdrGenState* hgs)
|
0
|
110 {
|
|
111 assert(false);
|
|
112 }
|
|
113
|
72
|
114 override dt_t* toDt()
|
0
|
115 {
|
|
116 dt_t* dt = null;
|
|
117
|
|
118 exp = exp.optimize(WANT.WANTvalue);
|
|
119 exp.toDt(&dt);
|
|
120
|
|
121 return dt;
|
|
122 }
|
|
123
|
72
|
124 override ExpInitializer isExpInitializer() { return this; }
|
|
125 }
|