blob: 676cc4a4fb1efafd1e74d3a00722f50ac083a7d3 [file] [log] [blame]
Aaron Ballman2ce598a2019-05-13 21:39:55 +00001#include "clang/AST/JSONNodeDumper.h"
2#include "llvm/ADT/StringSwitch.h"
3
4using namespace clang;
5
6void JSONNodeDumper::addPreviousDeclaration(const Decl *D) {
7 switch (D->getKind()) {
8#define DECL(DERIVED, BASE) \
9 case Decl::DERIVED: \
10 return writePreviousDeclImpl(cast<DERIVED##Decl>(D));
11#define ABSTRACT_DECL(DECL)
12#include "clang/AST/DeclNodes.inc"
13#undef ABSTRACT_DECL
14#undef DECL
15 }
16 llvm_unreachable("Decl that isn't part of DeclNodes.inc!");
17}
18
19void JSONNodeDumper::Visit(const Attr *A) {
20 const char *AttrName = nullptr;
21 switch (A->getKind()) {
22#define ATTR(X) \
23 case attr::X: \
24 AttrName = #X"Attr"; \
25 break;
26#include "clang/Basic/AttrList.inc"
27#undef ATTR
28 }
29 JOS.attribute("id", createPointerRepresentation(A));
30 JOS.attribute("kind", AttrName);
31 JOS.attribute("range", createSourceRange(A->getRange()));
32 attributeOnlyIfTrue("inherited", A->isInherited());
33 attributeOnlyIfTrue("implicit", A->isImplicit());
34
35 // FIXME: it would be useful for us to output the spelling kind as well as
36 // the actual spelling. This would allow us to distinguish between the
37 // various attribute syntaxes, but we don't currently track that information
38 // within the AST.
39 //JOS.attribute("spelling", A->getSpelling());
40
41 InnerAttrVisitor::Visit(A);
42}
43
44void JSONNodeDumper::Visit(const Stmt *S) {
45 if (!S)
46 return;
47
48 JOS.attribute("id", createPointerRepresentation(S));
49 JOS.attribute("kind", S->getStmtClassName());
50 JOS.attribute("range", createSourceRange(S->getSourceRange()));
51
52 if (const auto *E = dyn_cast<Expr>(S)) {
53 JOS.attribute("type", createQualType(E->getType()));
54 const char *Category = nullptr;
55 switch (E->getValueKind()) {
56 case VK_LValue: Category = "lvalue"; break;
57 case VK_XValue: Category = "xvalue"; break;
58 case VK_RValue: Category = "rvalue"; break;
59 }
60 JOS.attribute("valueCategory", Category);
61 }
62 InnerStmtVisitor::Visit(S);
63}
64
65void JSONNodeDumper::Visit(const Type *T) {
66 JOS.attribute("id", createPointerRepresentation(T));
67 JOS.attribute("kind", (llvm::Twine(T->getTypeClassName()) + "Type").str());
68 JOS.attribute("type", createQualType(QualType(T, 0), /*Desugar*/ false));
69 attributeOnlyIfTrue("isDependent", T->isDependentType());
70 attributeOnlyIfTrue("isInstantiationDependent",
71 T->isInstantiationDependentType());
72 attributeOnlyIfTrue("isVariablyModified", T->isVariablyModifiedType());
73 attributeOnlyIfTrue("containsUnexpandedPack",
74 T->containsUnexpandedParameterPack());
75 attributeOnlyIfTrue("isImported", T->isFromAST());
76 InnerTypeVisitor::Visit(T);
77}
78
79void JSONNodeDumper::Visit(QualType T) {
80 JOS.attribute("id", createPointerRepresentation(T.getAsOpaquePtr()));
81 JOS.attribute("type", createQualType(T));
82 JOS.attribute("qualifiers", T.split().Quals.getAsString());
83}
84
85void JSONNodeDumper::Visit(const Decl *D) {
86 JOS.attribute("id", createPointerRepresentation(D));
87
88 if (!D)
89 return;
90
91 JOS.attribute("kind", (llvm::Twine(D->getDeclKindName()) + "Decl").str());
92 JOS.attribute("loc", createSourceLocation(D->getLocation()));
93 JOS.attribute("range", createSourceRange(D->getSourceRange()));
94 attributeOnlyIfTrue("isImplicit", D->isImplicit());
95 attributeOnlyIfTrue("isInvalid", D->isInvalidDecl());
96
97 if (D->isUsed())
98 JOS.attribute("isUsed", true);
99 else if (D->isThisDeclarationReferenced())
100 JOS.attribute("isReferenced", true);
101
102 if (const auto *ND = dyn_cast<NamedDecl>(D))
103 attributeOnlyIfTrue("isHidden", ND->isHidden());
104
105 if (D->getLexicalDeclContext() != D->getDeclContext())
106 JOS.attribute("parentDeclContext",
107 createPointerRepresentation(D->getDeclContext()));
108
109 addPreviousDeclaration(D);
110 InnerDeclVisitor::Visit(D);
111}
112
113void JSONNodeDumper::Visit(const comments::Comment *C,
114 const comments::FullComment *FC) {}
115
116void JSONNodeDumper::Visit(const TemplateArgument &TA, SourceRange R,
117 const Decl *From, StringRef Label) {
118 JOS.attribute("kind", "TemplateArgument");
119 if (R.isValid())
120 JOS.attribute("range", createSourceRange(R));
121
122 if (From)
123 JOS.attribute(Label.empty() ? "fromDecl" : Label, createBareDeclRef(From));
124
125 InnerTemplateArgVisitor::Visit(TA);
126}
127
128void JSONNodeDumper::Visit(const CXXCtorInitializer *Init) {
129 JOS.attribute("kind", "CXXCtorInitializer");
130 if (Init->isAnyMemberInitializer())
131 JOS.attribute("anyInit", createBareDeclRef(Init->getAnyMember()));
132 else if (Init->isBaseInitializer())
133 JOS.attribute("baseInit",
134 createQualType(QualType(Init->getBaseClass(), 0)));
135 else if (Init->isDelegatingInitializer())
136 JOS.attribute("delegatingInit",
137 createQualType(Init->getTypeSourceInfo()->getType()));
138 else
139 llvm_unreachable("Unknown initializer type");
140}
141
142void JSONNodeDumper::Visit(const OMPClause *C) {}
143void JSONNodeDumper::Visit(const BlockDecl::Capture &C) {}
144void JSONNodeDumper::Visit(const GenericSelectionExpr::ConstAssociation &A) {
145 JOS.attribute("associationKind", A.getTypeSourceInfo() ? "case" : "default");
146 attributeOnlyIfTrue("selected", A.isSelected());
147}
148
Aaron Ballman4d05a972019-05-20 16:46:44 +0000149llvm::json::Object
150JSONNodeDumper::createBareSourceLocation(SourceLocation Loc) {
151 PresumedLoc Presumed = SM.getPresumedLoc(Loc);
Aaron Ballman2ce598a2019-05-13 21:39:55 +0000152
153 if (Presumed.isInvalid())
154 return llvm::json::Object{};
155
156 return llvm::json::Object{{"file", Presumed.getFilename()},
157 {"line", Presumed.getLine()},
158 {"col", Presumed.getColumn()}};
159}
160
Aaron Ballman4d05a972019-05-20 16:46:44 +0000161llvm::json::Object JSONNodeDumper::createSourceLocation(SourceLocation Loc) {
162 SourceLocation Spelling = SM.getSpellingLoc(Loc);
163 SourceLocation Expansion = SM.getExpansionLoc(Loc);
164
165 llvm::json::Object SLoc = createBareSourceLocation(Spelling);
166 if (Expansion != Spelling) {
167 // If the expansion and the spelling are different, output subobjects
168 // describing both locations.
169 llvm::json::Object ELoc = createBareSourceLocation(Expansion);
170
171 // If there is a macro expansion, add extra information if the interesting
172 // bit is the macro arg expansion.
173 if (SM.isMacroArgExpansion(Loc))
174 ELoc["isMacroArgExpansion"] = true;
175
176 return llvm::json::Object{{"spellingLoc", std::move(SLoc)},
177 {"expansionLoc", std::move(ELoc)}};
178 }
179
180 return SLoc;
181}
182
Aaron Ballman2ce598a2019-05-13 21:39:55 +0000183llvm::json::Object JSONNodeDumper::createSourceRange(SourceRange R) {
184 return llvm::json::Object{{"begin", createSourceLocation(R.getBegin())},
185 {"end", createSourceLocation(R.getEnd())}};
186}
187
188std::string JSONNodeDumper::createPointerRepresentation(const void *Ptr) {
189 // Because JSON stores integer values as signed 64-bit integers, trying to
190 // represent them as such makes for very ugly pointer values in the resulting
191 // output. Instead, we convert the value to hex and treat it as a string.
192 return "0x" + llvm::utohexstr(reinterpret_cast<uint64_t>(Ptr), true);
193}
194
195llvm::json::Object JSONNodeDumper::createQualType(QualType QT, bool Desugar) {
196 SplitQualType SQT = QT.split();
197 llvm::json::Object Ret{{"qualType", QualType::getAsString(SQT, PrintPolicy)}};
198
199 if (Desugar && !QT.isNull()) {
200 SplitQualType DSQT = QT.getSplitDesugaredType();
201 if (DSQT != SQT)
202 Ret["desugaredQualType"] = QualType::getAsString(DSQT, PrintPolicy);
203 }
204 return Ret;
205}
206
207llvm::json::Object JSONNodeDumper::createBareDeclRef(const Decl *D) {
208 llvm::json::Object Ret{
209 {"id", createPointerRepresentation(D)},
210 {"kind", (llvm::Twine(D->getDeclKindName()) + "Decl").str()}};
211 if (const auto *ND = dyn_cast<NamedDecl>(D))
212 Ret["name"] = ND->getDeclName().getAsString();
213 if (const auto *VD = dyn_cast<ValueDecl>(D))
214 Ret["type"] = createQualType(VD->getType());
215 return Ret;
216}
217
218llvm::json::Array JSONNodeDumper::createCastPath(const CastExpr *C) {
219 llvm::json::Array Ret;
220 if (C->path_empty())
221 return Ret;
222
223 for (auto I = C->path_begin(), E = C->path_end(); I != E; ++I) {
224 const CXXBaseSpecifier *Base = *I;
225 const auto *RD =
226 cast<CXXRecordDecl>(Base->getType()->getAs<RecordType>()->getDecl());
227
228 llvm::json::Object Val{{"name", RD->getName()}};
229 if (Base->isVirtual())
230 Val["isVirtual"] = true;
231 Ret.push_back(std::move(Val));
232 }
233 return Ret;
234}
235
236#define FIELD2(Name, Flag) if (RD->Flag()) Ret[Name] = true
237#define FIELD1(Flag) FIELD2(#Flag, Flag)
238
239static llvm::json::Object
240createDefaultConstructorDefinitionData(const CXXRecordDecl *RD) {
241 llvm::json::Object Ret;
242
243 FIELD2("exists", hasDefaultConstructor);
244 FIELD2("trivial", hasTrivialDefaultConstructor);
245 FIELD2("nonTrivial", hasNonTrivialDefaultConstructor);
246 FIELD2("userProvided", hasUserProvidedDefaultConstructor);
247 FIELD2("isConstexpr", hasConstexprDefaultConstructor);
248 FIELD2("needsImplicit", needsImplicitDefaultConstructor);
249 FIELD2("defaultedIsConstexpr", defaultedDefaultConstructorIsConstexpr);
250
251 return Ret;
252}
253
254static llvm::json::Object
255createCopyConstructorDefinitionData(const CXXRecordDecl *RD) {
256 llvm::json::Object Ret;
257
258 FIELD2("simple", hasSimpleCopyConstructor);
259 FIELD2("trivial", hasTrivialCopyConstructor);
260 FIELD2("nonTrivial", hasNonTrivialCopyConstructor);
261 FIELD2("userDeclared", hasUserDeclaredCopyConstructor);
262 FIELD2("hasConstParam", hasCopyConstructorWithConstParam);
263 FIELD2("implicitHasConstParam", implicitCopyConstructorHasConstParam);
264 FIELD2("needsImplicit", needsImplicitCopyConstructor);
265 FIELD2("needsOverloadResolution", needsOverloadResolutionForCopyConstructor);
266 if (!RD->needsOverloadResolutionForCopyConstructor())
267 FIELD2("defaultedIsDeleted", defaultedCopyConstructorIsDeleted);
268
269 return Ret;
270}
271
272static llvm::json::Object
273createMoveConstructorDefinitionData(const CXXRecordDecl *RD) {
274 llvm::json::Object Ret;
275
276 FIELD2("exists", hasMoveConstructor);
277 FIELD2("simple", hasSimpleMoveConstructor);
278 FIELD2("trivial", hasTrivialMoveConstructor);
279 FIELD2("nonTrivial", hasNonTrivialMoveConstructor);
280 FIELD2("userDeclared", hasUserDeclaredMoveConstructor);
281 FIELD2("needsImplicit", needsImplicitMoveConstructor);
282 FIELD2("needsOverloadResolution", needsOverloadResolutionForMoveConstructor);
283 if (!RD->needsOverloadResolutionForMoveConstructor())
284 FIELD2("defaultedIsDeleted", defaultedMoveConstructorIsDeleted);
285
286 return Ret;
287}
288
289static llvm::json::Object
290createCopyAssignmentDefinitionData(const CXXRecordDecl *RD) {
291 llvm::json::Object Ret;
292
293 FIELD2("trivial", hasTrivialCopyAssignment);
294 FIELD2("nonTrivial", hasNonTrivialCopyAssignment);
295 FIELD2("hasConstParam", hasCopyAssignmentWithConstParam);
296 FIELD2("implicitHasConstParam", implicitCopyAssignmentHasConstParam);
297 FIELD2("userDeclared", hasUserDeclaredCopyAssignment);
298 FIELD2("needsImplicit", needsImplicitCopyAssignment);
299 FIELD2("needsOverloadResolution", needsOverloadResolutionForCopyAssignment);
300
301 return Ret;
302}
303
304static llvm::json::Object
305createMoveAssignmentDefinitionData(const CXXRecordDecl *RD) {
306 llvm::json::Object Ret;
307
308 FIELD2("exists", hasMoveAssignment);
309 FIELD2("simple", hasSimpleMoveAssignment);
310 FIELD2("trivial", hasTrivialMoveAssignment);
311 FIELD2("nonTrivial", hasNonTrivialMoveAssignment);
312 FIELD2("userDeclared", hasUserDeclaredMoveAssignment);
313 FIELD2("needsImplicit", needsImplicitMoveAssignment);
314 FIELD2("needsOverloadResolution", needsOverloadResolutionForMoveAssignment);
315
316 return Ret;
317}
318
319static llvm::json::Object
320createDestructorDefinitionData(const CXXRecordDecl *RD) {
321 llvm::json::Object Ret;
322
323 FIELD2("simple", hasSimpleDestructor);
324 FIELD2("irrelevant", hasIrrelevantDestructor);
325 FIELD2("trivial", hasTrivialDestructor);
326 FIELD2("nonTrivial", hasNonTrivialDestructor);
327 FIELD2("userDeclared", hasUserDeclaredDestructor);
328 FIELD2("needsImplicit", needsImplicitDestructor);
329 FIELD2("needsOverloadResolution", needsOverloadResolutionForDestructor);
330 if (!RD->needsOverloadResolutionForDestructor())
331 FIELD2("defaultedIsDeleted", defaultedDestructorIsDeleted);
332
333 return Ret;
334}
335
336llvm::json::Object
337JSONNodeDumper::createCXXRecordDefinitionData(const CXXRecordDecl *RD) {
338 llvm::json::Object Ret;
339
340 // This data is common to all C++ classes.
341 FIELD1(isGenericLambda);
342 FIELD1(isLambda);
343 FIELD1(isEmpty);
344 FIELD1(isAggregate);
345 FIELD1(isStandardLayout);
346 FIELD1(isTriviallyCopyable);
347 FIELD1(isPOD);
348 FIELD1(isTrivial);
349 FIELD1(isPolymorphic);
350 FIELD1(isAbstract);
351 FIELD1(isLiteral);
352 FIELD1(canPassInRegisters);
353 FIELD1(hasUserDeclaredConstructor);
354 FIELD1(hasConstexprNonCopyMoveConstructor);
355 FIELD1(hasMutableFields);
356 FIELD1(hasVariantMembers);
357 FIELD2("canConstDefaultInit", allowConstDefaultInit);
358
359 Ret["defaultCtor"] = createDefaultConstructorDefinitionData(RD);
360 Ret["copyCtor"] = createCopyConstructorDefinitionData(RD);
361 Ret["moveCtor"] = createMoveConstructorDefinitionData(RD);
362 Ret["copyAssign"] = createCopyAssignmentDefinitionData(RD);
363 Ret["moveAssign"] = createMoveAssignmentDefinitionData(RD);
364 Ret["dtor"] = createDestructorDefinitionData(RD);
365
366 return Ret;
367}
368
369#undef FIELD1
370#undef FIELD2
371
372std::string JSONNodeDumper::createAccessSpecifier(AccessSpecifier AS) {
373 switch (AS) {
374 case AS_none: return "none";
375 case AS_private: return "private";
376 case AS_protected: return "protected";
377 case AS_public: return "public";
378 }
379 llvm_unreachable("Unknown access specifier");
380}
381
382llvm::json::Object
383JSONNodeDumper::createCXXBaseSpecifier(const CXXBaseSpecifier &BS) {
384 llvm::json::Object Ret;
385
386 Ret["type"] = createQualType(BS.getType());
387 Ret["access"] = createAccessSpecifier(BS.getAccessSpecifier());
388 Ret["writtenAccess"] =
389 createAccessSpecifier(BS.getAccessSpecifierAsWritten());
390 if (BS.isVirtual())
391 Ret["isVirtual"] = true;
392 if (BS.isPackExpansion())
393 Ret["isPackExpansion"] = true;
394
395 return Ret;
396}
397
398void JSONNodeDumper::VisitTypedefType(const TypedefType *TT) {
399 JOS.attribute("decl", createBareDeclRef(TT->getDecl()));
400}
401
402void JSONNodeDumper::VisitFunctionType(const FunctionType *T) {
403 FunctionType::ExtInfo E = T->getExtInfo();
404 attributeOnlyIfTrue("noreturn", E.getNoReturn());
405 attributeOnlyIfTrue("producesResult", E.getProducesResult());
406 if (E.getHasRegParm())
407 JOS.attribute("regParm", E.getRegParm());
408 JOS.attribute("cc", FunctionType::getNameForCallConv(E.getCC()));
409}
410
411void JSONNodeDumper::VisitFunctionProtoType(const FunctionProtoType *T) {
412 FunctionProtoType::ExtProtoInfo E = T->getExtProtoInfo();
413 attributeOnlyIfTrue("trailingReturn", E.HasTrailingReturn);
414 attributeOnlyIfTrue("const", T->isConst());
415 attributeOnlyIfTrue("volatile", T->isVolatile());
416 attributeOnlyIfTrue("restrict", T->isRestrict());
417 attributeOnlyIfTrue("variadic", E.Variadic);
418 switch (E.RefQualifier) {
419 case RQ_LValue: JOS.attribute("refQualifier", "&"); break;
420 case RQ_RValue: JOS.attribute("refQualifier", "&&"); break;
421 case RQ_None: break;
422 }
423 switch (E.ExceptionSpec.Type) {
424 case EST_DynamicNone:
425 case EST_Dynamic: {
426 JOS.attribute("exceptionSpec", "throw");
427 llvm::json::Array Types;
428 for (QualType QT : E.ExceptionSpec.Exceptions)
429 Types.push_back(createQualType(QT));
430 JOS.attribute("exceptionTypes", std::move(Types));
431 } break;
432 case EST_MSAny:
433 JOS.attribute("exceptionSpec", "throw");
434 JOS.attribute("throwsAny", true);
435 break;
436 case EST_BasicNoexcept:
437 JOS.attribute("exceptionSpec", "noexcept");
438 break;
439 case EST_NoexceptTrue:
440 case EST_NoexceptFalse:
441 JOS.attribute("exceptionSpec", "noexcept");
442 JOS.attribute("conditionEvaluatesTo",
443 E.ExceptionSpec.Type == EST_NoexceptTrue);
444 //JOS.attributeWithCall("exceptionSpecExpr",
445 // [this, E]() { Visit(E.ExceptionSpec.NoexceptExpr); });
446 break;
447
448 // FIXME: I cannot find a way to trigger these cases while dumping the AST. I
449 // suspect you can only run into them when executing an AST dump from within
450 // the debugger, which is not a use case we worry about for the JSON dumping
451 // feature.
452 case EST_DependentNoexcept:
453 case EST_Unevaluated:
454 case EST_Uninstantiated:
455 case EST_Unparsed:
456 case EST_None: break;
457 }
458 VisitFunctionType(T);
459}
460
461void JSONNodeDumper::VisitNamedDecl(const NamedDecl *ND) {
462 if (ND && ND->getDeclName())
463 JOS.attribute("name", ND->getNameAsString());
464}
465
466void JSONNodeDumper::VisitTypedefDecl(const TypedefDecl *TD) {
467 VisitNamedDecl(TD);
468 JOS.attribute("type", createQualType(TD->getUnderlyingType()));
469}
470
471void JSONNodeDumper::VisitTypeAliasDecl(const TypeAliasDecl *TAD) {
472 VisitNamedDecl(TAD);
473 JOS.attribute("type", createQualType(TAD->getUnderlyingType()));
474}
475
476void JSONNodeDumper::VisitNamespaceDecl(const NamespaceDecl *ND) {
477 VisitNamedDecl(ND);
478 attributeOnlyIfTrue("isInline", ND->isInline());
479 if (!ND->isOriginalNamespace())
480 JOS.attribute("originalNamespace",
481 createBareDeclRef(ND->getOriginalNamespace()));
482}
483
484void JSONNodeDumper::VisitUsingDirectiveDecl(const UsingDirectiveDecl *UDD) {
485 JOS.attribute("nominatedNamespace",
486 createBareDeclRef(UDD->getNominatedNamespace()));
487}
488
489void JSONNodeDumper::VisitNamespaceAliasDecl(const NamespaceAliasDecl *NAD) {
490 VisitNamedDecl(NAD);
491 JOS.attribute("aliasedNamespace",
492 createBareDeclRef(NAD->getAliasedNamespace()));
493}
494
495void JSONNodeDumper::VisitUsingDecl(const UsingDecl *UD) {
496 std::string Name;
497 if (const NestedNameSpecifier *NNS = UD->getQualifier()) {
498 llvm::raw_string_ostream SOS(Name);
499 NNS->print(SOS, UD->getASTContext().getPrintingPolicy());
500 }
501 Name += UD->getNameAsString();
502 JOS.attribute("name", Name);
503}
504
505void JSONNodeDumper::VisitUsingShadowDecl(const UsingShadowDecl *USD) {
506 JOS.attribute("target", createBareDeclRef(USD->getTargetDecl()));
507}
508
509void JSONNodeDumper::VisitVarDecl(const VarDecl *VD) {
510 VisitNamedDecl(VD);
511 JOS.attribute("type", createQualType(VD->getType()));
512
513 StorageClass SC = VD->getStorageClass();
514 if (SC != SC_None)
515 JOS.attribute("storageClass", VarDecl::getStorageClassSpecifierString(SC));
516 switch (VD->getTLSKind()) {
517 case VarDecl::TLS_Dynamic: JOS.attribute("tls", "dynamic"); break;
518 case VarDecl::TLS_Static: JOS.attribute("tls", "static"); break;
519 case VarDecl::TLS_None: break;
520 }
521 attributeOnlyIfTrue("nrvo", VD->isNRVOVariable());
522 attributeOnlyIfTrue("inline", VD->isInline());
523 attributeOnlyIfTrue("constexpr", VD->isConstexpr());
524 attributeOnlyIfTrue("modulePrivate", VD->isModulePrivate());
525 if (VD->hasInit()) {
526 switch (VD->getInitStyle()) {
527 case VarDecl::CInit: JOS.attribute("init", "c"); break;
528 case VarDecl::CallInit: JOS.attribute("init", "call"); break;
529 case VarDecl::ListInit: JOS.attribute("init", "list"); break;
530 }
531 }
532}
533
534void JSONNodeDumper::VisitFieldDecl(const FieldDecl *FD) {
535 VisitNamedDecl(FD);
536 JOS.attribute("type", createQualType(FD->getType()));
537 attributeOnlyIfTrue("mutable", FD->isMutable());
538 attributeOnlyIfTrue("modulePrivate", FD->isModulePrivate());
539 attributeOnlyIfTrue("isBitfield", FD->isBitField());
540 attributeOnlyIfTrue("hasInClassInitializer", FD->hasInClassInitializer());
541}
542
543void JSONNodeDumper::VisitFunctionDecl(const FunctionDecl *FD) {
544 VisitNamedDecl(FD);
545 JOS.attribute("type", createQualType(FD->getType()));
546 StorageClass SC = FD->getStorageClass();
547 if (SC != SC_None)
548 JOS.attribute("storageClass", VarDecl::getStorageClassSpecifierString(SC));
549 attributeOnlyIfTrue("inline", FD->isInlineSpecified());
550 attributeOnlyIfTrue("virtual", FD->isVirtualAsWritten());
551 attributeOnlyIfTrue("pure", FD->isPure());
552 attributeOnlyIfTrue("explicitlyDeleted", FD->isDeletedAsWritten());
553 attributeOnlyIfTrue("constexpr", FD->isConstexpr());
554 if (FD->isDefaulted())
555 JOS.attribute("explicitlyDefaulted",
556 FD->isDeleted() ? "deleted" : "default");
557}
558
559void JSONNodeDumper::VisitEnumDecl(const EnumDecl *ED) {
560 VisitNamedDecl(ED);
561 if (ED->isFixed())
562 JOS.attribute("fixedUnderlyingType", createQualType(ED->getIntegerType()));
563 if (ED->isScoped())
564 JOS.attribute("scopedEnumTag",
565 ED->isScopedUsingClassTag() ? "class" : "struct");
566}
567void JSONNodeDumper::VisitEnumConstantDecl(const EnumConstantDecl *ECD) {
568 VisitNamedDecl(ECD);
569 JOS.attribute("type", createQualType(ECD->getType()));
570}
571
572void JSONNodeDumper::VisitRecordDecl(const RecordDecl *RD) {
573 VisitNamedDecl(RD);
574 JOS.attribute("tagUsed", RD->getKindName());
575 attributeOnlyIfTrue("completeDefinition", RD->isCompleteDefinition());
576}
577void JSONNodeDumper::VisitCXXRecordDecl(const CXXRecordDecl *RD) {
578 VisitRecordDecl(RD);
579
580 // All other information requires a complete definition.
581 if (!RD->isCompleteDefinition())
582 return;
583
584 JOS.attribute("definitionData", createCXXRecordDefinitionData(RD));
585 if (RD->getNumBases()) {
586 JOS.attributeArray("bases", [this, RD] {
587 for (const auto &Spec : RD->bases())
588 JOS.value(createCXXBaseSpecifier(Spec));
589 });
590 }
591}
592
593void JSONNodeDumper::VisitTemplateTypeParmDecl(const TemplateTypeParmDecl *D) {
594 VisitNamedDecl(D);
595 JOS.attribute("tagUsed", D->wasDeclaredWithTypename() ? "typename" : "class");
596 JOS.attribute("depth", D->getDepth());
597 JOS.attribute("index", D->getIndex());
598 attributeOnlyIfTrue("isParameterPack", D->isParameterPack());
599}
600
601void JSONNodeDumper::VisitNonTypeTemplateParmDecl(
602 const NonTypeTemplateParmDecl *D) {
603 VisitNamedDecl(D);
604 JOS.attribute("type", createQualType(D->getType()));
605 JOS.attribute("depth", D->getDepth());
606 JOS.attribute("index", D->getIndex());
607 attributeOnlyIfTrue("isParameterPack", D->isParameterPack());
608}
609
610void JSONNodeDumper::VisitTemplateTemplateParmDecl(
611 const TemplateTemplateParmDecl *D) {
612 VisitNamedDecl(D);
613 JOS.attribute("depth", D->getDepth());
614 JOS.attribute("index", D->getIndex());
615 attributeOnlyIfTrue("isParameterPack", D->isParameterPack());
616}
617
618void JSONNodeDumper::VisitLinkageSpecDecl(const LinkageSpecDecl *LSD) {
619 StringRef Lang;
620 switch (LSD->getLanguage()) {
621 case LinkageSpecDecl::lang_c: Lang = "C"; break;
622 case LinkageSpecDecl::lang_cxx: Lang = "C++"; break;
623 }
624 JOS.attribute("language", Lang);
625 attributeOnlyIfTrue("hasBraces", LSD->hasBraces());
626}
627
628void JSONNodeDumper::VisitAccessSpecDecl(const AccessSpecDecl *ASD) {
629 JOS.attribute("access", createAccessSpecifier(ASD->getAccess()));
630}
631
632void JSONNodeDumper::VisitFriendDecl(const FriendDecl *FD) {
633 if (const TypeSourceInfo *T = FD->getFriendType())
634 JOS.attribute("type", createQualType(T->getType()));
635}
636
637void JSONNodeDumper::VisitDeclRefExpr(const DeclRefExpr *DRE) {
638 JOS.attribute("referencedDecl", createBareDeclRef(DRE->getDecl()));
639 if (DRE->getDecl() != DRE->getFoundDecl())
640 JOS.attribute("foundReferencedDecl",
641 createBareDeclRef(DRE->getFoundDecl()));
642}
643
644void JSONNodeDumper::VisitPredefinedExpr(const PredefinedExpr *PE) {
645 JOS.attribute("name", PredefinedExpr::getIdentKindName(PE->getIdentKind()));
646}
647
648void JSONNodeDumper::VisitUnaryOperator(const UnaryOperator *UO) {
649 JOS.attribute("isPostfix", UO->isPostfix());
650 JOS.attribute("opcode", UnaryOperator::getOpcodeStr(UO->getOpcode()));
651 if (!UO->canOverflow())
652 JOS.attribute("canOverflow", false);
653}
654
655void JSONNodeDumper::VisitBinaryOperator(const BinaryOperator *BO) {
656 JOS.attribute("opcode", BinaryOperator::getOpcodeStr(BO->getOpcode()));
657}
658
659void JSONNodeDumper::VisitCompoundAssignOperator(
660 const CompoundAssignOperator *CAO) {
661 VisitBinaryOperator(CAO);
662 JOS.attribute("computeLHSType", createQualType(CAO->getComputationLHSType()));
663 JOS.attribute("computeResultType",
664 createQualType(CAO->getComputationResultType()));
665}
666
667void JSONNodeDumper::VisitMemberExpr(const MemberExpr *ME) {
668 // Note, we always write this Boolean field because the information it conveys
669 // is critical to understanding the AST node.
670 JOS.attribute("isArrow", ME->isArrow());
671 JOS.attribute("referencedMemberDecl",
672 createPointerRepresentation(ME->getMemberDecl()));
673}
674
675void JSONNodeDumper::VisitCXXNewExpr(const CXXNewExpr *NE) {
676 attributeOnlyIfTrue("isGlobal", NE->isGlobalNew());
677 attributeOnlyIfTrue("isArray", NE->isArray());
678 attributeOnlyIfTrue("isPlacement", NE->getNumPlacementArgs() != 0);
679 switch (NE->getInitializationStyle()) {
680 case CXXNewExpr::NoInit: break;
681 case CXXNewExpr::CallInit: JOS.attribute("initStyle", "call"); break;
682 case CXXNewExpr::ListInit: JOS.attribute("initStyle", "list"); break;
683 }
684 if (const FunctionDecl *FD = NE->getOperatorNew())
685 JOS.attribute("operatorNewDecl", createBareDeclRef(FD));
686 if (const FunctionDecl *FD = NE->getOperatorDelete())
687 JOS.attribute("operatorDeleteDecl", createBareDeclRef(FD));
688}
689void JSONNodeDumper::VisitCXXDeleteExpr(const CXXDeleteExpr *DE) {
690 attributeOnlyIfTrue("isGlobal", DE->isGlobalDelete());
691 attributeOnlyIfTrue("isArray", DE->isArrayForm());
692 attributeOnlyIfTrue("isArrayAsWritten", DE->isArrayFormAsWritten());
693 if (const FunctionDecl *FD = DE->getOperatorDelete())
694 JOS.attribute("operatorDeleteDecl", createBareDeclRef(FD));
695}
696
697void JSONNodeDumper::VisitCXXThisExpr(const CXXThisExpr *TE) {
698 attributeOnlyIfTrue("implicit", TE->isImplicit());
699}
700
701void JSONNodeDumper::VisitCastExpr(const CastExpr *CE) {
702 JOS.attribute("castKind", CE->getCastKindName());
703 llvm::json::Array Path = createCastPath(CE);
704 if (!Path.empty())
705 JOS.attribute("path", std::move(Path));
706 // FIXME: This may not be useful information as it can be obtusely gleaned
707 // from the inner[] array.
708 if (const NamedDecl *ND = CE->getConversionFunction())
709 JOS.attribute("conversionFunc", createBareDeclRef(ND));
710}
711
712void JSONNodeDumper::VisitImplicitCastExpr(const ImplicitCastExpr *ICE) {
713 VisitCastExpr(ICE);
714 attributeOnlyIfTrue("isPartOfExplicitCast", ICE->isPartOfExplicitCast());
715}
716
717void JSONNodeDumper::VisitCallExpr(const CallExpr *CE) {
718 attributeOnlyIfTrue("adl", CE->usesADL());
719}
720
721void JSONNodeDumper::VisitUnaryExprOrTypeTraitExpr(
722 const UnaryExprOrTypeTraitExpr *TTE) {
723 switch (TTE->getKind()) {
724 case UETT_SizeOf: JOS.attribute("name", "sizeof"); break;
725 case UETT_AlignOf: JOS.attribute("name", "alignof"); break;
726 case UETT_VecStep: JOS.attribute("name", "vec_step"); break;
727 case UETT_PreferredAlignOf: JOS.attribute("name", "__alignof"); break;
728 case UETT_OpenMPRequiredSimdAlign:
729 JOS.attribute("name", "__builtin_omp_required_simd_align"); break;
730 }
731 if (TTE->isArgumentType())
732 JOS.attribute("argType", createQualType(TTE->getArgumentType()));
733}
734
735void JSONNodeDumper::VisitUnresolvedLookupExpr(
736 const UnresolvedLookupExpr *ULE) {
737 JOS.attribute("usesADL", ULE->requiresADL());
738 JOS.attribute("name", ULE->getName().getAsString());
739
740 JOS.attributeArray("lookups", [this, ULE] {
741 for (const NamedDecl *D : ULE->decls())
742 JOS.value(createBareDeclRef(D));
743 });
744}
745
746void JSONNodeDumper::VisitAddrLabelExpr(const AddrLabelExpr *ALE) {
747 JOS.attribute("name", ALE->getLabel()->getName());
748 JOS.attribute("labelDeclId", createPointerRepresentation(ALE->getLabel()));
749}
750
751void JSONNodeDumper::VisitIntegerLiteral(const IntegerLiteral *IL) {
752 JOS.attribute("value",
753 IL->getValue().toString(
754 /*Radix=*/10, IL->getType()->isSignedIntegerType()));
755}
756void JSONNodeDumper::VisitCharacterLiteral(const CharacterLiteral *CL) {
757 // FIXME: This should probably print the character literal as a string,
Aaron Ballman4aee1b52019-05-20 20:01:45 +0000758 // rather than as a numerical value. It would be nice if the behavior matched
759 // what we do to print a string literal; right now, it is impossible to tell
760 // the difference between 'a' and L'a' in C from the JSON output.
Aaron Ballman2ce598a2019-05-13 21:39:55 +0000761 JOS.attribute("value", CL->getValue());
762}
763void JSONNodeDumper::VisitFixedPointLiteral(const FixedPointLiteral *FPL) {
764 JOS.attribute("value", FPL->getValueAsString(/*Radix=*/10));
765}
766void JSONNodeDumper::VisitFloatingLiteral(const FloatingLiteral *FL) {
767 JOS.attribute("value", FL->getValueAsApproximateDouble());
768}
769void JSONNodeDumper::VisitStringLiteral(const StringLiteral *SL) {
770 std::string Buffer;
771 llvm::raw_string_ostream SS(Buffer);
772 SL->outputString(SS);
773 JOS.attribute("value", SS.str());
774}
775void JSONNodeDumper::VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *BLE) {
776 JOS.attribute("value", BLE->getValue());
777}
778
779void JSONNodeDumper::VisitIfStmt(const IfStmt *IS) {
780 attributeOnlyIfTrue("hasInit", IS->hasInitStorage());
781 attributeOnlyIfTrue("hasVar", IS->hasVarStorage());
782 attributeOnlyIfTrue("hasElse", IS->hasElseStorage());
783 attributeOnlyIfTrue("isConstexpr", IS->isConstexpr());
784}
785
786void JSONNodeDumper::VisitSwitchStmt(const SwitchStmt *SS) {
787 attributeOnlyIfTrue("hasInit", SS->hasInitStorage());
788 attributeOnlyIfTrue("hasVar", SS->hasVarStorage());
789}
790void JSONNodeDumper::VisitCaseStmt(const CaseStmt *CS) {
791 attributeOnlyIfTrue("isGNURange", CS->caseStmtIsGNURange());
792}
793
794void JSONNodeDumper::VisitLabelStmt(const LabelStmt *LS) {
795 JOS.attribute("name", LS->getName());
796 JOS.attribute("declId", createPointerRepresentation(LS->getDecl()));
797}
798void JSONNodeDumper::VisitGotoStmt(const GotoStmt *GS) {
799 JOS.attribute("targetLabelDeclId",
800 createPointerRepresentation(GS->getLabel()));
801}
802
803void JSONNodeDumper::VisitWhileStmt(const WhileStmt *WS) {
804 attributeOnlyIfTrue("hasVar", WS->hasVarStorage());
805}