annotate dmd/VarDeclaration.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 cd48cb899aee
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1 module dmd.VarDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
2
114
e28b18c23469 added a module dmd.common for commonly used stuff
Trass3r
parents: 113
diff changeset
3 import dmd.common;
84
be2ab491772e Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 77
diff changeset
4 import dmd.Array;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
5 import dmd.Declaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
6 import dmd.SliceExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
7 import dmd.ClassDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
8 import dmd.DeleteExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
9 import dmd.SymOffExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
10 import dmd.DotIdExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
11 import dmd.PtrExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
12 import dmd.CallExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
13 import dmd.DotVarExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
14 import dmd.CommaExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
15 import dmd.CastExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
16 import dmd.WANT;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
17 import dmd.StructDeclaration;
68
ee3a9f34dc48 final bits of codegen implementation to compile Phobos
korDen
parents: 53
diff changeset
18 import dmd.StorageClassDeclaration;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
19 import dmd.DsymbolExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
20 import dmd.TypeSArray;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
21 import dmd.IntegerExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
22 import dmd.VarExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
23 import dmd.AssignExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
24 import dmd.TypeTypedef;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
25 import dmd.ArrayInitializer;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
26 import dmd.StructInitializer;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
27 import dmd.NewExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
28 import dmd.TupleDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
29 import dmd.AggregateDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
30 import dmd.InterfaceDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
31 import dmd.TemplateInstance;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
32 import dmd.Id;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
33 import dmd.Initializer;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
34 import dmd.TypeStruct;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
35 import dmd.TypeTuple;
130
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
36 import dmd.Parameter;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
37 import dmd.ExpInitializer;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
38 import dmd.ArrayTypes;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
39 import dmd.Dsymbol;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
40 import dmd.Expression;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
41 import dmd.Loc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
42 import dmd.STC;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
43 import dmd.TOK;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
44 import dmd.TupleExp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
45 import dmd.Global;
22
fd4acc376c45 Implemented object file output and linking on linux.
Robert Clipsham <robert@octarineparrot.com>
parents: 16
diff changeset
46 import dmd.Module;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
47 import dmd.FuncDeclaration;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
48 import dmd.Type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
49 import dmd.TY;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
50 import dmd.LINK;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
51 import dmd.Scope;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
52 import dmd.Identifier;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
53 import dmd.OutBuffer;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
54 import dmd.HdrGenState;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
55 import dmd.PROT;
73
ef02e2e203c2 Updating to dmd2.033
korDen
parents: 72
diff changeset
56 import dmd.expression.Util;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
57
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
58 import dmd.backend.Symbol;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
59 import dmd.backend.TYM;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
60 import dmd.backend.FL;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
61 import dmd.backend.DT;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
62 import dmd.backend.mTY;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
63 import dmd.backend.SC;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
64 import dmd.backend.mTYman;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
65 import dmd.backend.TYPE;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
66 import dmd.backend.Util;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
67 import dmd.backend.LIST;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
68
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
69 import std.stdio : writef;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
70 import std.string : toStringz;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
71
187
b0d41ff5e0df Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents: 179
diff changeset
72 import dmd.DDMDExtensions;
b0d41ff5e0df Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents: 179
diff changeset
73
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
74 class VarDeclaration : Declaration
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
75 {
187
b0d41ff5e0df Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents: 179
diff changeset
76 mixin insertMemberExtension!(typeof(this));
b0d41ff5e0df Added expandability scheme outlined in http://www.dsource.org/forums/viewtopic.php?t=5659&sid=6f2150ff5b0bffcd47512a6a7608d218
Abscissa
parents: 179
diff changeset
77
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
78 Initializer init;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
79 uint offset;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
80 bool noauto; // no auto semantics
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
81 version (DMDV2) {
98
5c859d5fbe27 and more
Trass3r
parents: 88
diff changeset
82 FuncDeclarations nestedrefs; // referenced by these lexically nested functions
5c859d5fbe27 and more
Trass3r
parents: 88
diff changeset
83 bool isargptr = false; // if parameter that _argptr points to
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
84 } else {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
85 int nestedref; // referenced by a lexically nested function
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
86 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
87 int ctorinit; // it has been initialized in a ctor
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
88 int onstack; // 1: it has been allocated on the stack
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
89 // 2: on stack, run destructor anyway
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
90 int canassign; // it can be assigned to
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
91 Dsymbol aliassym; // if redone as alias to another symbol
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
92 Expression value; // when interpreting, this is the value
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
93 // (null if value not determinable)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
94 version (DMDV2) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
95 VarDeclaration rundtor; // if !null, rundtor is tested at runtime to see
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
96 // if the destructor should be run. Used to prevent
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
97 // dtor calls on postblitted vars
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
98 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
99
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
100 this(Loc loc, Type type, Identifier id, Initializer init)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
101 {
178
e3afd1303184 Many small bugs fixed
korDen
parents: 157
diff changeset
102 register();
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
103 super(id);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
104
98
5c859d5fbe27 and more
Trass3r
parents: 88
diff changeset
105 debug
5c859d5fbe27 and more
Trass3r
parents: 88
diff changeset
106 {
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
107 if (!type && !init)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
108 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
109 writef("VarDeclaration('%s')\n", id.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
110 //*(char*)0=0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
111 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
112 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
113 assert(type || init);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
114 this.type = type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
115 this.init = init;
98
5c859d5fbe27 and more
Trass3r
parents: 88
diff changeset
116 version(_DH)
5c859d5fbe27 and more
Trass3r
parents: 88
diff changeset
117 {
5c859d5fbe27 and more
Trass3r
parents: 88
diff changeset
118 this.htype = null;
5c859d5fbe27 and more
Trass3r
parents: 88
diff changeset
119 this.hinit = null;
5c859d5fbe27 and more
Trass3r
parents: 88
diff changeset
120 }
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
121 this.loc = loc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
122
98
5c859d5fbe27 and more
Trass3r
parents: 88
diff changeset
123 /* TODO:
5c859d5fbe27 and more
Trass3r
parents: 88
diff changeset
124 #if DMDV1
5c859d5fbe27 and more
Trass3r
parents: 88
diff changeset
125 nestedref = 0;
5c859d5fbe27 and more
Trass3r
parents: 88
diff changeset
126 #endif
5c859d5fbe27 and more
Trass3r
parents: 88
diff changeset
127 ctorinit = 0;
5c859d5fbe27 and more
Trass3r
parents: 88
diff changeset
128 aliassym = NULL;
5c859d5fbe27 and more
Trass3r
parents: 88
diff changeset
129 onstack = 0;
5c859d5fbe27 and more
Trass3r
parents: 88
diff changeset
130 canassign = 0;
5c859d5fbe27 and more
Trass3r
parents: 88
diff changeset
131 value = NULL;
5c859d5fbe27 and more
Trass3r
parents: 88
diff changeset
132 rundtor = NULL;
5c859d5fbe27 and more
Trass3r
parents: 88
diff changeset
133 */
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
134 nestedrefs = new FuncDeclarations();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
135 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
136
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
137 override Dsymbol syntaxCopy(Dsymbol s)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
138 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
139 //printf("VarDeclaration.syntaxCopy(%s)\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
140
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
141 VarDeclaration sv;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
142 if (s)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
143 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
144 sv = cast(VarDeclaration)s;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
145 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
146 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
147 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
148 Initializer init = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
149 if (this.init)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
150 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
151 init = this.init.syntaxCopy();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
152 //init.isExpInitializer().exp.print();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
153 //init.isExpInitializer().exp.dump(0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
154 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
155
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
156 sv = new VarDeclaration(loc, type ? type.syntaxCopy() : null, ident, init);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
157 sv.storage_class = storage_class;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
158 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
159
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
160 version (_DH) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
161 // Syntax copy for header file
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
162 if (!htype) // Don't overwrite original
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
163 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
164 if (type) // Make copy for both old and new instances
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
165 { htype = type.syntaxCopy();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
166 sv.htype = type.syntaxCopy();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
167 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
168 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
169 else // Make copy of original for new instance
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
170 sv.htype = htype.syntaxCopy();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
171 if (!hinit)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
172 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
173 if (init)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
174 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
175 hinit = init.syntaxCopy();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
176 sv.hinit = init.syntaxCopy();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
177 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
178 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
179 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
180 sv.hinit = hinit.syntaxCopy();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
181 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
182 return sv;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
183 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
184
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
185 override void semantic(Scope sc)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
186 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
187 static if (false) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
188 printf("VarDeclaration.semantic('%s', parent = '%s')\n", toChars(), sc.parent.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
189 printf(" type = %s\n", type ? type.toChars() : "null");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
190 printf(" stc = x%x\n", sc.stc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
191 printf(" storage_class = x%x\n", storage_class);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
192 printf("linkage = %d\n", sc.linkage);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
193 //if (strcmp(toChars(), "mul") == 0) halt();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
194 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
195
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
196 storage_class |= sc.stc;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
197 if (storage_class & STC.STCextern && init)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
198 error("extern symbols cannot have initializers");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
199
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
200 /* If auto type inference, do the inference
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
201 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
202 int inferred = 0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
203 if (!type)
157
b7b61140701d * added all missing default cases in switch statements
trass3r
parents: 135
diff changeset
204 {
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
205 inuse++;
130
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
206
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
207 ArrayInitializer ai = init.isArrayInitializer();
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
208 if (ai)
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
209 {
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
210 Expression e;
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
211 if (ai.isAssociativeArray())
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
212 e = ai.toAssocArrayLiteral();
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
213 else
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
214 e = init.toExpression();
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
215 init = new ExpInitializer(e.loc, e);
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
216 type = init.inferType(sc);
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
217 if (type.ty == TY.Tsarray)
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
218 type = type.nextOf().arrayOf();
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
219 }
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
220 else
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
221 type = init.inferType(sc);
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
222
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
223 inuse--;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
224 inferred = 1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
225
130
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
226 if (init.isArrayInitializer() && type.toBasetype().ty == TY.Tsarray)
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
227 { // Prefer array literals to give a T[] type rather than a T[dim]
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
228 type = type.toBasetype().nextOf().arrayOf();
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
229 }
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
230
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
231 /* This is a kludge to support the existing syntax for RAII
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
232 * declarations.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
233 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
234 storage_class &= ~STC.STCauto;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
235 originalType = type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
236 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
237 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
238 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
239 if (!originalType)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
240 originalType = type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
241
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
242 type = type.semantic(loc, sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
243 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
244 //printf(" semantic type = %s\n", type ? type.toChars() : "null");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
245
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
246 type.checkDeprecated(loc, sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
247 linkage = sc.linkage;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
248 this.parent = sc.parent;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
249 //printf("this = %p, parent = %p, '%s'\n", this, parent, parent.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
250 protection = sc.protection;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
251 //printf("sc.stc = %x\n", sc.stc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
252 //printf("storage_class = x%x\n", storage_class);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
253
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
254 version (DMDV2) {
130
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
255 static if (true) {
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
256 if (storage_class & STC.STCgshared && sc.func && sc.func.isSafe())
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
257 {
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
258 error("__gshared not allowed in safe functions; use shared");
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
259 }
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
260 } else {
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
261 if (storage_class & STC.STCgshared && global.params.safe && !sc.module_.safe)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
262 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
263 error("__gshared not allowed in safe mode; use shared");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
264 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
265 }
130
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
266 }
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
267
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
268 Dsymbol parent = toParent();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
269 FuncDeclaration fd = parent.isFuncDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
270
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
271 Type tb = type.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
272 if (tb.ty == TY.Tvoid && !(storage_class & STC.STClazy))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
273 { error("voids have no value");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
274 type = Type.terror;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
275 tb = type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
276 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
277 if (tb.ty == TY.Tfunction)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
278 { error("cannot be declared to be a function");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
279 type = Type.terror;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
280 tb = type;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
281 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
282 if (tb.ty == TY.Tstruct)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
283 { TypeStruct ts = cast(TypeStruct)tb;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
284
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
285 if (!ts.sym.members)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
286 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
287 error("no definition of struct %s", ts.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
288 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
289 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
290
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
291 if (tb.ty == TY.Ttuple)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
292 { /* Instead, declare variables for each of the tuple elements
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
293 * and add those.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
294 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
295 TypeTuple tt = cast(TypeTuple)tb;
130
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
296 size_t nelems = Parameter.dim(tt.arguments);
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
297 Objects exps = new Objects();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
298 exps.setDim(nelems);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
299 Expression ie = init ? init.toExpression() : null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
300
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
301 for (size_t i = 0; i < nelems; i++)
130
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
302 { auto arg = Parameter.getNth(tt.arguments, i);
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
303
130
60bb0fe4563e dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 114
diff changeset
304 auto buf = new OutBuffer();
53
a8b50ff7f201 ForeachStatement.syntaxCopy
korDen
parents: 23
diff changeset
305 ///buf.printf("_%s_field_%zu", ident.toChars(), i);
a8b50ff7f201 ForeachStatement.syntaxCopy
korDen
parents: 23
diff changeset
306 buf.printf("_%s_field_%s", ident.toChars(), i);
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
307 buf.writeByte(0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
308 string name = buf.extractString();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
309 Identifier id = new Identifier(name, TOK.TOKidentifier);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
310
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
311 Expression einit = ie;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
312 if (ie && ie.op == TOK.TOKtuple)
113
3482c73a991b More cleanup for arrays
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 110
diff changeset
313 { einit = (cast(TupleExp)ie).exps[i];
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
314 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
315 Initializer ti = init;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
316 if (einit)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
317 { ti = new ExpInitializer(einit.loc, einit);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
318 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
319
74
7e0d548de9e6 Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 73
diff changeset
320 auto v = new VarDeclaration(loc, arg.type, id, ti);
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
321 //printf("declaring field %s of type %s\n", v.toChars(), v.type.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
322 v.semantic(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
323
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
324 if (sc.scopesym)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
325 { //printf("adding %s to %s\n", v.toChars(), sc.scopesym.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
326 if (sc.scopesym.members)
74
7e0d548de9e6 Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 73
diff changeset
327 sc.scopesym.members.push(v);
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
328 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
329
94
3a0b150c9841 Objects -> Vector!Object iteration 1
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 88
diff changeset
330 auto e = new DsymbolExp(loc, v);
3a0b150c9841 Objects -> Vector!Object iteration 1
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 88
diff changeset
331 exps[i] = e;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
332 }
94
3a0b150c9841 Objects -> Vector!Object iteration 1
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 88
diff changeset
333 auto v2 = new TupleDeclaration(loc, ident, exps);
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
334 v2.isexp = 1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
335 aliassym = v2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
336 return;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
337 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
338
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
339 Lagain:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
340 /* Storage class can modify the type
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
341 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
342 type = type.addStorageClass(storage_class);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
343
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
344 /* Adjust storage class to reflect type
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
345 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
346 if (type.isConst())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
347 { storage_class |= STC.STCconst;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
348 if (type.isShared())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
349 storage_class |= STC.STCshared;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
350 }
135
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 130
diff changeset
351 else if (type.isImmutable())
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
352 storage_class |= STC.STCimmutable;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
353 else if (type.isShared())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
354 storage_class |= STC.STCshared;
135
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 130
diff changeset
355 else if (type.isWild())
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 130
diff changeset
356 storage_class |= STC.STCwild;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
357
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
358 if (isSynchronized())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
359 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
360 error("variable %s cannot be synchronized", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
361 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
362 else if (isOverride())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
363 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
364 error("override cannot be applied to variable");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
365 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
366 else if (isAbstract())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
367 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
368 error("abstract cannot be applied to variable");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
369 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
370 else if (storage_class & STC.STCfinal)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
371 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
372 error("final cannot be applied to variable");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
373 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
374
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
375 if (storage_class & (STC.STCstatic | STC.STCextern | STC.STCmanifest | STC.STCtemplateparameter | STC.STCtls | STC.STCgshared))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
376 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
377 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
378 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
379 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
380 AggregateDeclaration aad = sc.anonAgg;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
381 if (!aad)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
382 aad = parent.isAggregateDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
383 if (aad)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
384 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
385 ///version (DMDV2) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
386 assert(!(storage_class & (STC.STCextern | STC.STCstatic | STC.STCtls | STC.STCgshared)));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
387
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
388 if (storage_class & (STC.STCconst | STC.STCimmutable) && init)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
389 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
390 if (!type.toBasetype().isTypeBasic())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
391 storage_class |= STC.STCstatic;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
392 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
393 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
394 ///}
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
395 aad.addField(sc, this);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
396 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
397
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
398 InterfaceDeclaration id = parent.isInterfaceDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
399 if (id)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
400 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
401 error("field not allowed in interface");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
402 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
403
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
404 /* Templates cannot add fields to aggregates
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
405 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
406 TemplateInstance ti = parent.isTemplateInstance();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
407 if (ti)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
408 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
409 // Take care of nested templates
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
410 while (1)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
411 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
412 TemplateInstance ti2 = ti.tempdecl.parent.isTemplateInstance();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
413 if (!ti2)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
414 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
415 ti = ti2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
416 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
417
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
418 // If it's a member template
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
419 AggregateDeclaration ad = ti.tempdecl.isMember();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
420 if (ad && storage_class != STC.STCundefined)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
421 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
422 error("cannot use template to add field to aggregate '%s'", ad.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
423 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
424 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
425 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
426
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
427 version (DMDV2) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
428 if ((storage_class & (STC.STCref | STC.STCparameter | STC.STCforeach)) == STC.STCref && ident != Id.This)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
429 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
430 error("only parameters or foreach declarations can be ref");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
431 }
135
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 130
diff changeset
432
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 130
diff changeset
433 if ((storage_class & (STCstatic | STCextern | STCtls | STCgshared | STCmanifest) ||
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 130
diff changeset
434 isDataseg()) &&
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 130
diff changeset
435 type.hasWild())
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 130
diff changeset
436 {
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 130
diff changeset
437 error("only fields, parameters or stack based variables can be inout");
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 130
diff changeset
438 }
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
439 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
440
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
441 if (type.isauto() && !noauto)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
442 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
443 if (storage_class & (STC.STCfield | STC.STCout | STC.STCref | STC.STCstatic | STC.STCmanifest | STC.STCtls | STC.STCgshared) || !fd)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
444 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
445 error("globals, statics, fields, manifest constants, ref and out parameters cannot be scope");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
446 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
447
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
448 if (!(storage_class & (STC.STCauto | STC.STCscope)))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
449 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
450 if (!(storage_class & STC.STCparameter) && ident != Id.withSym)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
451 error("reference to scope class must be scope");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
452 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
453 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
454
135
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 130
diff changeset
455 if ((isConst() || isImmutable()) && !init && !fd)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
456 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
457 // Initialize by constructor only
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
458 storage_class |= STC.STCctorinit;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
459 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
460
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
461 if (init)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
462 storage_class |= STC.STCinit; // remember we had an explicit initializer
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
463 else if (storage_class & STC.STCmanifest)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
464 error("manifest constants must have initializers");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
465
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
466 TOK op = TOK.TOKconstruct;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
467 if (!init && !sc.inunion && !isStatic() && fd &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
468 (!(storage_class & (STC.STCfield | STC.STCin | STC.STCforeach | STC.STCparameter)) || (storage_class & STC.STCout)) &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
469 type.size() != 0)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
470 {
179
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
471 // Provide a default initializer
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
472 //printf("Providing default initializer for '%s'\n", toChars());
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
473 if (type.ty == TY.Tstruct &&
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
474 (cast(TypeStruct)type).sym.zeroInit)
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
475 { /* If a struct is all zeros, as a special case
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
476 * set it's initializer to the integer 0.
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
477 * In AssignExp.toElem(), we check for this and issue
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
478 * a memset() to initialize the struct.
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
479 * Must do same check in interpreter.
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
480 */
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
481 Expression e = new IntegerExp(loc, 0, Type.tint32);
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
482 Expression e1;
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
483 e1 = new VarExp(loc, this);
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
484 e = new AssignExp(loc, e1, e);
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
485 e.op = TOK.TOKconstruct;
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
486 e.type = e1.type; // don't type check this, it would fail
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
487 init = new ExpInitializer(loc, e);
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
488 return;
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
489 }
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
490 else if (type.ty == TY.Ttypedef)
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
491 {
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
492 TypeTypedef td = cast(TypeTypedef)type;
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
493 if (td.sym.init)
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
494 {
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
495 init = td.sym.init;
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
496 ExpInitializer ie = init.isExpInitializer();
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
497 if (ie)
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
498 // Make copy so we can modify it
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
499 init = new ExpInitializer(ie.loc, ie.exp);
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
500 }
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
501 else
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
502 init = getExpInitializer();
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
503 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
504 else
179
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
505 {
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
506 init = getExpInitializer();
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
507 }
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
508 // Default initializer is always a blit
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
509 op = TOK.TOKblit;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
510 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
511
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
512 if (init)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
513 {
179
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
514 sc = sc.push();
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
515 sc.stc &= ~(STC.STC_TYPECTOR | STC.STCpure | STC.STCnothrow | STC.STCref | STCdisable);
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
516
179
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
517 ArrayInitializer ai = init.isArrayInitializer();
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
518 if (ai && tb.ty == TY.Taarray)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
519 {
179
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
520 Expression e = ai.toAssocArrayLiteral();
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
521 init = new ExpInitializer(e.loc, e);
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
522 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
523
179
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
524 StructInitializer si = init.isStructInitializer();
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
525 ExpInitializer ei = init.isExpInitializer();
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
526
179
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
527 // See if initializer is a NewExp that can be allocated on the stack
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
528 if (ei && isScope() && ei.exp.op == TOK.TOKnew)
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
529 { NewExp ne = cast(NewExp)ei.exp;
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
530 if (!(ne.newargs && ne.newargs.dim))
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
531 { ne.onstack = 1;
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
532 onstack = 1;
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
533 if (type.isBaseOf(ne.newtype.semantic(loc, sc), null))
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
534 onstack = 2;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
535 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
536 }
179
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
537
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
538 // If inside function, there is no semantic3() call
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
539 if (sc.func)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
540 {
179
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
541 // If local variable, use AssignExp to handle all the various
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
542 // possibilities.
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
543 if (fd &&
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
544 !(storage_class & (STC.STCmanifest | STC.STCstatic | STC.STCtls | STC.STCgshared | STC.STCextern)) &&
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
545 !init.isVoidInitializer())
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
546 {
179
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
547 //printf("fd = '%s', var = '%s'\n", fd.toChars(), toChars());
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
548 if (!ei)
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
549 {
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
550 Expression e = init.toExpression();
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
551 if (!e)
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
552 {
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
553 init = init.semantic(sc, type);
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
554 e = init.toExpression();
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
555 if (!e)
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
556 { error("is not a static and cannot have static initializer");
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
557 return;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
558 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
559 }
179
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
560 ei = new ExpInitializer(init.loc, e);
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
561 init = ei;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
562 }
179
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
563
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
564 Expression e1 = new VarExp(loc, this);
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
565
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
566 Type t = type.toBasetype();
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
567 if (t.ty == TY.Tsarray && !(storage_class & (STC.STCref | STC.STCout)))
73
ef02e2e203c2 Updating to dmd2.033
korDen
parents: 72
diff changeset
568 {
179
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
569 ei.exp = ei.exp.semantic(sc);
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
570 if (!ei.exp.implicitConvTo(type))
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
571 {
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
572 int dim = cast(int)(cast(TypeSArray)t).dim.toInteger(); ///
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
573 // If multidimensional static array, treat as one large array
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
574 while (1)
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
575 {
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
576 t = t.nextOf().toBasetype();
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
577 if (t.ty != TY.Tsarray)
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
578 break;
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
579 dim *= (cast(TypeSArray)t).dim.toInteger();
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
580 e1.type = new TypeSArray(t.nextOf(), new IntegerExp(Loc(0), dim, Type.tindex));
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
581 }
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
582 }
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
583 e1 = new SliceExp(loc, e1, null, null);
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
584 }
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
585 else if (t.ty == TY.Tstruct)
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
586 {
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
587 ei.exp = ei.exp.semantic(sc);
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
588 ei.exp = resolveProperties(sc, ei.exp);
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
589 StructDeclaration sd = (cast(TypeStruct)t).sym;
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
590 version (DMDV2)
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
591 {
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
592 /* Look to see if initializer is a call to the constructor
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
593 */
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
594 if (sd.ctor && // there are constructors
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
595 ei.exp.type.ty == TY.Tstruct && // rvalue is the same struct
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
596 (cast(TypeStruct)ei.exp.type).sym == sd &&
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
597 ei.exp.op == TOK.TOKstar)
98
5c859d5fbe27 and more
Trass3r
parents: 88
diff changeset
598 {
179
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
599 /* Look for form of constructor call which is:
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
600 * *__ctmp.ctor(arguments...)
73
ef02e2e203c2 Updating to dmd2.033
korDen
parents: 72
diff changeset
601 */
179
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
602 PtrExp pe = cast(PtrExp)ei.exp;
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
603 if (pe.e1.op == TOK.TOKcall)
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
604 { CallExp ce = cast(CallExp)pe.e1;
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
605 if (ce.e1.op == TOK.TOKdotvar)
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
606 { DotVarExp dve = cast(DotVarExp)ce.e1;
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
607 if (dve.var.isCtorDeclaration())
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
608 { /* It's a constructor call, currently constructing
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
609 * a temporary __ctmp.
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
610 */
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
611 /* Before calling the constructor, initialize
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
612 * variable with a bit copy of the default
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
613 * initializer
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
614 */
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
615 Expression e = new AssignExp(loc, new VarExp(loc, this), t.defaultInit(loc));
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
616 e.op = TOK.TOKblit;
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
617 e.type = t;
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
618 ei.exp = new CommaExp(loc, e, ei.exp);
98
5c859d5fbe27 and more
Trass3r
parents: 88
diff changeset
619
179
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
620 /* Replace __ctmp being constructed with e1
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
621 */
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
622 dve.e1 = e1;
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
623 return;
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
624 }
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
625 }
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
626 }
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
627 }
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
628 }
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
629 if (!ei.exp.implicitConvTo(type))
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
630 {
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
631 Type ti = ei.exp.type.toBasetype();
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
632 // Look for constructor first
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
633 if (sd.ctor &&
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
634 /* Initializing with the same type is done differently
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
635 */
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
636 !(ti.ty == Tstruct && t.toDsymbol(sc) == ti.toDsymbol(sc)))
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
637 {
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
638 // Rewrite as e1.ctor(arguments)
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
639 Expression ector = new DotIdExp(loc, e1, Id.ctor);
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
640 ei.exp = new CallExp(loc, ector, ei.exp);
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
641 }
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
642 else
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
643 /* Look for opCall
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
644 * See bugzilla 2702 for more discussion
73
ef02e2e203c2 Updating to dmd2.033
korDen
parents: 72
diff changeset
645 */
179
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
646
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
647 // Don't cast away invariant or mutability in initializer
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
648 if (search_function(sd, Id.call) &&
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
649 /* Initializing with the same type is done differently
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
650 */
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
651 !(ti.ty == Tstruct && t.toDsymbol(sc) == ti.toDsymbol(sc)))
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
652 { // Rewrite as e1.call(arguments)
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
653 Expression eCall = new DotIdExp(loc, e1, Id.call);
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
654 ei.exp = new CallExp(loc, eCall, ei.exp);
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
655 }
73
ef02e2e203c2 Updating to dmd2.033
korDen
parents: 72
diff changeset
656 }
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
657 }
179
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
658 ei.exp = new AssignExp(loc, e1, ei.exp);
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
659 ei.exp.op = op;
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
660 canassign++;
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
661 ei.exp = ei.exp.semantic(sc);
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
662 canassign--;
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
663 ei.exp.optimize(WANT.WANTvalue);
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
664 }
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
665 else
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
666 {
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
667 init = init.semantic(sc, type);
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
668 }
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
669 }
179
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
670 else if (storage_class & (STC.STCconst | STC.STCimmutable | STC.STCmanifest) ||
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
671 type.isConst() || type.isImmutable() ||
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
672 parent.isAggregateDeclaration())
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
673 {
179
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
674 /* Because we may need the results of a const declaration in a
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
675 * subsequent type, such as an array dimension, before semantic2()
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
676 * gets ordinarily run, try to run semantic2() now.
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
677 * Ignore failure.
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
678 */
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
679
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
680 if (!global.errors && !inferred)
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
681 {
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
682 uint errors = global.errors;
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
683 global.gag++;
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
684 //printf("+gag\n");
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
685 Expression e;
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
686 Initializer i2 = init;
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
687 inuse++;
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
688 if (ei)
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
689 {
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
690 e = ei.exp.syntaxCopy();
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
691 e = e.semantic(sc);
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
692 e = resolveProperties(sc, e);
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
693 version (DMDV2) {
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
694 /* The problem is the following code:
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
695 * struct CopyTest {
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
696 * double x;
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
697 * this(double a) { x = a * 10.0;}
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
698 * this(this) { x += 2.0; }
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
699 * }
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
700 * const CopyTest z = CopyTest(5.3); // ok
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
701 * const CopyTest w = z; // not ok, postblit not run
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
702 * static assert(w.x == 55.0);
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
703 * because the postblit doesn't get run on the initialization of w.
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
704 */
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
705
179
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
706 Type tb_ = e.type.toBasetype();
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
707 if (tb_.ty == Tstruct)
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
708 {
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
709 StructDeclaration sd = (cast(TypeStruct)tb_).sym;
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
710 Type typeb = type.toBasetype();
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
711 /* Look to see if initializer involves a copy constructor
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
712 * (which implies a postblit)
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
713 */
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
714 if (sd.cpctor && // there is a copy constructor
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
715 typeb.equals(tb_)) // rvalue is the same struct
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
716 {
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
717 // The only allowable initializer is a (non-copy) constructor
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
718 if (e.op == TOKcall)
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
719 {
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
720 CallExp ce = cast(CallExp)e;
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
721 if (ce.e1.op == TOKdotvar)
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
722 {
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
723 DotVarExp dve = cast(DotVarExp)ce.e1;
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
724 if (dve.var.isCtorDeclaration())
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
725 goto LNoCopyConstruction;
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
726 }
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
727 }
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
728 global.gag--;
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
729 error("of type struct %s uses this(this), which is not allowed in static initialization", typeb.toChars());
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
730 global.gag++;
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
731
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
732 LNoCopyConstruction:
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
733 ;
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
734 }
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
735 }
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
736 }
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
737 e = e.implicitCastTo(sc, type);
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
738 }
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
739 else if (si || ai)
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
740 {
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
741 i2 = init.syntaxCopy();
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
742 i2 = i2.semantic(sc, type);
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
743 }
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
744 inuse--;
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
745 global.gag--;
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
746 //printf("-gag\n");
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
747 if (errors != global.errors) // if errors happened
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
748 {
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
749 if (global.gag == 0)
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
750 global.errors = errors; // act as if nothing happened
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
751 version (DMDV2) {
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
752 /* Save scope for later use, to try again
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
753 */
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
754 scope_ = sc.clone();
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
755 scope_.setNoFree();
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
756 }
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
757 }
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
758 else if (ei)
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
759 {
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
760 if (isDataseg())
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
761 /* static const/invariant does CTFE
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
762 */
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
763 e = e.optimize(WANT.WANTvalue | WANT.WANTinterpret);
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
764 else
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
765 e = e.optimize(WANT.WANTvalue);
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
766 if (e.op == TOK.TOKint64 || e.op == TOK.TOKstring || e.op == TOK.TOKfloat64)
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
767 {
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
768 ei.exp = e; // no errors, keep result
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
769 }
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
770 ///version (DMDV2) {
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
771 else
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
772 {
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
773 /* Save scope for later use, to try again
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
774 */
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
775 scope_ = sc.clone();
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
776 scope_.setNoFree();
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
777 }
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
778 ///}
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
779 }
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
780 else
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
781 init = i2; // no errors, keep result
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
782 }
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
783 }
179
cd48cb899aee Updated to dmd2.040
korDen
parents: 178
diff changeset
784 sc = sc.pop();
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
785 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
786 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
787
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
788 override void semantic2(Scope sc)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
789 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
790 //printf("VarDeclaration.semantic2('%s')\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
791 if (init && !toParent().isFuncDeclaration())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
792 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
793 inuse++;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
794 static if (false) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
795 ExpInitializer ei = init.isExpInitializer();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
796 if (ei)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
797 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
798 ei.exp.dump(0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
799 printf("type = %p\n", ei.exp.type);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
800 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
801 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
802 init = init.semantic(sc, type);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
803 inuse--;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
804 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
805 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
806
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
807 override string kind()
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
808 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
809 return "variable";
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
810 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
811
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
812 override void toCBuffer(OutBuffer buf, HdrGenState* hgs)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
813 {
68
ee3a9f34dc48 final bits of codegen implementation to compile Phobos
korDen
parents: 53
diff changeset
814 StorageClassDeclaration.stcToCBuffer(buf, storage_class);
ee3a9f34dc48 final bits of codegen implementation to compile Phobos
korDen
parents: 53
diff changeset
815
ee3a9f34dc48 final bits of codegen implementation to compile Phobos
korDen
parents: 53
diff changeset
816 /* If changing, be sure and fix CompoundDeclarationStatement.toCBuffer()
ee3a9f34dc48 final bits of codegen implementation to compile Phobos
korDen
parents: 53
diff changeset
817 * too.
ee3a9f34dc48 final bits of codegen implementation to compile Phobos
korDen
parents: 53
diff changeset
818 */
ee3a9f34dc48 final bits of codegen implementation to compile Phobos
korDen
parents: 53
diff changeset
819 if (type)
ee3a9f34dc48 final bits of codegen implementation to compile Phobos
korDen
parents: 53
diff changeset
820 type.toCBuffer(buf, ident, hgs);
ee3a9f34dc48 final bits of codegen implementation to compile Phobos
korDen
parents: 53
diff changeset
821 else
ee3a9f34dc48 final bits of codegen implementation to compile Phobos
korDen
parents: 53
diff changeset
822 buf.writestring(ident.toChars());
ee3a9f34dc48 final bits of codegen implementation to compile Phobos
korDen
parents: 53
diff changeset
823 if (init)
ee3a9f34dc48 final bits of codegen implementation to compile Phobos
korDen
parents: 53
diff changeset
824 {
ee3a9f34dc48 final bits of codegen implementation to compile Phobos
korDen
parents: 53
diff changeset
825 buf.writestring(" = ");
ee3a9f34dc48 final bits of codegen implementation to compile Phobos
korDen
parents: 53
diff changeset
826 ///version (DMDV2) {
ee3a9f34dc48 final bits of codegen implementation to compile Phobos
korDen
parents: 53
diff changeset
827 ExpInitializer ie = init.isExpInitializer();
ee3a9f34dc48 final bits of codegen implementation to compile Phobos
korDen
parents: 53
diff changeset
828 if (ie && (ie.exp.op == TOKconstruct || ie.exp.op == TOKblit))
ee3a9f34dc48 final bits of codegen implementation to compile Phobos
korDen
parents: 53
diff changeset
829 (cast(AssignExp)ie.exp).e2.toCBuffer(buf, hgs);
ee3a9f34dc48 final bits of codegen implementation to compile Phobos
korDen
parents: 53
diff changeset
830 else
ee3a9f34dc48 final bits of codegen implementation to compile Phobos
korDen
parents: 53
diff changeset
831 ///}
ee3a9f34dc48 final bits of codegen implementation to compile Phobos
korDen
parents: 53
diff changeset
832 init.toCBuffer(buf, hgs);
ee3a9f34dc48 final bits of codegen implementation to compile Phobos
korDen
parents: 53
diff changeset
833 }
ee3a9f34dc48 final bits of codegen implementation to compile Phobos
korDen
parents: 53
diff changeset
834 buf.writeByte(';');
ee3a9f34dc48 final bits of codegen implementation to compile Phobos
korDen
parents: 53
diff changeset
835 buf.writenl();
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
836 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
837
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
838 version (_DH) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
839 Type htype;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
840 Initializer hinit;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
841 }
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
842 override bool needThis()
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
843 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
844 //printf("VarDeclaration.needThis(%s, x%x)\n", toChars(), storage_class);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
845 return (storage_class & STC.STCfield) != 0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
846 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
847
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
848 override bool isImportedSymbol()
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
849 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
850 if (protection == PROT.PROTexport && !init && (storage_class & STC.STCstatic || parent.isModule()))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
851 return true;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
852
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
853 return false;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
854 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
855
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
856 override bool isDataseg()
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
857 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
858 static if (false) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
859 printf("VarDeclaration.isDataseg(%p, '%s')\n", this, toChars());
135
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 130
diff changeset
860 printf("%llx, isModule: %p, isTemplateInstance: %p\n", storage_class & (STC.STCstatic | STC.STCconst), parent.isModule(), parent.isTemplateInstance());
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
861 printf("parent = '%s'\n", parent.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
862 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
863 if (storage_class & STC.STCmanifest)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
864 return false;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
865
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
866 Dsymbol parent = this.toParent();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
867 if (!parent && !(storage_class & STC.STCstatic))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
868 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
869 error("forward referenced");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
870 type = Type.terror;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
871 return false;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
872 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
873
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
874 return canTakeAddressOf() && (storage_class & (STC.STCstatic | STC.STCextern | STC.STCtls | STC.STCgshared) || toParent().isModule() || toParent().isTemplateInstance());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
875 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
876
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
877 override bool isThreadlocal()
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
878 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
879 //printf("VarDeclaration.isThreadlocal(%p, '%s')\n", this, toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
880 static if (false) { /// || TARGET_OSX
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
881 /* To be thread-local, must use the __thread storage class.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
882 * BUG: OSX doesn't support thread local yet.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
883 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
884 return isDataseg() && (storage_class & (STC.STCtls | STC.STCconst | STC.STCimmutable | STC.STCshared | STC.STCgshared)) == STC.STCtls;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
885 } else {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
886 /* Data defaults to being thread-local. It is not thread-local
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
887 * if it is immutable, const or shared.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
888 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
889 bool i = isDataseg() && !(storage_class & (STC.STCimmutable | STC.STCconst | STC.STCshared | STC.STCgshared));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
890 //printf("\treturn %d\n", i);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
891 return i;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
892 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
893 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
894
135
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 130
diff changeset
895 /********************************************
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 130
diff changeset
896 * Can variable be read and written by CTFE?
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 130
diff changeset
897 */
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 130
diff changeset
898
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 130
diff changeset
899 int isCTFE()
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 130
diff changeset
900 {
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 130
diff changeset
901 return (storage_class & STCctfe) || !isDataseg();
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 130
diff changeset
902 }
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 130
diff changeset
903
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
904 override bool hasPointers()
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
905 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
906 //printf("VarDeclaration.hasPointers() %s, ty = %d\n", toChars(), type.ty);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
907 return (!isDataseg() && type.hasPointers());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
908 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
909
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
910 version (DMDV2) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
911 bool canTakeAddressOf()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
912 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
913 static if (false) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
914 /* Global variables and struct/class fields of the form:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
915 * const int x = 3;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
916 * are not stored and hence cannot have their address taken.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
917 */
135
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 130
diff changeset
918 if ((isConst() || isImmutable()) && (storage_class & STC.STCinit) && (!(storage_class & (STC.STCstatic | STC.STCextern)) || (storage_class & STC.STCfield)) &&
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
919 (!parent || toParent().isModule() || toParent().isTemplateInstance()) && type.toBasetype().isTypeBasic())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
920 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
921 return false;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
922 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
923 } else {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
924 if (storage_class & STC.STCmanifest)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
925 return false;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
926 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
927 return true;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
928 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
929
12
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
930 /******************************************
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
931 * Return TRUE if variable needs to call the destructor.
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
932 */
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
933 bool needsAutoDtor()
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
934 {
12
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
935 //printf("VarDeclaration.needsAutoDtor() %s\n", toChars());
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
936
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
937 if (noauto || storage_class & STCnodtor)
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
938 return false;
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
939
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
940 // Destructors for structs and arrays of structs
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
941 Type tv = type.toBasetype();
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
942 while (tv.ty == Tsarray)
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
943 {
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
944 TypeSArray ta = cast(TypeSArray)tv;
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
945 tv = tv.nextOf().toBasetype();
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
946 }
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
947 if (tv.ty == Tstruct)
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
948 {
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
949 TypeStruct ts = cast(TypeStruct)tv;
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
950 StructDeclaration sd = ts.sym;
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
951 if (sd.dtor)
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
952 return true;
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
953 }
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
954
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
955 // Destructors for classes
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
956 if (storage_class & (STCauto | STCscope))
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
957 {
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
958 if (type.isClassHandle())
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
959 return true;
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
960 }
832f71e6f96c *Exp and *AssignExp arrayOp implementation added (might be a bit incomplete)
korDen
parents: 0
diff changeset
961 return false;
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
962 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
963 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
964
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
965 /******************************************
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
966 * If a variable has an auto destructor call, return call for it.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
967 * Otherwise, return null.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
968 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
969 Expression callAutoDtor(Scope sc)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
970 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
971 Expression e = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
972
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
973 //printf("VarDeclaration.callAutoDtor() %s\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
974
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
975 if (noauto || storage_class & STC.STCnodtor)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
976 return null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
977
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
978 // Destructors for structs and arrays of structs
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
979 bool array = false;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
980 Type tv = type.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
981 while (tv.ty == TY.Tsarray)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
982 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
983 TypeSArray ta = cast(TypeSArray)tv;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
984 array = true;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
985 tv = tv.nextOf().toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
986 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
987 if (tv.ty == TY.Tstruct)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
988 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
989 TypeStruct ts = cast(TypeStruct)tv;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
990 StructDeclaration sd = ts.sym;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
991 if (sd.dtor)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
992 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
993 if (array)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
994 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
995 // Typeinfo.destroy(cast(void*)&v);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
996 Expression ea = new SymOffExp(loc, this, 0, 0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
997 ea = new CastExp(loc, ea, Type.tvoid.pointerTo());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
998 Expressions args = new Expressions();
84
be2ab491772e Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 77
diff changeset
999 args.push(ea);
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1000
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1001 Expression et = type.getTypeInfo(sc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1002 et = new DotIdExp(loc, et, Id.destroy);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1003
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1004 e = new CallExp(loc, et, args);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1005 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1006 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1007 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1008 e = new VarExp(loc, this);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1009 e = new DotVarExp(loc, e, sd.dtor, 0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1010 e = new CallExp(loc, e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1011 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1012 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1013 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1014 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1015
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1016 // Destructors for classes
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1017 if (storage_class & (STC.STCauto | STC.STCscope))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1018 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1019 for (ClassDeclaration cd = type.isClassHandle(); cd; cd = cd.baseClass)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1020 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1021 /* We can do better if there's a way with onstack
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1022 * classes to determine if there's no way the monitor
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1023 * could be set.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1024 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1025 //if (cd.isInterfaceDeclaration())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1026 //error("interface %s cannot be scope", cd.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1027 if (1 || onstack || cd.dtors.dim) // if any destructors
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1028 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1029 // delete this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1030 Expression ec = new VarExp(loc, this);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1031 e = new DeleteExp(loc, ec);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1032 e.type = Type.tvoid;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1033 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1034 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1035 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1036 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1037 return e;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1038 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1039
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1040 /****************************
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1041 * Get ExpInitializer for a variable, if there is one.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1042 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1043 ExpInitializer getExpInitializer()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1044 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1045 ExpInitializer ei;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1046
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1047 if (init)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1048 ei = init.isExpInitializer();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1049 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1050 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1051 Expression e = type.defaultInit(loc);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1052 if (e)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1053 ei = new ExpInitializer(loc, e);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1054 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1055 ei = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1056 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1057 return ei;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1058 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1059
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1060 /*******************************************
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1061 * If variable has a constant expression initializer, get it.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1062 * Otherwise, return null.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1063 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1064 Expression getConstInitializer()
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1065 {
135
af1bebfd96a4 dmd 2.038
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 130
diff changeset
1066 if ((isConst() || isImmutable() || storage_class & STC.STCmanifest) && storage_class & STC.STCinit)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1067 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1068 ExpInitializer ei = getExpInitializer();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1069 if (ei)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1070 return ei.exp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1071 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1072
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1073 return null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1074 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1075
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
1076 override void checkCtorConstInit()
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1077 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1078 static if (false) { /* doesn't work if more than one static ctor */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1079 if (ctorinit == 0 && isCtorinit() && !(storage_class & STCfield))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1080 error("missing initializer in static constructor for const variable");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1081 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1082 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1083
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1084 /************************************
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1085 * Check to see if this variable is actually in an enclosing function
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1086 * rather than the current one.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1087 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1088 void checkNestedReference(Scope sc, Loc loc)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1089 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1090 if (parent && !isDataseg() && parent != sc.parent && !(storage_class & STC.STCmanifest))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1091 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1092 // The function that this variable is in
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1093 FuncDeclaration fdv = toParent().isFuncDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1094 // The current function
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1095 FuncDeclaration fdthis = sc.parent.isFuncDeclaration();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1096
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1097 if (fdv && fdthis && fdv !is fdthis)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1098 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1099 if (loc.filename)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1100 fdthis.getLevel(loc, fdv);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1101
84
be2ab491772e Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 77
diff changeset
1102 foreach (f; nestedrefs)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1103 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1104 if (f == fdthis)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1105 goto L1;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1106 }
74
7e0d548de9e6 Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 73
diff changeset
1107 nestedrefs.push(fdthis);
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1108 L1: ;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1109
84
be2ab491772e Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 77
diff changeset
1110 foreach (s; fdv.closureVars)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1111 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1112 if (s == this)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1113 goto L2;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1114 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1115
74
7e0d548de9e6 Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 73
diff changeset
1116 fdv.closureVars.push(this);
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1117 L2: ;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1118
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1119 //printf("fdthis is %s\n", fdthis.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1120 //printf("var %s in function %s is nested ref\n", toChars(), fdv.toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1121 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1122 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1123 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1124
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
1125 override Dsymbol toAlias()
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1126 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1127 //printf("VarDeclaration::toAlias('%s', this = %p, aliassym = %p)\n", toChars(), this, aliassym);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1128 assert(this !is aliassym);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1129 return aliassym ? aliassym.toAlias() : this;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1130 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1131
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
1132 override Symbol* toSymbol()
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1133 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1134 //printf("VarDeclaration.toSymbol(%s)\n", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1135 //if (needThis()) *(char*)0=0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1136 assert(!needThis());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1137 if (!csym)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1138 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1139 Symbol* s;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1140 TYPE* t;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1141 string id;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1142
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1143 if (isDataseg())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1144 id = mangle();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1145 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1146 id = ident.toChars();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1147
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1148 s = symbol_calloc(toStringz(id));
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1149
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1150 if (storage_class & (STC.STCout | STC.STCref))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1151 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1152 if (global.params.symdebug && storage_class & STC.STCparameter)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1153 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1154 t = type_alloc(TYM.TYnptr); // should be TYref, but problems in back end
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1155 t.Tnext = type.toCtype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1156 t.Tnext.Tcount++;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1157 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1158 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1159 t = type_fake(TYM.TYnptr);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1160 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1161 else if (storage_class & STC.STClazy)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1162 t = type_fake(TYM.TYdelegate); // Tdelegate as C type
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1163 else if (isParameter())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1164 t = type.toCParamtype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1165 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1166 t = type.toCtype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1167
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1168 t.Tcount++;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1169
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1170 if (isDataseg())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1171 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1172 if (isThreadlocal())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1173 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1174 /* Thread local storage
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1175 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1176 TYPE* ts = t;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1177 ts.Tcount++; // make sure a different t is allocated
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1178 type_setty(&t, t.Tty | mTY.mTYthread);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1179 ts.Tcount--;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1180
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1181 if (global.params.vtls)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1182 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1183 string p = loc.toChars();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1184 writef("%s: %s is thread local\n", p ? p : "", toChars());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1185 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1186 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1187
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1188 s.Sclass = SC.SCextern;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1189 s.Sfl = FL.FLextern;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1190 slist_add(s);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1191 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1192 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1193 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1194 s.Sclass = SC.SCauto;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1195 s.Sfl = FL.FLauto;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1196
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1197 if (nestedrefs.dim)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1198 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1199 /* Symbol is accessed by a nested function. Make sure
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1200 * it is not put in a register, and that the optimizer
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1201 * assumes it is modified across function calls and pointer
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1202 * dereferences.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1203 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1204 //printf("\tnested ref, not register\n");
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1205 type_setcv(&t, t.Tty | mTY.mTYvolatile);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1206 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1207 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1208
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1209 mangle_t m = 0;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1210 switch (linkage)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1211 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1212 case LINK.LINKwindows:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1213 m = mTYman.mTYman_std;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1214 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1215
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1216 case LINK.LINKpascal:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1217 m = mTYman.mTYman_pas;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1218 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1219
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1220 case LINK.LINKc:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1221 m = mTYman.mTYman_c;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1222 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1223
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1224 case LINK.LINKd:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1225 m = mTYman.mTYman_d;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1226 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1227
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1228 case LINK.LINKcpp:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1229 m = mTYman.mTYman_cpp;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1230 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1231
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1232 default:
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1233 writef("linkage = %d\n", linkage);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1234 assert(0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1235 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1236 type_setmangle(&t, m);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1237 s.Stype = t;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1238
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1239 csym = s;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1240 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1241 return csym;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1242 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1243
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
1244 override void toObjFile(int multiobj) // compile to .obj file
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1245 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1246 Symbol* s;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1247 uint sz;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1248 Dsymbol parent;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1249
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1250 //printf("VarDeclaration.toObjFile(%p '%s' type=%s) protection %d\n", this, toChars(), type.toChars(), protection);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1251 //printf("\talign = %d\n", type.alignsize());
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1252
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1253 if (aliassym)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1254 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1255 toAlias().toObjFile(0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1256 return;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1257 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1258
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1259 version (DMDV2) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1260 // Do not store variables we cannot take the address of
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1261 if (!canTakeAddressOf())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1262 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1263 return;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1264 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1265 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1266
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1267 if (isDataseg() && !(storage_class & STC.STCextern))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1268 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1269 s = toSymbol();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1270 sz = cast(uint)type.size();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1271
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1272 parent = this.toParent();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1273 /// version (DMDV1) { /* private statics should still get a global symbol, in case
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1274 /// * another module inlines a function that references it.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1275 /// */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1276 /// if (/*protection == PROT.PROTprivate ||*/
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1277 /// !parent || parent.ident == null || parent.isFuncDeclaration())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1278 /// {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1279 /// s.Sclass = SC.SCstatic;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1280 /// }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1281 /// else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1282 /// }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1283 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1284 if (storage_class & STC.STCcomdat)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1285 s.Sclass = SC.SCcomdat;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1286 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1287 s.Sclass = SC.SCglobal;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1288
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1289 do
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1290 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1291 /* Global template data members need to be in comdat's
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1292 * in case multiple .obj files instantiate the same
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1293 * template with the same types.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1294 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1295 if (parent.isTemplateInstance() && !parent.isTemplateMixin())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1296 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1297 version (DMDV1) {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1298 /* These symbol constants have already been copied,
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1299 * so no reason to output them.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1300 * Note that currently there is no way to take
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1301 * the address of such a const.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1302 */
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1303 if (isConst() && type.toBasetype().ty != TY.Tsarray && init && init.isExpInitializer())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1304 return;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1305 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1306 s.Sclass = SC.SCcomdat;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1307 break;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1308 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1309 parent = parent.parent;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1310 } while (parent);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1311 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1312
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1313 s.Sfl = FL.FLdata;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1314
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1315 if (init)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1316 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1317 s.Sdt = init.toDt();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1318
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1319 // Look for static array that is block initialized
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1320 Type tb;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1321 ExpInitializer ie = init.isExpInitializer();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1322
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1323 tb = type.toBasetype();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1324 if (tb.ty == TY.Tsarray && ie
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1325 && !tb.nextOf().equals(ie.exp.type.toBasetype().nextOf())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1326 && ie.exp.implicitConvTo(tb.nextOf()))
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1327 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1328 int dim = cast(int)(cast(TypeSArray)tb).dim.toInteger();
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1329
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1330 // Duplicate Sdt 'dim-1' times, as we already have the first one
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1331 while (--dim > 0)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1332 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1333 ie.exp.toDt(&s.Sdt);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1334 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1335 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1336 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1337 else if (storage_class & STC.STCextern)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1338 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1339 s.Sclass = SC.SCextern;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1340 s.Sfl = FL.FLextern;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1341 s.Sdt = null;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1342 // BUG: if isExport(), shouldn't we make it dllimport?
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1343 return;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1344 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1345 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1346 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1347 type.toDt(&s.Sdt);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1348 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1349 dt_optimize(s.Sdt);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1350
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1351 // See if we can convert a comdat to a comdef,
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1352 // which saves on exe file space.
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1353 if (s.Sclass == SC.SCcomdat &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1354 s.Sdt &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1355 s.Sdt.dt == DT.DT_azeros &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1356 s.Sdt.DTnext is null &&
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1357 !isThreadlocal())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1358 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1359 s.Sclass = SC.SCglobal;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1360 s.Sdt.dt = DT.DT_common;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1361 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1362
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1363 version (ELFOBJ_OR_MACHOBJ) { // Burton
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1364 if (s.Sdt && s.Sdt.dt == DT.DT_azeros && s.Sdt.DTnext is null)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1365 s.Sseg = Segment.UDATA;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1366 else
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1367 s.Sseg = Segment.DATA;
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1368 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1369 if (sz)
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1370 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1371 outdata(s);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1372 if (isExport())
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1373 obj_export(s, 0);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1374 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1375 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1376 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1377
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
1378 override int cvMember(ubyte* p)
0
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1379 {
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1380 assert(false);
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1381 }
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1382
10317f0c89a5 Initial commit
korDen
parents:
diff changeset
1383 // Eliminate need for dynamic_cast
72
2e2a5c3f943a reduced warnings by adding override to the methods
Trass3r
parents: 68
diff changeset
1384 override VarDeclaration isVarDeclaration() { return this; }
16
5c9b78899f5d Implemented methods for Tuples, fixed some linking issues.
Robert Clipsham <robert@octarineparrot.com>
parents: 0
diff changeset
1385 }
84
be2ab491772e Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 77
diff changeset
1386
be2ab491772e Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents: 77
diff changeset
1387 alias Vector!VarDeclaration VarDeclarations;