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