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