Mercurial > projects > ddmd
annotate dmd/PragmaDeclaration.d @ 116:352a5164f692
small bugfixes
author | Trass3r |
---|---|
date | Thu, 02 Sep 2010 02:49:29 +0200 |
parents | e28b18c23469 |
children | 60bb0fe4563e |
rev | line source |
---|---|
0 | 1 module dmd.PragmaDeclaration; |
2 | |
114 | 3 import dmd.common; |
0 | 4 import dmd.ArrayTypes; |
5 import dmd.AttribDeclaration; | |
6 import dmd.Loc; | |
7 import dmd.Identifier; | |
8 import dmd.StringExp; | |
9 import dmd.TOK; | |
10 import dmd.WANT; | |
11 import dmd.Global; | |
12 import dmd.Id; | |
13 import dmd.Array; | |
14 import dmd.Dsymbol; | |
15 import dmd.Scope; | |
16 import dmd.OutBuffer; | |
17 import dmd.HdrGenState; | |
18 import dmd.Expression; | |
19 import dmd.FuncDeclaration; | |
20 | |
21 import dmd.backend.Util; | |
22 import dmd.backend.Symbol; | |
23 | |
4 | 24 import core.memory; |
2 | 25 |
0 | 26 class PragmaDeclaration : AttribDeclaration |
27 { | |
28 Expressions args; // array of Expression's | |
29 | |
74
7e0d548de9e6
Switch Arrays of Dsymbols to the new templated Vector type
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
72
diff
changeset
|
30 this(Loc loc, Identifier ident, Expressions args, Dsymbols decl) |
0 | 31 { |
32 super(decl); | |
33 this.loc = loc; | |
34 this.ident = ident; | |
35 this.args = args; | |
36 } | |
37 | |
72 | 38 override Dsymbol syntaxCopy(Dsymbol s) |
0 | 39 { |
40 //printf("PragmaDeclaration.syntaxCopy(%s)\n", toChars()); | |
41 PragmaDeclaration pd; | |
42 | |
43 assert(!s); | |
44 pd = new PragmaDeclaration(loc, ident, Expression.arraySyntaxCopy(args), Dsymbol.arraySyntaxCopy(decl)); | |
45 return pd; | |
46 } | |
47 | |
72 | 48 override void semantic(Scope sc) |
0 | 49 { |
50 // Should be merged with PragmaStatement | |
51 | |
52 //printf("\tPragmaDeclaration.semantic '%s'\n",toChars()); | |
53 if (ident == Id.msg) | |
54 { | |
55 if (args) | |
56 { | |
90
39648eb578f6
more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
84
diff
changeset
|
57 foreach (e; args) |
0 | 58 { |
59 e = e.semantic(sc); | |
60 e = e.optimize(WANTvalue | WANTinterpret); | |
61 if (e.op == TOKstring) | |
62 { | |
90
39648eb578f6
more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
84
diff
changeset
|
63 auto se = cast(StringExp)e; |
116 | 64 writef("%s", se.toChars()[1..$-2] /*se.len, cast(char*)se.string_*/); |
0 | 65 } |
66 else | |
95 | 67 writef(e.toChars()); |
0 | 68 } |
69 writef("\n"); | |
70 } | |
71 goto Lnodecl; | |
72 } | |
73 else if (ident == Id.lib) | |
74 { | |
75 if (!args || args.dim != 1) | |
76 error("string expected for library name"); | |
77 else | |
78 { | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
77
diff
changeset
|
79 auto e = args[0]; |
0 | 80 |
81 e = e.semantic(sc); | |
82 e = e.optimize(WANTvalue | WANTinterpret); | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
77
diff
changeset
|
83 args[0] = e; |
0 | 84 if (e.op != TOKstring) |
85 error("string expected for library name, not '%s'", e.toChars()); | |
86 else if (global.params.verbose) | |
87 { | |
88 StringExp se = cast(StringExp)e; | |
89 writef("library %.*s\n", cast(int)se.len, cast(char*)se.string_); | |
90 } | |
91 } | |
92 goto Lnodecl; | |
93 } | |
94 /// version (IN_GCC) { | |
95 /// else if (ident == Id.GNU_asm) | |
96 /// { | |
97 /// if (! args || args.dim != 2) | |
98 /// error("identifier and string expected for asm name"); | |
99 /// else | |
100 /// { | |
101 /// Expression *e; | |
102 /// Declaration *d = null; | |
103 /// StringExp *s = null; | |
104 /// | |
105 /// e = (Expression *)args.data[0]; | |
106 /// e = e.semantic(sc); | |
107 /// if (e.op == TOKvar) | |
108 /// { | |
109 /// d = ((VarExp *)e).var; | |
110 /// if (! d.isFuncDeclaration() && ! d.isVarDeclaration()) | |
111 /// d = null; | |
112 /// } | |
113 /// if (!d) | |
114 /// error("first argument of GNU_asm must be a function or variable declaration"); | |
115 /// | |
116 /// e = (Expression *)args.data[1]; | |
117 /// e = e.semantic(sc); | |
118 /// e = e.optimize(WANTvalue); | |
119 /// if (e.op == TOKstring && ((StringExp *)e).sz == 1) | |
120 /// s = ((StringExp *)e); | |
121 /// else | |
122 /// error("second argument of GNU_asm must be a char string"); | |
123 /// | |
124 /// if (d && s) | |
125 /// d.c_ident = Lexer.idPool((char*) s.string); | |
126 /// } | |
127 /// goto Lnodecl; | |
128 /// } | |
129 /// } | |
130 else if (ident == Id.startaddress) | |
131 { | |
132 if (!args || args.dim != 1) | |
133 error("function name expected for start address"); | |
134 else | |
135 { | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
77
diff
changeset
|
136 auto e = args[0]; |
0 | 137 e = e.semantic(sc); |
138 e = e.optimize(WANTvalue | WANTinterpret); | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
77
diff
changeset
|
139 args[0] = e; |
0 | 140 Dsymbol sa = getDsymbol(e); |
141 if (!sa || !sa.isFuncDeclaration()) | |
142 error("function name expected for start address, not '%s'", e.toChars()); | |
143 } | |
144 goto Lnodecl; | |
145 } | |
146 /// version (TARGET_NET) { | |
147 /// else if (ident == Lexer.idPool("assembly")) | |
148 /// { | |
149 /// } | |
150 /// } // TARGET_NET | |
151 else if (global.params.ignoreUnsupportedPragmas) | |
152 { | |
153 if (global.params.verbose) | |
154 { | |
155 /* Print unrecognized pragmas | |
156 */ | |
157 writef("pragma %s", ident.toChars()); | |
158 if (args) | |
159 { | |
90
39648eb578f6
more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
84
diff
changeset
|
160 foreach (size_t i, Expression e; args) |
0 | 161 { |
162 e = e.semantic(sc); | |
163 e = e.optimize(WANTvalue | WANTinterpret); | |
164 if (i == 0) | |
165 writef(" ("); | |
166 else | |
167 writef(","); | |
168 writef("%s", e.toChars()); | |
169 } | |
170 if (args.dim) | |
171 writef(")"); | |
172 } | |
173 writef("\n"); | |
174 } | |
175 goto Lnodecl; | |
176 } | |
177 else | |
178 error("unrecognized pragma(%s)", ident.toChars()); | |
179 | |
180 if (decl) | |
181 { | |
77
ad4792a1cfd6
more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
74
diff
changeset
|
182 foreach(Dsymbol s; decl) |
0 | 183 s.semantic(sc); |
184 } | |
185 return; | |
186 | |
187 Lnodecl: | |
188 if (decl) | |
189 error("pragma is missing closing ';'"); | |
190 } | |
191 | |
72 | 192 override void setScope(Scope sc) |
0 | 193 { |
194 version (TARGET_NET) { | |
195 if (ident == Lexer.idPool("assembly")) | |
196 { | |
197 if (!args || args.dim != 1) | |
198 { | |
199 error("pragma has invalid number of arguments"); | |
200 } | |
201 else | |
202 { | |
203 Expression e = cast(Expression)args.data[0]; | |
204 e = e.semantic(sc); | |
205 e = e.optimize(WANTvalue | WANTinterpret); | |
206 args.data[0] = cast(void*)e; | |
207 if (e.op != TOKstring) | |
208 { | |
209 error("string expected, not '%s'", e.toChars()); | |
210 } | |
211 PragmaScope pragma_ = new PragmaScope(this, sc.parent, cast(StringExp)e); | |
212 | |
213 assert(sc); | |
214 pragma_.setScope(sc); | |
215 | |
216 //add to module members | |
217 assert(sc.module_); | |
218 assert(sc.module_.members); | |
219 sc.module_.members.push(cast(void*)pragma_); | |
220 } | |
221 } | |
222 } | |
223 } | |
224 | |
72 | 225 override bool oneMember(Dsymbol* ps) |
0 | 226 { |
227 assert(false); | |
228 } | |
229 | |
72 | 230 override void toCBuffer(OutBuffer buf, HdrGenState* hgs) |
0 | 231 { |
232 assert(false); | |
233 } | |
234 | |
72 | 235 override string kind() |
0 | 236 { |
237 assert(false); | |
238 } | |
239 | |
72 | 240 override void toObjFile(int multiobj) // compile to .obj file |
0 | 241 { |
242 if (ident == Id.lib) | |
243 { | |
244 assert(args && args.dim == 1); | |
245 | |
90
39648eb578f6
more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
84
diff
changeset
|
246 auto e = args[0]; |
0 | 247 |
248 assert(e.op == TOKstring); | |
249 | |
90
39648eb578f6
more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
84
diff
changeset
|
250 auto se = cast(StringExp)e; |
2 | 251 char* name = cast(char*)GC.malloc(se.len + 1); |
0 | 252 memcpy(name, se.string_, se.len); |
253 name[se.len] = 0; | |
254 version (OMFOBJ) { | |
255 /* The OMF format allows library names to be inserted | |
256 * into the object file. The linker will then automatically | |
257 * search that library, too. | |
258 */ | |
259 obj_includelib(name); | |
260 } else version (ELFOBJ_OR_MACHOBJ) { | |
261 /* The format does not allow embedded library names, | |
262 * so instead append the library name to the list to be passed | |
263 * to the linker. | |
264 */ | |
265 global.params.libfiles.push(cast(void*) name); | |
266 } else { | |
267 error("pragma lib not supported"); | |
268 } | |
269 } | |
270 /// version (DMDV2) { | |
271 else if (ident == Id.startaddress) | |
272 { | |
273 assert(args && args.dim == 1); | |
90
39648eb578f6
more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
84
diff
changeset
|
274 auto e = args[0]; |
0 | 275 Dsymbol sa = getDsymbol(e); |
276 FuncDeclaration f = sa.isFuncDeclaration(); | |
277 assert(f); | |
278 Symbol* s = f.toSymbol(); | |
279 obj_startaddress(s); | |
280 } | |
281 /// } | |
282 AttribDeclaration.toObjFile(multiobj); | |
283 } | |
22
fd4acc376c45
Implemented object file output and linking on linux.
Robert Clipsham <robert@octarineparrot.com>
parents:
4
diff
changeset
|
284 } |