| 1 |
/** |
| 2 |
* Code for generating .json descriptions of the module when passing the `-X` flag to dmd. |
| 3 |
* |
| 4 |
* Copyright: Copyright (C) 1999-2025 by The D Language Foundation, All Rights Reserved |
| 5 |
* Authors: $(LINK2 https://www.digitalmars.com, Walter Bright) |
| 6 |
* License: $(LINK2 https://www.boost.org/LICENSE_1_0.txt, Boost License 1.0) |
| 7 |
* Source: $(LINK2 https://github.com/dlang/dmd/blob/master/compiler/src/dmd/json.d, _json.d) |
| 8 |
* Documentation: https://dlang.org/phobos/dmd_json.html |
| 9 |
* Coverage: https://codecov.io/gh/dlang/dmd/src/master/compiler/src/dmd/json.d |
| 10 |
*/ |
| 11 |
|
| 12 |
module dmd.json; |
| 13 |
|
| 14 |
import core.stdc.stdio; |
| 15 |
import core.stdc.string; |
| 16 |
import dmd.aggregate; |
| 17 |
import dmd.arraytypes; |
| 18 |
import dmd.astenums; |
| 19 |
import dmd.attrib; |
| 20 |
import dmd.cond; |
| 21 |
import dmd.dclass; |
| 22 |
import dmd.declaration; |
| 23 |
import dmd.denum; |
| 24 |
import dmd.dimport; |
| 25 |
import dmd.dmodule; |
| 26 |
import dmd.dsymbol; |
| 27 |
import dmd.dsymbolsem : include; |
| 28 |
import dmd.dtemplate; |
| 29 |
import dmd.errors; |
| 30 |
import dmd.expression; |
| 31 |
import dmd.func; |
| 32 |
import dmd.globals; |
| 33 |
import dmd.hdrgen; |
| 34 |
import dmd.id; |
| 35 |
import dmd.identifier; |
| 36 |
import dmd.location; |
| 37 |
import dmd.mtype; |
| 38 |
import dmd.common.outbuffer; |
| 39 |
import dmd.rootobject; |
| 40 |
import dmd.root.string; |
| 41 |
import dmd.target; |
| 42 |
import dmd.visitor; |
| 43 |
|
| 44 |
version(Windows) |
| 45 |
{ |
| 46 |
extern (C) char* _getcwd(char* buffer, size_t maxlen); |
| 47 |
alias getcwd = _getcwd; |
| 48 |
} |
| 49 |
else |
| 50 |
{ |
| 51 |
import core.sys.posix.unistd : getcwd; |
| 52 |
} |
| 53 |
|
| 54 |
private extern (C++) final class ToJsonVisitor : Visitor |
| 55 |
{ |
| 56 |
alias visit = Visitor.visit; |
| 57 |
public: |
| 58 |
OutBuffer* buf; |
| 59 |
int indentLevel; |
| 60 |
const(char)[] filename; |
| 61 |
|
| 62 |
extern (D) this(OutBuffer* buf) scope @safe |
| 63 |
{ |
| 64 |
this.buf = buf; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
void indent() |
| 69 |
{ |
| 70 |
if (buf.length >= 1 && (*buf)[buf.length - 1] == '\n') |
| 71 |
for (int i = 0; i < indentLevel; i++) |
| 72 |
buf.writeByte(' '); |
| 73 |
} |
| 74 |
|
| 75 |
void removeComma() |
| 76 |
{ |
| 77 |
if (buf.length >= 2 && (*buf)[buf.length - 2] == ',' && ((*buf)[buf.length - 1] == '\n' || (*buf)[buf.length - 1] == ' ')) |
| 78 |
buf.setsize(buf.length - 2); |
| 79 |
} |
| 80 |
|
| 81 |
void comma() |
| 82 |
{ |
| 83 |
if (indentLevel > 0) |
| 84 |
buf.writestring(",\n"); |
| 85 |
} |
| 86 |
|
| 87 |
void stringStart() |
| 88 |
{ |
| 89 |
buf.writeByte('\"'); |
| 90 |
} |
| 91 |
|
| 92 |
void stringEnd() |
| 93 |
{ |
| 94 |
buf.writeByte('\"'); |
| 95 |
} |
| 96 |
|
| 97 |
extern(D) void stringPart(const char[] s) |
| 98 |
{ |
| 99 |
foreach (char c; s) |
| 100 |
{ |
| 101 |
switch (c) |
| 102 |
{ |
| 103 |
case '\n': |
| 104 |
buf.writestring("\\n"); |
| 105 |
break; |
| 106 |
case '\r': |
| 107 |
buf.writestring("\\r"); |
| 108 |
break; |
| 109 |
case '\t': |
| 110 |
buf.writestring("\\t"); |
| 111 |
break; |
| 112 |
case '\"': |
| 113 |
buf.writestring("\\\""); |
| 114 |
break; |
| 115 |
case '\\': |
| 116 |
buf.writestring("\\\\"); |
| 117 |
break; |
| 118 |
case '\b': |
| 119 |
buf.writestring("\\b"); |
| 120 |
break; |
| 121 |
case '\f': |
| 122 |
buf.writestring("\\f"); |
| 123 |
break; |
| 124 |
default: |
| 125 |
if (c < 0x20) |
| 126 |
buf.printf("\\u%04x", c); |
| 127 |
else |
| 128 |
{ |
| 129 |
// Note that UTF-8 chars pass through here just fine |
| 130 |
buf.writeByte(c); |
| 131 |
} |
| 132 |
break; |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
// Json value functions |
| 138 |
/********************************* |
| 139 |
* Encode string into buf, and wrap it in double quotes. |
| 140 |
*/ |
| 141 |
extern(D) void value(const char[] s) |
| 142 |
{ |
| 143 |
stringStart(); |
| 144 |
stringPart(s); |
| 145 |
stringEnd(); |
| 146 |
} |
| 147 |
|
| 148 |
void value(int value) |
| 149 |
{ |
| 150 |
if (value < 0) |
| 151 |
{ |
| 152 |
buf.writeByte('-'); |
| 153 |
value = -value; |
| 154 |
} |
| 155 |
buf.print(value); |
| 156 |
} |
| 157 |
|
| 158 |
void valueBool(bool value) |
| 159 |
{ |
| 160 |
buf.writestring(value ? "true" : "false"); |
| 161 |
} |
| 162 |
|
| 163 |
/********************************* |
| 164 |
* Item is an intented value and a comma, for use in arrays |
| 165 |
*/ |
| 166 |
extern(D) void item(const char[] s) |
| 167 |
{ |
| 168 |
indent(); |
| 169 |
value(s); |
| 170 |
comma(); |
| 171 |
} |
| 172 |
|
| 173 |
void item(int i) |
| 174 |
{ |
| 175 |
indent(); |
| 176 |
value(i); |
| 177 |
comma(); |
| 178 |
} |
| 179 |
|
| 180 |
void itemBool(const bool b) |
| 181 |
{ |
| 182 |
indent(); |
| 183 |
valueBool(b); |
| 184 |
comma(); |
| 185 |
} |
| 186 |
|
| 187 |
// Json array functions |
| 188 |
void arrayStart() |
| 189 |
{ |
| 190 |
indent(); |
| 191 |
buf.writestring("[\n"); |
| 192 |
indentLevel++; |
| 193 |
} |
| 194 |
|
| 195 |
void arrayEnd() |
| 196 |
{ |
| 197 |
indentLevel--; |
| 198 |
removeComma(); |
| 199 |
if (buf.length >= 2 && (*buf)[buf.length - 2] == '[' && (*buf)[buf.length - 1] == '\n') |
| 200 |
buf.setsize(buf.length - 1); |
| 201 |
else if (!(buf.length >= 1 && (*buf)[buf.length - 1] == '[')) |
| 202 |
{ |
| 203 |
buf.writestring("\n"); |
| 204 |
indent(); |
| 205 |
} |
| 206 |
buf.writestring("]"); |
| 207 |
comma(); |
| 208 |
} |
| 209 |
|
| 210 |
// Json object functions |
| 211 |
void objectStart() |
| 212 |
{ |
| 213 |
indent(); |
| 214 |
buf.writestring("{\n"); |
| 215 |
indentLevel++; |
| 216 |
} |
| 217 |
|
| 218 |
void objectEnd() |
| 219 |
{ |
| 220 |
indentLevel--; |
| 221 |
removeComma(); |
| 222 |
if (buf.length >= 2 && (*buf)[buf.length - 2] == '{' && (*buf)[buf.length - 1] == '\n') |
| 223 |
buf.setsize(buf.length - 1); |
| 224 |
else |
| 225 |
{ |
| 226 |
buf.writestring("\n"); |
| 227 |
indent(); |
| 228 |
} |
| 229 |
buf.writestring("}"); |
| 230 |
comma(); |
| 231 |
} |
| 232 |
|
| 233 |
// Json object property functions |
| 234 |
extern(D) void propertyStart(const char[] name) |
| 235 |
{ |
| 236 |
indent(); |
| 237 |
value(name); |
| 238 |
buf.writestring(" : "); |
| 239 |
} |
| 240 |
|
| 241 |
/** |
| 242 |
Write the given string object property only if `s` is not null. |
| 243 |
|
| 244 |
Params: |
| 245 |
name = the name of the object property |
| 246 |
s = the string value of the object property |
| 247 |
*/ |
| 248 |
extern(D) void property(const char[] name, const char[] s) |
| 249 |
{ |
| 250 |
if (s is null) |
| 251 |
return; |
| 252 |
propertyStart(name); |
| 253 |
value(s); |
| 254 |
comma(); |
| 255 |
} |
| 256 |
|
| 257 |
/** |
| 258 |
Write the given string object property. |
| 259 |
|
| 260 |
Params: |
| 261 |
name = the name of the object property |
| 262 |
s = the string value of the object property |
| 263 |
*/ |
| 264 |
extern(D) void requiredProperty(const char[] name, const char[] s) |
| 265 |
{ |
| 266 |
propertyStart(name); |
| 267 |
if (s is null) |
| 268 |
buf.writestring("null"); |
| 269 |
else |
| 270 |
value(s); |
| 271 |
comma(); |
| 272 |
} |
| 273 |
|
| 274 |
extern(D) void property(const char[] name, int i) |
| 275 |
{ |
| 276 |
propertyStart(name); |
| 277 |
value(i); |
| 278 |
comma(); |
| 279 |
} |
| 280 |
|
| 281 |
extern(D) void propertyBool(const char[] name, const bool b) |
| 282 |
{ |
| 283 |
propertyStart(name); |
| 284 |
valueBool(b); |
| 285 |
comma(); |
| 286 |
} |
| 287 |
|
| 288 |
extern(D) void property(const char[] name, TRUST trust) |
| 289 |
{ |
| 290 |
final switch (trust) |
| 291 |
{ |
| 292 |
case TRUST.default_: |
| 293 |
// Should not be printed |
| 294 |
//property(name, "default"); |
| 295 |
break; |
| 296 |
case TRUST.system: return property(name, "system"); |
| 297 |
case TRUST.trusted: return property(name, "trusted"); |
| 298 |
case TRUST.safe: return property(name, "safe"); |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
extern(D) void property(const char[] name, PURE purity) |
| 303 |
{ |
| 304 |
final switch (purity) |
| 305 |
{ |
| 306 |
case PURE.impure: |
| 307 |
// Should not be printed |
| 308 |
//property(name, "impure"); |
| 309 |
break; |
| 310 |
case PURE.weak: return property(name, "weak"); |
| 311 |
case PURE.const_: return property(name, "strong"); |
| 312 |
case PURE.fwdref: return property(name, "fwdref"); |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
extern(D) void property(const char[] name, const LINK linkage) |
| 317 |
{ |
| 318 |
final switch (linkage) |
| 319 |
{ |
| 320 |
case LINK.default_: |
| 321 |
// Should not be printed |
| 322 |
//property(name, "default"); |
| 323 |
break; |
| 324 |
case LINK.d: |
| 325 |
// Should not be printed |
| 326 |
//property(name, "d"); |
| 327 |
break; |
| 328 |
case LINK.system: return property(name, "system"); |
| 329 |
case LINK.c: return property(name, "c"); |
| 330 |
case LINK.cpp: return property(name, "cpp"); |
| 331 |
case LINK.windows: return property(name, "windows"); |
| 332 |
case LINK.objc: return property(name, "objc"); |
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
extern(D) void propertyStorageClass(const char[] name, STC stc) |
| 337 |
{ |
| 338 |
stc &= STC.visibleStorageClasses; |
| 339 |
if (stc) |
| 340 |
{ |
| 341 |
propertyStart(name); |
| 342 |
arrayStart(); |
| 343 |
while (stc) |
| 344 |
{ |
| 345 |
auto p = stcToString(stc); |
| 346 |
assert(p.length); |
| 347 |
item(p); |
| 348 |
} |
| 349 |
arrayEnd(); |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
extern(D) void property(const char[] linename, const char[] charname, Loc loc) |
| 354 |
{ |
| 355 |
if (loc.isValid()) |
| 356 |
{ |
| 357 |
SourceLoc sl = SourceLoc(loc); |
| 358 |
if (sl.filename.length > 0 && sl.filename != this.filename) |
| 359 |
{ |
| 360 |
this.filename = sl.filename; |
| 361 |
property("file", sl.filename); |
| 362 |
} |
| 363 |
|
| 364 |
if (sl.linnum) |
| 365 |
{ |
| 366 |
property(linename, sl.linnum); |
| 367 |
if (sl.charnum) |
| 368 |
property(charname, sl.charnum); |
| 369 |
} |
| 370 |
} |
| 371 |
} |
| 372 |
|
| 373 |
extern(D) void property(const char[] name, Type type) |
| 374 |
{ |
| 375 |
if (type) |
| 376 |
{ |
| 377 |
property(name, type.toString()); |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
extern(D) void property(const char[] name, const char[] deconame, Type type) |
| 382 |
{ |
| 383 |
if (type) |
| 384 |
{ |
| 385 |
if (type.deco) |
| 386 |
property(deconame, type.deco.toDString); |
| 387 |
else |
| 388 |
property(name, type.toString()); |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
extern(D) void property(const char[] name, Parameters* parameters) |
| 393 |
{ |
| 394 |
if (parameters is null || parameters.length == 0) |
| 395 |
return; |
| 396 |
propertyStart(name); |
| 397 |
arrayStart(); |
| 398 |
if (parameters) |
| 399 |
{ |
| 400 |
for (size_t i = 0; i < parameters.length; i++) |
| 401 |
{ |
| 402 |
Parameter p = (*parameters)[i]; |
| 403 |
objectStart(); |
| 404 |
if (p.ident) |
| 405 |
property("name", p.ident.toString()); |
| 406 |
property("type", "deco", p.type); |
| 407 |
propertyStorageClass("storageClass", p.storageClass); |
| 408 |
if (p.defaultArg) |
| 409 |
property("default", p.defaultArg.toString()); |
| 410 |
objectEnd(); |
| 411 |
} |
| 412 |
} |
| 413 |
arrayEnd(); |
| 414 |
} |
| 415 |
|
| 416 |
/* ========================================================================== */ |
| 417 |
void jsonProperties(Dsymbol s) |
| 418 |
{ |
| 419 |
if (s.isModule()) |
| 420 |
return; |
| 421 |
if (!s.isTemplateDeclaration()) // TemplateDeclaration::kind() acts weird sometimes |
| 422 |
{ |
| 423 |
property("name", s.toString()); |
| 424 |
if (s.isStaticCtorDeclaration()) |
| 425 |
{ |
| 426 |
property("kind", s.isSharedStaticCtorDeclaration() |
| 427 |
? "shared static constructor" : "static constructor"); |
| 428 |
} |
| 429 |
else if (s.isStaticDtorDeclaration()) |
| 430 |
{ |
| 431 |
property("kind", s.isSharedStaticDtorDeclaration() |
| 432 |
? "shared static destructor" : "static destructor"); |
| 433 |
} |
| 434 |
else |
| 435 |
property("kind", s.kind.toDString); |
| 436 |
} |
| 437 |
// TODO: How about package(names)? |
| 438 |
property("protection", visibilityToString(s.visible().kind)); |
| 439 |
if (EnumMember em = s.isEnumMember()) |
| 440 |
{ |
| 441 |
if (em.origValue) |
| 442 |
property("value", em.origValue.toString()); |
| 443 |
} |
| 444 |
property("comment", s.comment.toDString); |
| 445 |
property("line", "char", s.loc); |
| 446 |
} |
| 447 |
|
| 448 |
void jsonProperties(Declaration d) |
| 449 |
{ |
| 450 |
if (d.storage_class & STC.local) |
| 451 |
return; |
| 452 |
jsonProperties(cast(Dsymbol)d); |
| 453 |
propertyStorageClass("storageClass", d.storage_class); |
| 454 |
property("linkage", d._linkage); |
| 455 |
property("type", "deco", d.type); |
| 456 |
// Emit originalType if it differs from type |
| 457 |
if (d.type != d.originalType && d.originalType) |
| 458 |
{ |
| 459 |
auto ostr = d.originalType.toString(); |
| 460 |
if (d.type) |
| 461 |
{ |
| 462 |
auto tstr = d.type.toString(); |
| 463 |
if (ostr != tstr) |
| 464 |
{ |
| 465 |
//printf("tstr = %s, ostr = %s\n", tstr, ostr); |
| 466 |
property("originalType", ostr); |
| 467 |
} |
| 468 |
} |
| 469 |
else |
| 470 |
property("originalType", ostr); |
| 471 |
} |
| 472 |
} |
| 473 |
|
| 474 |
void jsonProperties(TemplateDeclaration td) |
| 475 |
{ |
| 476 |
jsonProperties(cast(Dsymbol)td); |
| 477 |
if (td.onemember && td.onemember.isCtorDeclaration()) |
| 478 |
property("name", "this"); // __ctor -> this |
| 479 |
else |
| 480 |
property("name", td.ident.toString()); // Foo(T) -> Foo |
| 481 |
} |
| 482 |
|
| 483 |
/* ========================================================================== */ |
| 484 |
override void visit(Dsymbol s) |
| 485 |
{ |
| 486 |
} |
| 487 |
|
| 488 |
override void visit(Module s) |
| 489 |
{ |
| 490 |
objectStart(); |
| 491 |
if (s.md) |
| 492 |
property("name", s.md.toString()); |
| 493 |
property("kind", s.kind.toDString); |
| 494 |
filename = s.srcfile.toString(); |
| 495 |
property("file", filename); |
| 496 |
property("comment", s.comment.toDString); |
| 497 |
propertyStart("members"); |
| 498 |
arrayStart(); |
| 499 |
for (size_t i = 0; i < s.members.length; i++) |
| 500 |
{ |
| 501 |
(*s.members)[i].accept(this); |
| 502 |
} |
| 503 |
arrayEnd(); |
| 504 |
objectEnd(); |
| 505 |
} |
| 506 |
|
| 507 |
override void visit(Import s) |
| 508 |
{ |
| 509 |
if (s.id == Id.object) |
| 510 |
return; |
| 511 |
objectStart(); |
| 512 |
propertyStart("name"); |
| 513 |
stringStart(); |
| 514 |
foreach (const pid; s.packages){ |
| 515 |
stringPart(pid.toString()); |
| 516 |
buf.writeByte('.'); |
| 517 |
} |
| 518 |
stringPart(s.id.toString()); |
| 519 |
stringEnd(); |
| 520 |
comma(); |
| 521 |
property("kind", s.kind.toDString); |
| 522 |
property("comment", s.comment.toDString); |
| 523 |
property("line", "char", s.loc); |
| 524 |
if (s.visible().kind != Visibility.Kind.public_) |
| 525 |
property("protection", visibilityToString(s.visible().kind)); |
| 526 |
if (s.aliasId) |
| 527 |
property("alias", s.aliasId.toString()); |
| 528 |
bool hasRenamed = false; |
| 529 |
bool hasSelective = false; |
| 530 |
for (size_t i = 0; i < s.aliases.length; i++) |
| 531 |
{ |
| 532 |
// avoid empty "renamed" and "selective" sections |
| 533 |
if (hasRenamed && hasSelective) |
| 534 |
break; |
| 535 |
else if (s.aliases[i]) |
| 536 |
hasRenamed = true; |
| 537 |
else |
| 538 |
hasSelective = true; |
| 539 |
} |
| 540 |
if (hasRenamed) |
| 541 |
{ |
| 542 |
// import foo : alias1 = target1; |
| 543 |
propertyStart("renamed"); |
| 544 |
objectStart(); |
| 545 |
for (size_t i = 0; i < s.aliases.length; i++) |
| 546 |
{ |
| 547 |
const name = s.names[i]; |
| 548 |
const _alias = s.aliases[i]; |
| 549 |
if (_alias) |
| 550 |
property(_alias.toString(), name.toString()); |
| 551 |
} |
| 552 |
objectEnd(); |
| 553 |
} |
| 554 |
if (hasSelective) |
| 555 |
{ |
| 556 |
// import foo : target1; |
| 557 |
propertyStart("selective"); |
| 558 |
arrayStart(); |
| 559 |
foreach (i, name; s.names) |
| 560 |
{ |
| 561 |
if (!s.aliases[i]) |
| 562 |
item(name.toString()); |
| 563 |
} |
| 564 |
arrayEnd(); |
| 565 |
} |
| 566 |
objectEnd(); |
| 567 |
} |
| 568 |
|
| 569 |
override void visit(AttribDeclaration d) |
| 570 |
{ |
| 571 |
Dsymbols* ds = d.include(null); |
| 572 |
if (ds) |
| 573 |
{ |
| 574 |
for (size_t i = 0; i < ds.length; i++) |
| 575 |
{ |
| 576 |
Dsymbol s = (*ds)[i]; |
| 577 |
s.accept(this); |
| 578 |
} |
| 579 |
} |
| 580 |
} |
| 581 |
|
| 582 |
override void visit(ConditionalDeclaration d) |
| 583 |
{ |
| 584 |
if (d.condition.inc != Include.notComputed) |
| 585 |
{ |
| 586 |
visit(cast(AttribDeclaration)d); |
| 587 |
return; // Don't visit the if/else bodies again below |
| 588 |
} |
| 589 |
Dsymbols* ds = d.decl ? d.decl : d.elsedecl; |
| 590 |
for (size_t i = 0; i < ds.length; i++) |
| 591 |
{ |
| 592 |
Dsymbol s = (*ds)[i]; |
| 593 |
s.accept(this); |
| 594 |
} |
| 595 |
} |
| 596 |
|
| 597 |
override void visit(TypeInfoDeclaration d) |
| 598 |
{ |
| 599 |
} |
| 600 |
|
| 601 |
override void visit(PostBlitDeclaration d) |
| 602 |
{ |
| 603 |
} |
| 604 |
|
| 605 |
override void visit(Declaration d) |
| 606 |
{ |
| 607 |
objectStart(); |
| 608 |
//property("unknown", "declaration"); |
| 609 |
jsonProperties(d); |
| 610 |
objectEnd(); |
| 611 |
} |
| 612 |
|
| 613 |
override void visit(AggregateDeclaration d) |
| 614 |
{ |
| 615 |
objectStart(); |
| 616 |
jsonProperties(d); |
| 617 |
ClassDeclaration cd = d.isClassDeclaration(); |
| 618 |
if (cd) |
| 619 |
{ |
| 620 |
if (cd.baseClass && cd.baseClass.ident != Id.Object) |
| 621 |
{ |
| 622 |
property("base", cd.baseClass.toPrettyChars(true).toDString); |
| 623 |
} |
| 624 |
if (cd.interfaces.length) |
| 625 |
{ |
| 626 |
propertyStart("interfaces"); |
| 627 |
arrayStart(); |
| 628 |
foreach (b; cd.interfaces) |
| 629 |
{ |
| 630 |
item(b.sym.toPrettyChars(true).toDString); |
| 631 |
} |
| 632 |
arrayEnd(); |
| 633 |
} |
| 634 |
} |
| 635 |
if (d.members) |
| 636 |
{ |
| 637 |
propertyStart("members"); |
| 638 |
arrayStart(); |
| 639 |
for (size_t i = 0; i < d.members.length; i++) |
| 640 |
{ |
| 641 |
Dsymbol s = (*d.members)[i]; |
| 642 |
s.accept(this); |
| 643 |
} |
| 644 |
arrayEnd(); |
| 645 |
} |
| 646 |
objectEnd(); |
| 647 |
} |
| 648 |
|
| 649 |
override void visit(FuncDeclaration d) |
| 650 |
{ |
| 651 |
objectStart(); |
| 652 |
jsonProperties(d); |
| 653 |
TypeFunction tf = cast(TypeFunction)d.type; |
| 654 |
if (tf && tf.ty == Tfunction) |
| 655 |
property("parameters", tf.parameterList.parameters); |
| 656 |
property("endline", "endchar", d.endloc); |
| 657 |
if (d.foverrides.length) |
| 658 |
{ |
| 659 |
propertyStart("overrides"); |
| 660 |
arrayStart(); |
| 661 |
for (size_t i = 0; i < d.foverrides.length; i++) |
| 662 |
{ |
| 663 |
FuncDeclaration fd = d.foverrides[i]; |
| 664 |
item(fd.toPrettyChars().toDString); |
| 665 |
} |
| 666 |
arrayEnd(); |
| 667 |
} |
| 668 |
if (d.fdrequire) |
| 669 |
{ |
| 670 |
propertyStart("in"); |
| 671 |
d.fdrequire.accept(this); |
| 672 |
} |
| 673 |
if (d.fdensure) |
| 674 |
{ |
| 675 |
propertyStart("out"); |
| 676 |
d.fdensure.accept(this); |
| 677 |
} |
| 678 |
objectEnd(); |
| 679 |
} |
| 680 |
|
| 681 |
override void visit(TemplateDeclaration d) |
| 682 |
{ |
| 683 |
objectStart(); |
| 684 |
// TemplateDeclaration::kind returns the kind of its Aggregate onemember, if it is one |
| 685 |
property("kind", "template"); |
| 686 |
jsonProperties(d); |
| 687 |
propertyStart("parameters"); |
| 688 |
arrayStart(); |
| 689 |
for (size_t i = 0; i < d.parameters.length; i++) |
| 690 |
{ |
| 691 |
TemplateParameter s = (*d.parameters)[i]; |
| 692 |
objectStart(); |
| 693 |
property("name", s.ident.toString()); |
| 694 |
|
| 695 |
if (auto type = s.isTemplateTypeParameter()) |
| 696 |
{ |
| 697 |
if (s.isTemplateThisParameter()) |
| 698 |
property("kind", "this"); |
| 699 |
else |
| 700 |
property("kind", "type"); |
| 701 |
property("type", "deco", type.specType); |
| 702 |
property("default", "defaultDeco", type.defaultType); |
| 703 |
} |
| 704 |
|
| 705 |
if (auto value = s.isTemplateValueParameter()) |
| 706 |
{ |
| 707 |
property("kind", "value"); |
| 708 |
property("type", "deco", value.valType); |
| 709 |
if (value.specValue) |
| 710 |
property("specValue", value.specValue.toString()); |
| 711 |
if (value.defaultValue) |
| 712 |
property("defaultValue", value.defaultValue.toString()); |
| 713 |
} |
| 714 |
|
| 715 |
if (auto _alias = s.isTemplateAliasParameter()) |
| 716 |
{ |
| 717 |
property("kind", "alias"); |
| 718 |
property("type", "deco", _alias.specType); |
| 719 |
if (_alias.specAlias) |
| 720 |
property("specAlias", _alias.specAlias.toString()); |
| 721 |
if (_alias.defaultAlias) |
| 722 |
property("defaultAlias", _alias.defaultAlias.toString()); |
| 723 |
} |
| 724 |
|
| 725 |
if (auto tuple = s.isTemplateTupleParameter()) |
| 726 |
{ |
| 727 |
property("kind", "tuple"); |
| 728 |
} |
| 729 |
|
| 730 |
objectEnd(); |
| 731 |
} |
| 732 |
arrayEnd(); |
| 733 |
Expression expression = d.constraint; |
| 734 |
if (expression) |
| 735 |
{ |
| 736 |
property("constraint", expression.toString()); |
| 737 |
} |
| 738 |
propertyStart("members"); |
| 739 |
arrayStart(); |
| 740 |
for (size_t i = 0; i < d.members.length; i++) |
| 741 |
{ |
| 742 |
Dsymbol s = (*d.members)[i]; |
| 743 |
s.accept(this); |
| 744 |
} |
| 745 |
arrayEnd(); |
| 746 |
objectEnd(); |
| 747 |
} |
| 748 |
|
| 749 |
override void visit(EnumDeclaration d) |
| 750 |
{ |
| 751 |
if (d.isAnonymous()) |
| 752 |
{ |
| 753 |
if (d.members) |
| 754 |
{ |
| 755 |
for (size_t i = 0; i < d.members.length; i++) |
| 756 |
{ |
| 757 |
Dsymbol s = (*d.members)[i]; |
| 758 |
s.accept(this); |
| 759 |
} |
| 760 |
} |
| 761 |
return; |
| 762 |
} |
| 763 |
objectStart(); |
| 764 |
jsonProperties(d); |
| 765 |
property("base", "baseDeco", d.memtype); |
| 766 |
if (d.members) |
| 767 |
{ |
| 768 |
propertyStart("members"); |
| 769 |
arrayStart(); |
| 770 |
for (size_t i = 0; i < d.members.length; i++) |
| 771 |
{ |
| 772 |
Dsymbol s = (*d.members)[i]; |
| 773 |
s.accept(this); |
| 774 |
} |
| 775 |
arrayEnd(); |
| 776 |
} |
| 777 |
objectEnd(); |
| 778 |
} |
| 779 |
|
| 780 |
override void visit(EnumMember s) |
| 781 |
{ |
| 782 |
objectStart(); |
| 783 |
jsonProperties(cast(Dsymbol)s); |
| 784 |
property("type", "deco", s.origType); |
| 785 |
objectEnd(); |
| 786 |
} |
| 787 |
|
| 788 |
override void visit(VarDeclaration d) |
| 789 |
{ |
| 790 |
if (d.storage_class & STC.local) |
| 791 |
return; |
| 792 |
objectStart(); |
| 793 |
jsonProperties(d); |
| 794 |
if (d._init) |
| 795 |
property("init", toString(d._init)); |
| 796 |
if (d.isField()) |
| 797 |
property("offset", d.offset); |
| 798 |
if (!d.alignment.isUnknown() && !d.alignment.isDefault()) |
| 799 |
property("align", d.alignment.get()); |
| 800 |
objectEnd(); |
| 801 |
} |
| 802 |
|
| 803 |
override void visit(TemplateMixin d) |
| 804 |
{ |
| 805 |
objectStart(); |
| 806 |
jsonProperties(d); |
| 807 |
objectEnd(); |
| 808 |
} |
| 809 |
|
| 810 |
/** |
| 811 |
Generate an array of module objects that represent the syntax of each |
| 812 |
"root module". |
| 813 |
|
| 814 |
Params: |
| 815 |
modules = array of the "root modules" |
| 816 |
*/ |
| 817 |
private void generateModules(ref Modules modules) |
| 818 |
{ |
| 819 |
arrayStart(); |
| 820 |
foreach (m; modules) |
| 821 |
{ |
| 822 |
if (global.params.v.verbose) |
| 823 |
message("json gen %s", m.toChars()); |
| 824 |
m.accept(this); |
| 825 |
} |
| 826 |
arrayEnd(); |
| 827 |
} |
| 828 |
|
| 829 |
/** |
| 830 |
Generate the "compilerInfo" object which contains information about the compiler |
| 831 |
such as the filename, version, supported features, etc. |
| 832 |
*/ |
| 833 |
private void generateCompilerInfo() |
| 834 |
{ |
| 835 |
import dmd.target : target; |
| 836 |
objectStart(); |
| 837 |
requiredProperty("vendor", global.compileEnv.vendor); |
| 838 |
requiredProperty("version", global.versionString()); |
| 839 |
property("__VERSION__", global.versionNumber()); |
| 840 |
requiredProperty("interface", determineCompilerInterface()); |
| 841 |
property("size_t", size_t.sizeof); |
| 842 |
propertyStart("platforms"); |
| 843 |
arrayStart(); |
| 844 |
if (target.os == Target.OS.Windows) |
| 845 |
{ |
| 846 |
item("windows"); |
| 847 |
} |
| 848 |
else |
| 849 |
{ |
| 850 |
item("posix"); |
| 851 |
if (target.os == Target.OS.linux) |
| 852 |
item("linux"); |
| 853 |
else if (target.os == Target.OS.OSX) |
| 854 |
item("osx"); |
| 855 |
else if (target.os == Target.OS.FreeBSD) |
| 856 |
{ |
| 857 |
item("freebsd"); |
| 858 |
item("bsd"); |
| 859 |
} |
| 860 |
else if (target.os == Target.OS.OpenBSD) |
| 861 |
{ |
| 862 |
item("openbsd"); |
| 863 |
item("bsd"); |
| 864 |
} |
| 865 |
else if (target.os == Target.OS.Solaris) |
| 866 |
{ |
| 867 |
item("solaris"); |
| 868 |
item("bsd"); |
| 869 |
} |
| 870 |
} |
| 871 |
arrayEnd(); |
| 872 |
|
| 873 |
propertyStart("architectures"); |
| 874 |
arrayStart(); |
| 875 |
item(target.architectureName); |
| 876 |
arrayEnd(); |
| 877 |
|
| 878 |
propertyStart("predefinedVersions"); |
| 879 |
arrayStart(); |
| 880 |
foreach (const versionid; global.versionids) |
| 881 |
{ |
| 882 |
item(versionid.toString()); |
| 883 |
} |
| 884 |
arrayEnd(); |
| 885 |
|
| 886 |
propertyStart("supportedFeatures"); |
| 887 |
{ |
| 888 |
objectStart(); |
| 889 |
scope(exit) objectEnd(); |
| 890 |
propertyBool("includeImports", true); |
| 891 |
} |
| 892 |
objectEnd(); |
| 893 |
} |
| 894 |
|
| 895 |
/** |
| 896 |
Generate the "buildInfo" object which contains information specific to the |
| 897 |
current build such as CWD, importPaths, configFile, etc. |
| 898 |
*/ |
| 899 |
private void generateBuildInfo() |
| 900 |
{ |
| 901 |
objectStart(); |
| 902 |
requiredProperty("cwd", getcwd(null, 0).toDString); |
| 903 |
requiredProperty("argv0", global.params.argv0); |
| 904 |
requiredProperty("config", global.inifilename); |
| 905 |
requiredProperty("libName", global.params.libname); |
| 906 |
|
| 907 |
propertyStart("importPaths"); |
| 908 |
arrayStart(); |
| 909 |
foreach (importPath; global.params.imppath[]) |
| 910 |
{ |
| 911 |
item(importPath.path.toDString); |
| 912 |
} |
| 913 |
arrayEnd(); |
| 914 |
|
| 915 |
propertyStart("objectFiles"); |
| 916 |
arrayStart(); |
| 917 |
foreach (objfile; global.params.objfiles) |
| 918 |
{ |
| 919 |
item(objfile.toDString); |
| 920 |
} |
| 921 |
arrayEnd(); |
| 922 |
|
| 923 |
propertyStart("libraryFiles"); |
| 924 |
arrayStart(); |
| 925 |
foreach (lib; global.params.libfiles) |
| 926 |
{ |
| 927 |
item(lib.toDString); |
| 928 |
} |
| 929 |
arrayEnd(); |
| 930 |
|
| 931 |
propertyStart("ddocFiles"); |
| 932 |
arrayStart(); |
| 933 |
foreach (ddocFile; global.params.ddoc.files) |
| 934 |
{ |
| 935 |
item(ddocFile.toDString); |
| 936 |
} |
| 937 |
arrayEnd(); |
| 938 |
|
| 939 |
requiredProperty("mapFile", global.params.mapfile); |
| 940 |
requiredProperty("resourceFile", global.params.resfile); |
| 941 |
requiredProperty("defFile", global.params.deffile); |
| 942 |
|
| 943 |
objectEnd(); |
| 944 |
} |
| 945 |
|
| 946 |
/** |
| 947 |
Generate the "semantics" object which contains a 'modules' field representing |
| 948 |
semantic information about all the modules used in the compilation such as |
| 949 |
module name, isRoot, contentImportedFiles, etc. |
| 950 |
*/ |
| 951 |
private void generateSemantics() |
| 952 |
{ |
| 953 |
objectStart(); |
| 954 |
propertyStart("modules"); |
| 955 |
arrayStart(); |
| 956 |
foreach (m; Module.amodules) |
| 957 |
{ |
| 958 |
objectStart(); |
| 959 |
requiredProperty("name", m.md ? m.md.toString() : null); |
| 960 |
requiredProperty("file", m.srcfile.toString()); |
| 961 |
propertyBool("isRoot", m.isRoot()); |
| 962 |
if(m.contentImportedFiles.length > 0) |
| 963 |
{ |
| 964 |
propertyStart("contentImports"); |
| 965 |
arrayStart(); |
| 966 |
foreach (file; m.contentImportedFiles) |
| 967 |
{ |
| 968 |
item(file.toDString); |
| 969 |
} |
| 970 |
arrayEnd(); |
| 971 |
} |
| 972 |
objectEnd(); |
| 973 |
} |
| 974 |
arrayEnd(); |
| 975 |
objectEnd(); |
| 976 |
} |
| 977 |
} |
| 978 |
|
| 979 |
/*********************************** |
| 980 |
* Generate json for the modules. |
| 981 |
* Params: |
| 982 |
* modules = array of Modules |
| 983 |
* buf = write json output to buf |
| 984 |
*/ |
| 985 |
void json_generate(ref Modules modules, ref OutBuffer buf) |
| 986 |
{ |
| 987 |
scope ToJsonVisitor json = new ToJsonVisitor(&buf); |
| 988 |
// write trailing newline |
| 989 |
scope(exit) buf.writeByte('\n'); |
| 990 |
|
| 991 |
if (global.params.jsonFieldFlags == 0) |
| 992 |
{ |
| 993 |
// Generate the original format, which is just an array |
| 994 |
// of modules representing their syntax. |
| 995 |
json.generateModules(modules); |
| 996 |
json.removeComma(); |
| 997 |
return; |
| 998 |
} |
| 999 |
|
| 1000 |
// Generate the new format which is an object where each |
| 1001 |
// output option is its own field. |
| 1002 |
|
| 1003 |
json.objectStart(); |
| 1004 |
if (global.params.jsonFieldFlags & JsonFieldFlags.compilerInfo) |
| 1005 |
{ |
| 1006 |
json.propertyStart("compilerInfo"); |
| 1007 |
json.generateCompilerInfo(); |
| 1008 |
} |
| 1009 |
if (global.params.jsonFieldFlags & JsonFieldFlags.buildInfo) |
| 1010 |
{ |
| 1011 |
json.propertyStart("buildInfo"); |
| 1012 |
json.generateBuildInfo(); |
| 1013 |
} |
| 1014 |
if (global.params.jsonFieldFlags & JsonFieldFlags.modules) |
| 1015 |
{ |
| 1016 |
json.propertyStart("modules"); |
| 1017 |
json.generateModules(modules); |
| 1018 |
} |
| 1019 |
if (global.params.jsonFieldFlags & JsonFieldFlags.semantics) |
| 1020 |
{ |
| 1021 |
json.propertyStart("semantics"); |
| 1022 |
json.generateSemantics(); |
| 1023 |
} |
| 1024 |
json.objectEnd(); |
| 1025 |
} |
| 1026 |
|
| 1027 |
/** |
| 1028 |
A string listing the name of each JSON field. Useful for errors messages. |
| 1029 |
*/ |
| 1030 |
enum jsonFieldNames = () { |
| 1031 |
string s; |
| 1032 |
string prefix = ""; |
| 1033 |
foreach (idx, enumName; __traits(allMembers, JsonFieldFlags)) |
| 1034 |
{ |
| 1035 |
static if (idx > 0) |
| 1036 |
{ |
| 1037 |
s ~= prefix ~ "`" ~ enumName ~ "`"; |
| 1038 |
prefix = ", "; |
| 1039 |
} |
| 1040 |
} |
| 1041 |
return s; |
| 1042 |
}(); |
| 1043 |
|
| 1044 |
/** |
| 1045 |
Parse the given `fieldName` and return its corresponding JsonFieldFlags value. |
| 1046 |
|
| 1047 |
Params: |
| 1048 |
fieldName = the field name to parse |
| 1049 |
|
| 1050 |
Returns: JsonFieldFlags.none on error, otherwise the JsonFieldFlags value |
| 1051 |
corresponding to the given fieldName. |
| 1052 |
*/ |
| 1053 |
JsonFieldFlags tryParseJsonField(const(char)* fieldName) |
| 1054 |
{ |
| 1055 |
auto fieldNameString = fieldName.toDString(); |
| 1056 |
foreach (idx, enumName; __traits(allMembers, JsonFieldFlags)) |
| 1057 |
{ |
| 1058 |
static if (idx > 0) |
| 1059 |
{ |
| 1060 |
if (fieldNameString == enumName) |
| 1061 |
return __traits(getMember, JsonFieldFlags, enumName); |
| 1062 |
} |
| 1063 |
} |
| 1064 |
return JsonFieldFlags.none; |
| 1065 |
} |
| 1066 |
|
| 1067 |
/** |
| 1068 |
Determines and returns the compiler interface which is one of `dmd`, `ldc`, |
| 1069 |
`gdc` or `sdc`. Returns `null` if no interface can be determined. |
| 1070 |
*/ |
| 1071 |
private extern(D) string determineCompilerInterface() |
| 1072 |
{ |
| 1073 |
if (global.compileEnv.vendor == "Digital Mars D") |
| 1074 |
return "dmd"; |
| 1075 |
if (global.compileEnv.vendor == "LDC") |
| 1076 |
return "ldc"; |
| 1077 |
if (global.compileEnv.vendor == "GNU D") |
| 1078 |
return "gdc"; |
| 1079 |
if (global.compileEnv.vendor == "SDC") |
| 1080 |
return "sdc"; |
| 1081 |
return null; |
| 1082 |
} |