comparison dmd/json.c @ 1594:9f7151d890ff

Added in forgotten json.c and json.h files so ldc compiles again.
author Robert Clipsham <robert@octarineparrot.com>
date Sat, 07 Nov 2009 14:21:56 +0000
parents
children
comparison
equal deleted inserted replaced
1593:b0dfdd5f6006 1594:9f7151d890ff
1
2 // Compiler implementation of the D programming language
3 // Copyright (c) 1999-2009 by Digital Mars
4 // All Rights Reserved
5 // written by Walter Bright
6 // http://www.digitalmars.com
7 // License for redistribution is by either the Artistic License
8 // in artistic.txt, or the GNU General Public License in gnu.txt.
9 // See the included readme.txt for details.
10
11 // This implements the JSON capability.
12
13 #include <stdio.h>
14 #include <string.h>
15 #include <time.h>
16 #include <ctype.h>
17 #include <assert.h>
18
19 #include "rmem.h"
20 #include "root.h"
21
22 #include "mars.h"
23 #include "dsymbol.h"
24 #include "macro.h"
25 #include "template.h"
26 #include "lexer.h"
27 #include "aggregate.h"
28 #include "declaration.h"
29 #include "enum.h"
30 #include "id.h"
31 #include "module.h"
32 #include "scope.h"
33 #include "hdrgen.h"
34 #include "json.h"
35 #include "mtype.h"
36 #include "attrib.h"
37 #include "cond.h"
38
39 const char Pname[] = "name";
40 const char Pkind[] = "kind";
41 const char Pfile[] = "file";
42 const char Pline[] = "line";
43 const char Ptype[] = "type";
44 const char Pcomment[] = "comment";
45 const char Pmembers[] = "members";
46
47 void JsonRemoveComma(OutBuffer *buf);
48
49 void json_generate(Array *modules)
50 { OutBuffer buf;
51
52 buf.writestring("[\n");
53 for (int i = 0; i < modules->dim; i++)
54 { Module *m = (Module *)modules->data[i];
55 if (global.params.verbose)
56 printf("json gen %s\n", m->toChars());
57 m->toJsonBuffer(&buf);
58 buf.writestring(",\n");
59 }
60 JsonRemoveComma(&buf);
61 buf.writestring("]\n");
62
63 // Write buf to file
64 char *arg = global.params.xfilename;
65 if (!arg || !*arg)
66 { // Generate lib file name from first obj name
67 char *n = (char *)global.params.objfiles->data[0];
68
69 n = FileName::name(n);
70 FileName *fn = FileName::forceExt(n, global.json_ext);
71 arg = fn->toChars();
72 }
73 else if (arg[0] == '-' && arg[1] == 0)
74 { // Write to stdout
75 fwrite(buf.data, 1, buf.offset, stdout);
76 return;
77 }
78 // if (!FileName::absolute(arg))
79 // arg = FileName::combine(dir, arg);
80 FileName *jsonfilename = FileName::defaultExt(arg, global.json_ext);
81 File *jsonfile = new File(jsonfilename);
82 assert(jsonfile);
83 jsonfile->setbuffer(buf.data, buf.offset);
84 jsonfile->ref = 1;
85 char *pt = FileName::path(jsonfile->toChars());
86 if (*pt)
87 FileName::ensurePathExists(pt);
88 mem.free(pt);
89 jsonfile->writev();
90 }
91
92
93 /*********************************
94 * Encode string into buf, and wrap it in double quotes.
95 */
96 void JsonString(OutBuffer *buf, const char *s)
97 {
98 buf->writeByte('\"');
99 for (; *s; s++)
100 {
101 unsigned char c = (unsigned char) *s;
102 switch (c)
103 {
104 case '\n':
105 buf->writestring("\\n");
106 break;
107
108 case '\r':
109 buf->writestring("\\r");
110 break;
111
112 case '\t':
113 buf->writestring("\\t");
114 break;
115
116 case '\"':
117 buf->writestring("\\\"");
118 break;
119
120 case '\\':
121 buf->writestring("\\\\");
122 break;
123
124 case '/':
125 buf->writestring("\\/");
126 break;
127
128 case '\b':
129 buf->writestring("\\b");
130 break;
131
132 case '\f':
133 buf->writestring("\\f");
134 break;
135
136 default:
137 if (c < 0x20)
138 buf->printf("\\u%04x", c);
139 else
140 // Note that UTF-8 chars pass through here just fine
141 buf->writeByte(c);
142 break;
143 }
144 }
145 buf->writeByte('\"');
146 }
147
148 void JsonProperty(OutBuffer *buf, const char *name, const char *value)
149 {
150 JsonString(buf, name);
151 buf->writestring(" : ");
152 JsonString(buf, value);
153 buf->writestring(",\n");
154 }
155
156 void JsonProperty(OutBuffer *buf, const char *name, int value)
157 {
158 JsonString(buf, name);
159 buf->writestring(" : ");
160 buf->printf("%d", value);
161 buf->writestring(",\n");
162 }
163
164 void JsonRemoveComma(OutBuffer *buf)
165 {
166 if (buf->offset >= 2 &&
167 buf->data[buf->offset - 2] == ',' &&
168 buf->data[buf->offset - 1] == '\n')
169 buf->offset -= 2;
170 }
171
172 void Dsymbol::toJsonBuffer(OutBuffer *buf)
173 {
174 }
175
176 void Module::toJsonBuffer(OutBuffer *buf)
177 {
178 buf->writestring("{\n");
179
180 if (md)
181 JsonProperty(buf, Pname, md->toChars());
182
183 JsonProperty(buf, Pkind, kind());
184
185 JsonProperty(buf, Pfile, srcfile->toChars());
186
187 if (comment)
188 JsonProperty(buf, Pcomment, (const char *)comment);
189
190 JsonString(buf, Pmembers);
191 buf->writestring(" : [\n");
192
193 size_t offset = buf->offset;
194 for (int i = 0; i < members->dim; i++)
195 { Dsymbol *s = (Dsymbol *)members->data[i];
196 if (offset != buf->offset)
197 { buf->writestring(",\n");
198 offset = buf->offset;
199 }
200 s->toJsonBuffer(buf);
201 }
202
203 JsonRemoveComma(buf);
204 buf->writestring("]\n");
205
206 buf->writestring("}\n");
207 }
208
209 void AttribDeclaration::toJsonBuffer(OutBuffer *buf)
210 {
211 //printf("AttribDeclaration::toJsonBuffer()\n");
212
213 Array *d = include(NULL, NULL);
214
215 if (d)
216 {
217 for (unsigned i = 0; i < d->dim; i++)
218 { Dsymbol *s = (Dsymbol *)d->data[i];
219 //printf("AttribDeclaration::toJsonBuffer %s\n", s->toChars());
220 s->toJsonBuffer(buf);
221 }
222 }
223 }
224
225
226 void ConditionalDeclaration::toJsonBuffer(OutBuffer *buf)
227 {
228 //printf("ConditionalDeclaration::toJsonBuffer()\n");
229 if (condition->inc)
230 {
231 AttribDeclaration::toJsonBuffer(buf);
232 }
233 }
234
235
236 void InvariantDeclaration::toJsonBuffer(OutBuffer *buf) { }
237 void DtorDeclaration::toJsonBuffer(OutBuffer *buf) { }
238 void StaticCtorDeclaration::toJsonBuffer(OutBuffer *buf) { }
239 void StaticDtorDeclaration::toJsonBuffer(OutBuffer *buf) { }
240 void ClassInfoDeclaration::toJsonBuffer(OutBuffer *buf) { }
241 void ModuleInfoDeclaration::toJsonBuffer(OutBuffer *buf) { }
242 void TypeInfoDeclaration::toJsonBuffer(OutBuffer *buf) { }
243 #if DMDV2
244 void PostBlitDeclaration::toJsonBuffer(OutBuffer *buf) { }
245 #endif
246
247 void Declaration::toJsonBuffer(OutBuffer *buf)
248 {
249 //printf("Declaration::toJsonBuffer()\n");
250 buf->writestring("{\n");
251
252 JsonProperty(buf, Pname, toChars());
253 JsonProperty(buf, Pkind, kind());
254 if (type)
255 JsonProperty(buf, Ptype, type->toChars());
256
257 if (comment)
258 JsonProperty(buf, Pcomment, (const char *)comment);
259
260 if (loc.linnum)
261 JsonProperty(buf, Pline, loc.linnum);
262
263 TypedefDeclaration *td = isTypedefDeclaration();
264 if (td)
265 {
266 JsonProperty(buf, "base", td->basetype->toChars());
267 }
268
269 JsonRemoveComma(buf);
270 buf->writestring("}\n");
271 }
272
273 void AggregateDeclaration::toJsonBuffer(OutBuffer *buf)
274 {
275 //printf("AggregateDeclaration::toJsonBuffer()\n");
276 buf->writestring("{\n");
277
278 JsonProperty(buf, Pname, toChars());
279 JsonProperty(buf, Pkind, kind());
280 if (comment)
281 JsonProperty(buf, Pcomment, (const char *)comment);
282 if (loc.linnum)
283 JsonProperty(buf, Pline, loc.linnum);
284
285 ClassDeclaration *cd = isClassDeclaration();
286 if (cd)
287 {
288 if (cd->baseClass)
289 {
290 JsonProperty(buf, "base", cd->baseClass->toChars());
291 }
292 if (cd->interfaces_dim)
293 {
294 JsonString(buf, "interfaces");
295 buf->writestring(" : [\n");
296 size_t offset = buf->offset;
297 for (int i = 0; i < cd->interfaces_dim; i++)
298 { BaseClass *b = cd->interfaces[i];
299 if (offset != buf->offset)
300 { buf->writestring(",\n");
301 offset = buf->offset;
302 }
303 JsonString(buf, b->base->toChars());
304 }
305 JsonRemoveComma(buf);
306 buf->writestring("],\n");
307 }
308 }
309
310 JsonString(buf, Pmembers);
311 buf->writestring(" : [\n");
312 size_t offset = buf->offset;
313 for (int i = 0; i < members->dim; i++)
314 { Dsymbol *s = (Dsymbol *)members->data[i];
315 if (offset != buf->offset)
316 { buf->writestring(",\n");
317 offset = buf->offset;
318 }
319 s->toJsonBuffer(buf);
320 }
321 JsonRemoveComma(buf);
322 buf->writestring("]\n");
323
324 buf->writestring("}\n");
325 }
326
327 void TemplateDeclaration::toJsonBuffer(OutBuffer *buf)
328 {
329 //printf("TemplateDeclaration::toJsonBuffer()\n");
330
331 buf->writestring("{\n");
332
333 JsonProperty(buf, Pname, toChars());
334 JsonProperty(buf, Pkind, kind());
335 if (comment)
336 JsonProperty(buf, Pcomment, (const char *)comment);
337
338 if (loc.linnum)
339 JsonProperty(buf, Pline, loc.linnum);
340
341 JsonString(buf, Pmembers);
342 buf->writestring(" : [\n");
343 size_t offset = buf->offset;
344 for (int i = 0; i < members->dim; i++)
345 { Dsymbol *s = (Dsymbol *)members->data[i];
346 if (offset != buf->offset)
347 { buf->writestring(",\n");
348 offset = buf->offset;
349 }
350 s->toJsonBuffer(buf);
351 }
352 JsonRemoveComma(buf);
353 buf->writestring("]\n");
354
355 buf->writestring("}\n");
356 }
357
358 void EnumDeclaration::toJsonBuffer(OutBuffer *buf)
359 {
360 //printf("EnumDeclaration::toJsonBuffer()\n");
361 if (isAnonymous())
362 {
363 if (members)
364 {
365 for (int i = 0; i < members->dim; i++)
366 {
367 Dsymbol *s = (Dsymbol *)members->data[i];
368 s->toJsonBuffer(buf);
369 buf->writestring(",\n");
370 }
371 JsonRemoveComma(buf);
372 }
373 return;
374 }
375
376 buf->writestring("{\n");
377
378 JsonProperty(buf, Pname, toChars());
379 JsonProperty(buf, Pkind, kind());
380 if (comment)
381 JsonProperty(buf, Pcomment, (const char *)comment);
382
383 if (loc.linnum)
384 JsonProperty(buf, Pline, loc.linnum);
385
386 if (memtype)
387 JsonProperty(buf, "base", memtype->toChars());
388
389 JsonString(buf, Pmembers);
390 buf->writestring(" : [\n");
391 size_t offset = buf->offset;
392 for (int i = 0; i < members->dim; i++)
393 { Dsymbol *s = (Dsymbol *)members->data[i];
394 if (offset != buf->offset)
395 { buf->writestring(",\n");
396 offset = buf->offset;
397 }
398 s->toJsonBuffer(buf);
399 }
400 JsonRemoveComma(buf);
401 buf->writestring("]\n");
402
403 buf->writestring("}\n");
404 }
405
406 void EnumMember::toJsonBuffer(OutBuffer *buf)
407 {
408 //printf("EnumMember::toJsonBuffer()\n");
409 buf->writestring("{\n");
410
411 JsonProperty(buf, Pname, toChars());
412 JsonProperty(buf, Pkind, kind());
413
414 if (comment)
415 JsonProperty(buf, Pcomment, (const char *)comment);
416
417 if (loc.linnum)
418 JsonProperty(buf, Pline, loc.linnum);
419
420 JsonRemoveComma(buf);
421 buf->writestring("}\n");
422 }
423
424