Mercurial > projects > ddmd
annotate dmd/PragmaDeclaration.d @ 130:60bb0fe4563e
dmdfe 2.037 first main iteration
author | Eldar Insafutdinov <e.insafutdinov@gmail.com> |
---|---|
date | Thu, 09 Sep 2010 22:51:44 +0100 |
parents | 352a5164f692 |
children | 9d194c848e3a |
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
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
116
diff
changeset
|
130 ///version(DMDV2) { |
0 | 131 else if (ident == Id.startaddress) |
132 { | |
133 if (!args || args.dim != 1) | |
134 error("function name expected for start address"); | |
135 else | |
136 { | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
77
diff
changeset
|
137 auto e = args[0]; |
0 | 138 e = e.semantic(sc); |
139 e = e.optimize(WANTvalue | WANTinterpret); | |
84
be2ab491772e
Expressions -> Vector!Expression
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
77
diff
changeset
|
140 args[0] = e; |
0 | 141 Dsymbol sa = getDsymbol(e); |
142 if (!sa || !sa.isFuncDeclaration()) | |
143 error("function name expected for start address, not '%s'", e.toChars()); | |
144 } | |
145 goto Lnodecl; | |
146 } | |
130
60bb0fe4563e
dmdfe 2.037 first main iteration
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
116
diff
changeset
|
147 ///} |
0 | 148 /// version (TARGET_NET) { |
149 /// else if (ident == Lexer.idPool("assembly")) | |
150 /// { | |
151 /// } | |
152 /// } // TARGET_NET | |
153 else if (global.params.ignoreUnsupportedPragmas) | |
154 { | |
155 if (global.params.verbose) | |
156 { | |
157 /* Print unrecognized pragmas | |
158 */ | |
159 writef("pragma %s", ident.toChars()); | |
160 if (args) | |
161 { | |
90
39648eb578f6
more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
84
diff
changeset
|
162 foreach (size_t i, Expression e; args) |
0 | 163 { |
164 e = e.semantic(sc); | |
165 e = e.optimize(WANTvalue | WANTinterpret); | |
166 if (i == 0) | |
167 writef(" ("); | |
168 else | |
169 writef(","); | |
170 writef("%s", e.toChars()); | |
171 } | |
172 if (args.dim) | |
173 writef(")"); | |
174 } | |
175 writef("\n"); | |
176 } | |
177 goto Lnodecl; | |
178 } | |
179 else | |
180 error("unrecognized pragma(%s)", ident.toChars()); | |
181 | |
182 if (decl) | |
183 { | |
77
ad4792a1cfd6
more D-ification container accessing
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
74
diff
changeset
|
184 foreach(Dsymbol s; decl) |
0 | 185 s.semantic(sc); |
186 } | |
187 return; | |
188 | |
189 Lnodecl: | |
190 if (decl) | |
191 error("pragma is missing closing ';'"); | |
192 } | |
193 | |
72 | 194 override void setScope(Scope sc) |
0 | 195 { |
196 version (TARGET_NET) { | |
197 if (ident == Lexer.idPool("assembly")) | |
198 { | |
199 if (!args || args.dim != 1) | |
200 { | |
201 error("pragma has invalid number of arguments"); | |
202 } | |
203 else | |
204 { | |
205 Expression e = cast(Expression)args.data[0]; | |
206 e = e.semantic(sc); | |
207 e = e.optimize(WANTvalue | WANTinterpret); | |
208 args.data[0] = cast(void*)e; | |
209 if (e.op != TOKstring) | |
210 { | |
211 error("string expected, not '%s'", e.toChars()); | |
212 } | |
213 PragmaScope pragma_ = new PragmaScope(this, sc.parent, cast(StringExp)e); | |
214 | |
215 assert(sc); | |
216 pragma_.setScope(sc); | |
217 | |
218 //add to module members | |
219 assert(sc.module_); | |
220 assert(sc.module_.members); | |
221 sc.module_.members.push(cast(void*)pragma_); | |
222 } | |
223 } | |
224 } | |
225 } | |
226 | |
72 | 227 override bool oneMember(Dsymbol* ps) |
0 | 228 { |
229 assert(false); | |
230 } | |
231 | |
72 | 232 override void toCBuffer(OutBuffer buf, HdrGenState* hgs) |
0 | 233 { |
234 assert(false); | |
235 } | |
236 | |
72 | 237 override string kind() |
0 | 238 { |
239 assert(false); | |
240 } | |
241 | |
72 | 242 override void toObjFile(int multiobj) // compile to .obj file |
0 | 243 { |
244 if (ident == Id.lib) | |
245 { | |
246 assert(args && args.dim == 1); | |
247 | |
90
39648eb578f6
more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
84
diff
changeset
|
248 auto e = args[0]; |
0 | 249 |
250 assert(e.op == TOKstring); | |
251 | |
90
39648eb578f6
more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
84
diff
changeset
|
252 auto se = cast(StringExp)e; |
2 | 253 char* name = cast(char*)GC.malloc(se.len + 1); |
0 | 254 memcpy(name, se.string_, se.len); |
255 name[se.len] = 0; | |
256 version (OMFOBJ) { | |
257 /* The OMF format allows library names to be inserted | |
258 * into the object file. The linker will then automatically | |
259 * search that library, too. | |
260 */ | |
261 obj_includelib(name); | |
262 } else version (ELFOBJ_OR_MACHOBJ) { | |
263 /* The format does not allow embedded library names, | |
264 * so instead append the library name to the list to be passed | |
265 * to the linker. | |
266 */ | |
267 global.params.libfiles.push(cast(void*) name); | |
268 } else { | |
269 error("pragma lib not supported"); | |
270 } | |
271 } | |
272 /// version (DMDV2) { | |
273 else if (ident == Id.startaddress) | |
274 { | |
275 assert(args && args.dim == 1); | |
90
39648eb578f6
more Expressions work
Eldar Insafutdinov <e.insafutdinov@gmail.com>
parents:
84
diff
changeset
|
276 auto e = args[0]; |
0 | 277 Dsymbol sa = getDsymbol(e); |
278 FuncDeclaration f = sa.isFuncDeclaration(); | |
279 assert(f); | |
280 Symbol* s = f.toSymbol(); | |
281 obj_startaddress(s); | |
282 } | |
283 /// } | |
284 AttribDeclaration.toObjFile(multiobj); | |
285 } | |
22
fd4acc376c45
Implemented object file output and linking on linux.
Robert Clipsham <robert@octarineparrot.com>
parents:
4
diff
changeset
|
286 } |