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