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