189f6b26fSZixu Wang //===- ExtractAPI/Serialization/SymbolGraphSerializer.cpp -------*- C++ -*-===//
289f6b26fSZixu Wang //
389f6b26fSZixu Wang // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
489f6b26fSZixu Wang // See https://llvm.org/LICENSE.txt for license information.
589f6b26fSZixu Wang // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
689f6b26fSZixu Wang //
789f6b26fSZixu Wang //===----------------------------------------------------------------------===//
889f6b26fSZixu Wang ///
989f6b26fSZixu Wang /// \file
1089f6b26fSZixu Wang /// This file implements the SymbolGraphSerializer.
1189f6b26fSZixu Wang ///
1289f6b26fSZixu Wang //===----------------------------------------------------------------------===//
1389f6b26fSZixu Wang 
1489f6b26fSZixu Wang #include "clang/ExtractAPI/Serialization/SymbolGraphSerializer.h"
1589f6b26fSZixu Wang #include "clang/Basic/Version.h"
1689f6b26fSZixu Wang #include "clang/ExtractAPI/API.h"
1780ae3665SDaniel Grumberg #include "clang/ExtractAPI/DeclarationFragments.h"
1889f6b26fSZixu Wang #include "llvm/Support/JSON.h"
1989f6b26fSZixu Wang #include "llvm/Support/Path.h"
2089f6b26fSZixu Wang #include "llvm/Support/VersionTuple.h"
21*236b6a0eSDaniel Grumberg #include <type_traits>
2289f6b26fSZixu Wang 
2389f6b26fSZixu Wang using namespace clang;
2489f6b26fSZixu Wang using namespace clang::extractapi;
2589f6b26fSZixu Wang using namespace llvm;
2689f6b26fSZixu Wang using namespace llvm::json;
2789f6b26fSZixu Wang 
2889f6b26fSZixu Wang namespace {
2989f6b26fSZixu Wang 
3089f6b26fSZixu Wang /// Helper function to inject a JSON object \p Obj into another object \p Paren
3189f6b26fSZixu Wang /// at position \p Key.
3289f6b26fSZixu Wang void serializeObject(Object &Paren, StringRef Key, Optional<Object> Obj) {
3389f6b26fSZixu Wang   if (Obj)
3489f6b26fSZixu Wang     Paren[Key] = std::move(Obj.getValue());
3589f6b26fSZixu Wang }
3689f6b26fSZixu Wang 
3789f6b26fSZixu Wang /// Helper function to inject a JSON array \p Array into object \p Paren at
3889f6b26fSZixu Wang /// position \p Key.
3989f6b26fSZixu Wang void serializeArray(Object &Paren, StringRef Key, Optional<Array> Array) {
4089f6b26fSZixu Wang   if (Array)
4189f6b26fSZixu Wang     Paren[Key] = std::move(Array.getValue());
4289f6b26fSZixu Wang }
4389f6b26fSZixu Wang 
4489f6b26fSZixu Wang /// Serialize a \c VersionTuple \p V with the Symbol Graph semantic version
4589f6b26fSZixu Wang /// format.
4689f6b26fSZixu Wang ///
4789f6b26fSZixu Wang /// A semantic version object contains three numeric fields, representing the
4889f6b26fSZixu Wang /// \c major, \c minor, and \c patch parts of the version tuple.
4989f6b26fSZixu Wang /// For example version tuple 1.0.3 is serialized as:
5089f6b26fSZixu Wang /// \code
5189f6b26fSZixu Wang ///   {
5289f6b26fSZixu Wang ///     "major" : 1,
5389f6b26fSZixu Wang ///     "minor" : 0,
5489f6b26fSZixu Wang ///     "patch" : 3
5589f6b26fSZixu Wang ///   }
5689f6b26fSZixu Wang /// \endcode
5789f6b26fSZixu Wang ///
5889f6b26fSZixu Wang /// \returns \c None if the version \p V is empty, or an \c Object containing
5989f6b26fSZixu Wang /// the semantic version representation of \p V.
6089f6b26fSZixu Wang Optional<Object> serializeSemanticVersion(const VersionTuple &V) {
6189f6b26fSZixu Wang   if (V.empty())
6289f6b26fSZixu Wang     return None;
6389f6b26fSZixu Wang 
6489f6b26fSZixu Wang   Object Version;
6589f6b26fSZixu Wang   Version["major"] = V.getMajor();
6689f6b26fSZixu Wang   Version["minor"] = V.getMinor().getValueOr(0);
6789f6b26fSZixu Wang   Version["patch"] = V.getSubminor().getValueOr(0);
6889f6b26fSZixu Wang   return Version;
6989f6b26fSZixu Wang }
7089f6b26fSZixu Wang 
7189f6b26fSZixu Wang /// Serialize the OS information in the Symbol Graph platform property.
7289f6b26fSZixu Wang ///
7389f6b26fSZixu Wang /// The OS information in Symbol Graph contains the \c name of the OS, and an
7489f6b26fSZixu Wang /// optional \c minimumVersion semantic version field.
7589f6b26fSZixu Wang Object serializeOperatingSystem(const Triple &T) {
7689f6b26fSZixu Wang   Object OS;
7789f6b26fSZixu Wang   OS["name"] = T.getOSTypeName(T.getOS());
7889f6b26fSZixu Wang   serializeObject(OS, "minimumVersion",
7989f6b26fSZixu Wang                   serializeSemanticVersion(T.getMinimumSupportedOSVersion()));
8089f6b26fSZixu Wang   return OS;
8189f6b26fSZixu Wang }
8289f6b26fSZixu Wang 
8389f6b26fSZixu Wang /// Serialize the platform information in the Symbol Graph module section.
8489f6b26fSZixu Wang ///
8589f6b26fSZixu Wang /// The platform object describes a target platform triple in corresponding
8689f6b26fSZixu Wang /// three fields: \c architecture, \c vendor, and \c operatingSystem.
8789f6b26fSZixu Wang Object serializePlatform(const Triple &T) {
8889f6b26fSZixu Wang   Object Platform;
8989f6b26fSZixu Wang   Platform["architecture"] = T.getArchName();
9089f6b26fSZixu Wang   Platform["vendor"] = T.getVendorName();
9189f6b26fSZixu Wang   Platform["operatingSystem"] = serializeOperatingSystem(T);
9289f6b26fSZixu Wang   return Platform;
9389f6b26fSZixu Wang }
9489f6b26fSZixu Wang 
9528d79314SDaniel Grumberg /// Serialize a source position.
9628d79314SDaniel Grumberg Object serializeSourcePosition(const PresumedLoc &Loc) {
9789f6b26fSZixu Wang   assert(Loc.isValid() && "invalid source position");
9889f6b26fSZixu Wang 
9989f6b26fSZixu Wang   Object SourcePosition;
10089f6b26fSZixu Wang   SourcePosition["line"] = Loc.getLine();
10189f6b26fSZixu Wang   SourcePosition["character"] = Loc.getColumn();
10289f6b26fSZixu Wang 
10328d79314SDaniel Grumberg   return SourcePosition;
10428d79314SDaniel Grumberg }
10528d79314SDaniel Grumberg 
10628d79314SDaniel Grumberg /// Serialize a source location in file.
10728d79314SDaniel Grumberg ///
10828d79314SDaniel Grumberg /// \param Loc The presumed location to serialize.
10928d79314SDaniel Grumberg /// \param IncludeFileURI If true, include the file path of \p Loc as a URI.
11028d79314SDaniel Grumberg /// Defaults to false.
11128d79314SDaniel Grumberg Object serializeSourceLocation(const PresumedLoc &Loc,
11228d79314SDaniel Grumberg                                bool IncludeFileURI = false) {
11328d79314SDaniel Grumberg   Object SourceLocation;
11428d79314SDaniel Grumberg   serializeObject(SourceLocation, "position", serializeSourcePosition(Loc));
11528d79314SDaniel Grumberg 
11689f6b26fSZixu Wang   if (IncludeFileURI) {
11789f6b26fSZixu Wang     std::string FileURI = "file://";
11889f6b26fSZixu Wang     // Normalize file path to use forward slashes for the URI.
11989f6b26fSZixu Wang     FileURI += sys::path::convert_to_slash(Loc.getFilename());
12028d79314SDaniel Grumberg     SourceLocation["uri"] = FileURI;
12189f6b26fSZixu Wang   }
12289f6b26fSZixu Wang 
12328d79314SDaniel Grumberg   return SourceLocation;
12489f6b26fSZixu Wang }
12589f6b26fSZixu Wang 
12689f6b26fSZixu Wang /// Serialize a source range with begin and end locations.
12789f6b26fSZixu Wang Object serializeSourceRange(const PresumedLoc &BeginLoc,
12889f6b26fSZixu Wang                             const PresumedLoc &EndLoc) {
12989f6b26fSZixu Wang   Object SourceRange;
13089f6b26fSZixu Wang   serializeObject(SourceRange, "start", serializeSourcePosition(BeginLoc));
13189f6b26fSZixu Wang   serializeObject(SourceRange, "end", serializeSourcePosition(EndLoc));
13289f6b26fSZixu Wang   return SourceRange;
13389f6b26fSZixu Wang }
13489f6b26fSZixu Wang 
13589f6b26fSZixu Wang /// Serialize the availability attributes of a symbol.
13689f6b26fSZixu Wang ///
13789f6b26fSZixu Wang /// Availability information contains the introduced, deprecated, and obsoleted
13889f6b26fSZixu Wang /// versions of the symbol as semantic versions, if not default.
13989f6b26fSZixu Wang /// Availability information also contains flags to indicate if the symbol is
14089f6b26fSZixu Wang /// unconditionally unavailable or deprecated,
14189f6b26fSZixu Wang /// i.e. \c __attribute__((unavailable)) and \c __attribute__((deprecated)).
14289f6b26fSZixu Wang ///
14389f6b26fSZixu Wang /// \returns \c None if the symbol has default availability attributes, or
14489f6b26fSZixu Wang /// an \c Object containing the formatted availability information.
14589f6b26fSZixu Wang Optional<Object> serializeAvailability(const AvailabilityInfo &Avail) {
14689f6b26fSZixu Wang   if (Avail.isDefault())
14789f6b26fSZixu Wang     return None;
14889f6b26fSZixu Wang 
14989f6b26fSZixu Wang   Object Availbility;
15089f6b26fSZixu Wang   serializeObject(Availbility, "introducedVersion",
15189f6b26fSZixu Wang                   serializeSemanticVersion(Avail.Introduced));
15289f6b26fSZixu Wang   serializeObject(Availbility, "deprecatedVersion",
15389f6b26fSZixu Wang                   serializeSemanticVersion(Avail.Deprecated));
15489f6b26fSZixu Wang   serializeObject(Availbility, "obsoletedVersion",
15589f6b26fSZixu Wang                   serializeSemanticVersion(Avail.Obsoleted));
15689f6b26fSZixu Wang   if (Avail.isUnavailable())
15789f6b26fSZixu Wang     Availbility["isUnconditionallyUnavailable"] = true;
15889f6b26fSZixu Wang   if (Avail.isUnconditionallyDeprecated())
15989f6b26fSZixu Wang     Availbility["isUnconditionallyDeprecated"] = true;
16089f6b26fSZixu Wang 
16189f6b26fSZixu Wang   return Availbility;
16289f6b26fSZixu Wang }
16389f6b26fSZixu Wang 
16415bf0e56SZixu Wang /// Get the language name string for interface language references.
16515bf0e56SZixu Wang StringRef getLanguageName(Language Lang) {
16615bf0e56SZixu Wang   switch (Lang) {
16789f6b26fSZixu Wang   case Language::C:
16889f6b26fSZixu Wang     return "c";
16989f6b26fSZixu Wang   case Language::ObjC:
170b62d4021SZixu Wang     return "objective-c";
17189f6b26fSZixu Wang 
17289f6b26fSZixu Wang   // Unsupported language currently
17389f6b26fSZixu Wang   case Language::CXX:
17489f6b26fSZixu Wang   case Language::ObjCXX:
17589f6b26fSZixu Wang   case Language::OpenCL:
17689f6b26fSZixu Wang   case Language::OpenCLCXX:
17789f6b26fSZixu Wang   case Language::CUDA:
17889f6b26fSZixu Wang   case Language::RenderScript:
17989f6b26fSZixu Wang   case Language::HIP:
180d394f9f8SChris Bieneman   case Language::HLSL:
18189f6b26fSZixu Wang 
18289f6b26fSZixu Wang   // Languages that the frontend cannot parse and compile
18389f6b26fSZixu Wang   case Language::Unknown:
18489f6b26fSZixu Wang   case Language::Asm:
18589f6b26fSZixu Wang   case Language::LLVM_IR:
18689f6b26fSZixu Wang     llvm_unreachable("Unsupported language kind");
18789f6b26fSZixu Wang   }
18889f6b26fSZixu Wang 
18989f6b26fSZixu Wang   llvm_unreachable("Unhandled language kind");
19089f6b26fSZixu Wang }
19189f6b26fSZixu Wang 
19289f6b26fSZixu Wang /// Serialize the identifier object as specified by the Symbol Graph format.
19389f6b26fSZixu Wang ///
19489f6b26fSZixu Wang /// The identifier property of a symbol contains the USR for precise and unique
19589f6b26fSZixu Wang /// references, and the interface language name.
19615bf0e56SZixu Wang Object serializeIdentifier(const APIRecord &Record, Language Lang) {
19789f6b26fSZixu Wang   Object Identifier;
19889f6b26fSZixu Wang   Identifier["precise"] = Record.USR;
19915bf0e56SZixu Wang   Identifier["interfaceLanguage"] = getLanguageName(Lang);
20089f6b26fSZixu Wang 
20189f6b26fSZixu Wang   return Identifier;
20289f6b26fSZixu Wang }
20389f6b26fSZixu Wang 
20489f6b26fSZixu Wang /// Serialize the documentation comments attached to a symbol, as specified by
20589f6b26fSZixu Wang /// the Symbol Graph format.
20689f6b26fSZixu Wang ///
20789f6b26fSZixu Wang /// The Symbol Graph \c docComment object contains an array of lines. Each line
20889f6b26fSZixu Wang /// represents one line of striped documentation comment, with source range
20989f6b26fSZixu Wang /// information.
21089f6b26fSZixu Wang /// e.g.
21189f6b26fSZixu Wang /// \code
21289f6b26fSZixu Wang ///   /// This is a documentation comment
21389f6b26fSZixu Wang ///       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~'  First line.
21489f6b26fSZixu Wang ///   ///     with multiple lines.
21589f6b26fSZixu Wang ///       ^~~~~~~~~~~~~~~~~~~~~~~'         Second line.
21689f6b26fSZixu Wang /// \endcode
21789f6b26fSZixu Wang ///
21889f6b26fSZixu Wang /// \returns \c None if \p Comment is empty, or an \c Object containing the
21989f6b26fSZixu Wang /// formatted lines.
22089f6b26fSZixu Wang Optional<Object> serializeDocComment(const DocComment &Comment) {
22189f6b26fSZixu Wang   if (Comment.empty())
22289f6b26fSZixu Wang     return None;
22389f6b26fSZixu Wang 
22489f6b26fSZixu Wang   Object DocComment;
22589f6b26fSZixu Wang   Array LinesArray;
22689f6b26fSZixu Wang   for (const auto &CommentLine : Comment) {
22789f6b26fSZixu Wang     Object Line;
22889f6b26fSZixu Wang     Line["text"] = CommentLine.Text;
22989f6b26fSZixu Wang     serializeObject(Line, "range",
23089f6b26fSZixu Wang                     serializeSourceRange(CommentLine.Begin, CommentLine.End));
23189f6b26fSZixu Wang     LinesArray.emplace_back(std::move(Line));
23289f6b26fSZixu Wang   }
23389f6b26fSZixu Wang   serializeArray(DocComment, "lines", LinesArray);
23489f6b26fSZixu Wang 
23589f6b26fSZixu Wang   return DocComment;
23689f6b26fSZixu Wang }
23789f6b26fSZixu Wang 
23889f6b26fSZixu Wang /// Serialize the declaration fragments of a symbol.
23989f6b26fSZixu Wang ///
24089f6b26fSZixu Wang /// The Symbol Graph declaration fragments is an array of tagged important
24189f6b26fSZixu Wang /// parts of a symbol's declaration. The fragments sequence can be joined to
24289f6b26fSZixu Wang /// form spans of declaration text, with attached information useful for
24389f6b26fSZixu Wang /// purposes like syntax-highlighting etc. For example:
24489f6b26fSZixu Wang /// \code
24589f6b26fSZixu Wang ///   const int pi; -> "declarationFragments" : [
24689f6b26fSZixu Wang ///                      {
24789f6b26fSZixu Wang ///                        "kind" : "keyword",
24889f6b26fSZixu Wang ///                        "spelling" : "const"
24989f6b26fSZixu Wang ///                      },
25089f6b26fSZixu Wang ///                      {
25189f6b26fSZixu Wang ///                        "kind" : "text",
25289f6b26fSZixu Wang ///                        "spelling" : " "
25389f6b26fSZixu Wang ///                      },
25489f6b26fSZixu Wang ///                      {
25589f6b26fSZixu Wang ///                        "kind" : "typeIdentifier",
25689f6b26fSZixu Wang ///                        "preciseIdentifier" : "c:I",
25789f6b26fSZixu Wang ///                        "spelling" : "int"
25889f6b26fSZixu Wang ///                      },
25989f6b26fSZixu Wang ///                      {
26089f6b26fSZixu Wang ///                        "kind" : "text",
26189f6b26fSZixu Wang ///                        "spelling" : " "
26289f6b26fSZixu Wang ///                      },
26389f6b26fSZixu Wang ///                      {
26489f6b26fSZixu Wang ///                        "kind" : "identifier",
26589f6b26fSZixu Wang ///                        "spelling" : "pi"
26689f6b26fSZixu Wang ///                      }
26789f6b26fSZixu Wang ///                    ]
26889f6b26fSZixu Wang /// \endcode
26989f6b26fSZixu Wang ///
27089f6b26fSZixu Wang /// \returns \c None if \p DF is empty, or an \c Array containing the formatted
27189f6b26fSZixu Wang /// declaration fragments array.
27289f6b26fSZixu Wang Optional<Array> serializeDeclarationFragments(const DeclarationFragments &DF) {
27389f6b26fSZixu Wang   if (DF.getFragments().empty())
27489f6b26fSZixu Wang     return None;
27589f6b26fSZixu Wang 
27689f6b26fSZixu Wang   Array Fragments;
27789f6b26fSZixu Wang   for (const auto &F : DF.getFragments()) {
27889f6b26fSZixu Wang     Object Fragment;
27989f6b26fSZixu Wang     Fragment["spelling"] = F.Spelling;
28089f6b26fSZixu Wang     Fragment["kind"] = DeclarationFragments::getFragmentKindString(F.Kind);
28189f6b26fSZixu Wang     if (!F.PreciseIdentifier.empty())
28289f6b26fSZixu Wang       Fragment["preciseIdentifier"] = F.PreciseIdentifier;
28389f6b26fSZixu Wang     Fragments.emplace_back(std::move(Fragment));
28489f6b26fSZixu Wang   }
28589f6b26fSZixu Wang 
28689f6b26fSZixu Wang   return Fragments;
28789f6b26fSZixu Wang }
28889f6b26fSZixu Wang 
28989f6b26fSZixu Wang /// Serialize the \c names field of a symbol as specified by the Symbol Graph
29089f6b26fSZixu Wang /// format.
29189f6b26fSZixu Wang ///
29289f6b26fSZixu Wang /// The Symbol Graph names field contains multiple representations of a symbol
29389f6b26fSZixu Wang /// that can be used for different applications:
29489f6b26fSZixu Wang ///   - \c title : The simple declared name of the symbol;
29589f6b26fSZixu Wang ///   - \c subHeading : An array of declaration fragments that provides tags,
29689f6b26fSZixu Wang ///     and potentially more tokens (for example the \c +/- symbol for
29789f6b26fSZixu Wang ///     Objective-C methods). Can be used as sub-headings for documentation.
29889f6b26fSZixu Wang Object serializeNames(const APIRecord &Record) {
29989f6b26fSZixu Wang   Object Names;
30089f6b26fSZixu Wang   Names["title"] = Record.Name;
30189f6b26fSZixu Wang   serializeArray(Names, "subHeading",
30289f6b26fSZixu Wang                  serializeDeclarationFragments(Record.SubHeading));
30380ae3665SDaniel Grumberg   DeclarationFragments NavigatorFragments;
30480ae3665SDaniel Grumberg   NavigatorFragments.append(Record.Name,
30580ae3665SDaniel Grumberg                             DeclarationFragments::FragmentKind::Identifier,
30680ae3665SDaniel Grumberg                             /*PreciseIdentifier*/ "");
30780ae3665SDaniel Grumberg   serializeArray(Names, "navigator",
30880ae3665SDaniel Grumberg                  serializeDeclarationFragments(NavigatorFragments));
30989f6b26fSZixu Wang 
31089f6b26fSZixu Wang   return Names;
31189f6b26fSZixu Wang }
31289f6b26fSZixu Wang 
31389f6b26fSZixu Wang /// Serialize the symbol kind information.
31489f6b26fSZixu Wang ///
31589f6b26fSZixu Wang /// The Symbol Graph symbol kind property contains a shorthand \c identifier
31689f6b26fSZixu Wang /// which is prefixed by the source language name, useful for tooling to parse
31789f6b26fSZixu Wang /// the kind, and a \c displayName for rendering human-readable names.
31815bf0e56SZixu Wang Object serializeSymbolKind(const APIRecord &Record, Language Lang) {
31915bf0e56SZixu Wang   auto AddLangPrefix = [&Lang](StringRef S) -> std::string {
32015bf0e56SZixu Wang     return (getLanguageName(Lang) + "." + S).str();
32171b4c226SZixu Wang   };
32271b4c226SZixu Wang 
32389f6b26fSZixu Wang   Object Kind;
32489f6b26fSZixu Wang   switch (Record.getKind()) {
325*236b6a0eSDaniel Grumberg   case APIRecord::RK_GlobalFunction:
32671b4c226SZixu Wang     Kind["identifier"] = AddLangPrefix("func");
32789f6b26fSZixu Wang     Kind["displayName"] = "Function";
32889f6b26fSZixu Wang     break;
329*236b6a0eSDaniel Grumberg   case APIRecord::RK_GlobalVariable:
33071b4c226SZixu Wang     Kind["identifier"] = AddLangPrefix("var");
33189f6b26fSZixu Wang     Kind["displayName"] = "Global Variable";
33289f6b26fSZixu Wang     break;
33371b4c226SZixu Wang   case APIRecord::RK_EnumConstant:
33471b4c226SZixu Wang     Kind["identifier"] = AddLangPrefix("enum.case");
33571b4c226SZixu Wang     Kind["displayName"] = "Enumeration Case";
33671b4c226SZixu Wang     break;
33771b4c226SZixu Wang   case APIRecord::RK_Enum:
33871b4c226SZixu Wang     Kind["identifier"] = AddLangPrefix("enum");
33971b4c226SZixu Wang     Kind["displayName"] = "Enumeration";
34071b4c226SZixu Wang     break;
3415bb5704cSZixu Wang   case APIRecord::RK_StructField:
3425bb5704cSZixu Wang     Kind["identifier"] = AddLangPrefix("property");
3435bb5704cSZixu Wang     Kind["displayName"] = "Instance Property";
3445bb5704cSZixu Wang     break;
3455bb5704cSZixu Wang   case APIRecord::RK_Struct:
3465bb5704cSZixu Wang     Kind["identifier"] = AddLangPrefix("struct");
3475bb5704cSZixu Wang     Kind["displayName"] = "Structure";
3485bb5704cSZixu Wang     break;
3499b36e126SZixu Wang   case APIRecord::RK_ObjCIvar:
3509b36e126SZixu Wang     Kind["identifier"] = AddLangPrefix("ivar");
3519b36e126SZixu Wang     Kind["displayName"] = "Instance Variable";
3529b36e126SZixu Wang     break;
3539b36e126SZixu Wang   case APIRecord::RK_ObjCMethod:
3549b36e126SZixu Wang     if (dyn_cast<ObjCMethodRecord>(&Record)->IsInstanceMethod) {
3559b36e126SZixu Wang       Kind["identifier"] = AddLangPrefix("method");
3569b36e126SZixu Wang       Kind["displayName"] = "Instance Method";
3579b36e126SZixu Wang     } else {
3589b36e126SZixu Wang       Kind["identifier"] = AddLangPrefix("type.method");
3599b36e126SZixu Wang       Kind["displayName"] = "Type Method";
3609b36e126SZixu Wang     }
3619b36e126SZixu Wang     break;
3629b36e126SZixu Wang   case APIRecord::RK_ObjCProperty:
3639b36e126SZixu Wang     Kind["identifier"] = AddLangPrefix("property");
3649b36e126SZixu Wang     Kind["displayName"] = "Instance Property";
3659b36e126SZixu Wang     break;
3669b36e126SZixu Wang   case APIRecord::RK_ObjCInterface:
3679b36e126SZixu Wang     Kind["identifier"] = AddLangPrefix("class");
3689b36e126SZixu Wang     Kind["displayName"] = "Class";
3699b36e126SZixu Wang     break;
370178aad9bSZixu Wang   case APIRecord::RK_ObjCCategory:
371178aad9bSZixu Wang     // We don't serialize out standalone Objective-C category symbols yet.
372178aad9bSZixu Wang     llvm_unreachable("Serializing standalone Objective-C category symbols is "
373178aad9bSZixu Wang                      "not supported.");
374178aad9bSZixu Wang     break;
375d1d34bafSZixu Wang   case APIRecord::RK_ObjCProtocol:
376d1d34bafSZixu Wang     Kind["identifier"] = AddLangPrefix("protocol");
377d1d34bafSZixu Wang     Kind["displayName"] = "Protocol";
378d1d34bafSZixu Wang     break;
379529a0570SDaniel Grumberg   case APIRecord::RK_MacroDefinition:
380529a0570SDaniel Grumberg     Kind["identifier"] = AddLangPrefix("macro");
381529a0570SDaniel Grumberg     Kind["displayName"] = "Macro";
3829fc45ca0SDaniel Grumberg     break;
3839fc45ca0SDaniel Grumberg   case APIRecord::RK_Typedef:
3849fc45ca0SDaniel Grumberg     Kind["identifier"] = AddLangPrefix("typealias");
3859fc45ca0SDaniel Grumberg     Kind["displayName"] = "Type Alias";
3869fc45ca0SDaniel Grumberg     break;
38771b4c226SZixu Wang   }
38889f6b26fSZixu Wang 
38989f6b26fSZixu Wang   return Kind;
39089f6b26fSZixu Wang }
39189f6b26fSZixu Wang 
392*236b6a0eSDaniel Grumberg template <typename RecordTy>
393*236b6a0eSDaniel Grumberg Optional<Object> serializeFunctionSignatureMixinImpl(const RecordTy &Record,
394*236b6a0eSDaniel Grumberg                                                      std::true_type) {
395*236b6a0eSDaniel Grumberg   const auto &FS = Record.Signature;
396*236b6a0eSDaniel Grumberg   if (FS.empty())
397*236b6a0eSDaniel Grumberg     return None;
398*236b6a0eSDaniel Grumberg 
399*236b6a0eSDaniel Grumberg   Object Signature;
400*236b6a0eSDaniel Grumberg   serializeArray(Signature, "returns",
401*236b6a0eSDaniel Grumberg                  serializeDeclarationFragments(FS.getReturnType()));
402*236b6a0eSDaniel Grumberg 
403*236b6a0eSDaniel Grumberg   Array Parameters;
404*236b6a0eSDaniel Grumberg   for (const auto &P : FS.getParameters()) {
405*236b6a0eSDaniel Grumberg     Object Parameter;
406*236b6a0eSDaniel Grumberg     Parameter["name"] = P.Name;
407*236b6a0eSDaniel Grumberg     serializeArray(Parameter, "declarationFragments",
408*236b6a0eSDaniel Grumberg                    serializeDeclarationFragments(P.Fragments));
409*236b6a0eSDaniel Grumberg     Parameters.emplace_back(std::move(Parameter));
410*236b6a0eSDaniel Grumberg   }
411*236b6a0eSDaniel Grumberg 
412*236b6a0eSDaniel Grumberg   if (!Parameters.empty())
413*236b6a0eSDaniel Grumberg     Signature["parameters"] = std::move(Parameters);
414*236b6a0eSDaniel Grumberg 
415*236b6a0eSDaniel Grumberg   return Signature;
416*236b6a0eSDaniel Grumberg }
417*236b6a0eSDaniel Grumberg 
418*236b6a0eSDaniel Grumberg template <typename RecordTy>
419*236b6a0eSDaniel Grumberg Optional<Object> serializeFunctionSignatureMixinImpl(const RecordTy &Record,
420*236b6a0eSDaniel Grumberg                                                      std::false_type) {
421*236b6a0eSDaniel Grumberg   return None;
422*236b6a0eSDaniel Grumberg }
423*236b6a0eSDaniel Grumberg 
424*236b6a0eSDaniel Grumberg /// Serialize the function signature field, as specified by the
425*236b6a0eSDaniel Grumberg /// Symbol Graph format.
426*236b6a0eSDaniel Grumberg ///
427*236b6a0eSDaniel Grumberg /// The Symbol Graph function signature property contains two arrays.
428*236b6a0eSDaniel Grumberg ///   - The \c returns array is the declaration fragments of the return type;
429*236b6a0eSDaniel Grumberg ///   - The \c parameters array contains names and declaration fragments of the
430*236b6a0eSDaniel Grumberg ///     parameters.
431*236b6a0eSDaniel Grumberg ///
432*236b6a0eSDaniel Grumberg /// \returns \c None if \p FS is empty, or an \c Object containing the
433*236b6a0eSDaniel Grumberg /// formatted function signature.
434*236b6a0eSDaniel Grumberg template <typename RecordTy>
435*236b6a0eSDaniel Grumberg void serializeFunctionSignatureMixin(Object &Paren, const RecordTy &Record) {
436*236b6a0eSDaniel Grumberg   serializeObject(Paren, "functionSignature",
437*236b6a0eSDaniel Grumberg                   serializeFunctionSignatureMixinImpl(
438*236b6a0eSDaniel Grumberg                       Record, has_function_signature<RecordTy>()));
439*236b6a0eSDaniel Grumberg }
440*236b6a0eSDaniel Grumberg 
44189f6b26fSZixu Wang } // namespace
44289f6b26fSZixu Wang 
44389f6b26fSZixu Wang void SymbolGraphSerializer::anchor() {}
44489f6b26fSZixu Wang 
44589f6b26fSZixu Wang /// Defines the format version emitted by SymbolGraphSerializer.
44689f6b26fSZixu Wang const VersionTuple SymbolGraphSerializer::FormatVersion{0, 5, 3};
44789f6b26fSZixu Wang 
44889f6b26fSZixu Wang Object SymbolGraphSerializer::serializeMetadata() const {
44989f6b26fSZixu Wang   Object Metadata;
45089f6b26fSZixu Wang   serializeObject(Metadata, "formatVersion",
45189f6b26fSZixu Wang                   serializeSemanticVersion(FormatVersion));
45289f6b26fSZixu Wang   Metadata["generator"] = clang::getClangFullVersion();
45389f6b26fSZixu Wang   return Metadata;
45489f6b26fSZixu Wang }
45589f6b26fSZixu Wang 
45689f6b26fSZixu Wang Object SymbolGraphSerializer::serializeModule() const {
45789f6b26fSZixu Wang   Object Module;
4585ef2ec7eSDaniel Grumberg   // The user is expected to always pass `--product-name=` on the command line
4595ef2ec7eSDaniel Grumberg   // to populate this field.
4605ef2ec7eSDaniel Grumberg   Module["name"] = ProductName;
46189f6b26fSZixu Wang   serializeObject(Module, "platform", serializePlatform(API.getTarget()));
46289f6b26fSZixu Wang   return Module;
46389f6b26fSZixu Wang }
46489f6b26fSZixu Wang 
46589f6b26fSZixu Wang bool SymbolGraphSerializer::shouldSkip(const APIRecord &Record) const {
46689f6b26fSZixu Wang   // Skip unconditionally unavailable symbols
46789f6b26fSZixu Wang   if (Record.Availability.isUnconditionallyUnavailable())
46889f6b26fSZixu Wang     return true;
46989f6b26fSZixu Wang 
47089f6b26fSZixu Wang   return false;
47189f6b26fSZixu Wang }
47289f6b26fSZixu Wang 
473*236b6a0eSDaniel Grumberg template <typename RecordTy>
47489f6b26fSZixu Wang Optional<Object>
475*236b6a0eSDaniel Grumberg SymbolGraphSerializer::serializeAPIRecord(const RecordTy &Record) const {
47689f6b26fSZixu Wang   if (shouldSkip(Record))
47789f6b26fSZixu Wang     return None;
47889f6b26fSZixu Wang 
47989f6b26fSZixu Wang   Object Obj;
48089f6b26fSZixu Wang   serializeObject(Obj, "identifier",
48115bf0e56SZixu Wang                   serializeIdentifier(Record, API.getLanguage()));
48215bf0e56SZixu Wang   serializeObject(Obj, "kind", serializeSymbolKind(Record, API.getLanguage()));
48389f6b26fSZixu Wang   serializeObject(Obj, "names", serializeNames(Record));
48489f6b26fSZixu Wang   serializeObject(
48589f6b26fSZixu Wang       Obj, "location",
48628d79314SDaniel Grumberg       serializeSourceLocation(Record.Location, /*IncludeFileURI=*/true));
48789f6b26fSZixu Wang   serializeObject(Obj, "availbility",
48889f6b26fSZixu Wang                   serializeAvailability(Record.Availability));
48989f6b26fSZixu Wang   serializeObject(Obj, "docComment", serializeDocComment(Record.Comment));
49089f6b26fSZixu Wang   serializeArray(Obj, "declarationFragments",
49189f6b26fSZixu Wang                  serializeDeclarationFragments(Record.Declaration));
49228d79314SDaniel Grumberg   // TODO: Once we keep track of symbol access information serialize it
49328d79314SDaniel Grumberg   // correctly here.
49428d79314SDaniel Grumberg   Obj["accessLevel"] = "public";
49528d79314SDaniel Grumberg   serializeArray(Obj, "pathComponents", Array(PathComponents));
49689f6b26fSZixu Wang 
497*236b6a0eSDaniel Grumberg   serializeFunctionSignatureMixin(Obj, Record);
498*236b6a0eSDaniel Grumberg 
49989f6b26fSZixu Wang   return Obj;
50089f6b26fSZixu Wang }
50189f6b26fSZixu Wang 
502178aad9bSZixu Wang template <typename MemberTy>
503178aad9bSZixu Wang void SymbolGraphSerializer::serializeMembers(
504178aad9bSZixu Wang     const APIRecord &Record,
505178aad9bSZixu Wang     const SmallVector<std::unique_ptr<MemberTy>> &Members) {
506178aad9bSZixu Wang   for (const auto &Member : Members) {
507178aad9bSZixu Wang     auto MemberPathComponentGuard = makePathComponentGuard(Member->Name);
508178aad9bSZixu Wang     auto MemberRecord = serializeAPIRecord(*Member);
509178aad9bSZixu Wang     if (!MemberRecord)
510178aad9bSZixu Wang       continue;
511178aad9bSZixu Wang 
512178aad9bSZixu Wang     Symbols.emplace_back(std::move(*MemberRecord));
513178aad9bSZixu Wang     serializeRelationship(RelationshipKind::MemberOf, *Member, Record);
514178aad9bSZixu Wang   }
515178aad9bSZixu Wang }
516178aad9bSZixu Wang 
51771b4c226SZixu Wang StringRef SymbolGraphSerializer::getRelationshipString(RelationshipKind Kind) {
51871b4c226SZixu Wang   switch (Kind) {
51971b4c226SZixu Wang   case RelationshipKind::MemberOf:
52071b4c226SZixu Wang     return "memberOf";
5219b36e126SZixu Wang   case RelationshipKind::InheritsFrom:
5229b36e126SZixu Wang     return "inheritsFrom";
5239b36e126SZixu Wang   case RelationshipKind::ConformsTo:
5249b36e126SZixu Wang     return "conformsTo";
52571b4c226SZixu Wang   }
52671b4c226SZixu Wang   llvm_unreachable("Unhandled relationship kind");
52771b4c226SZixu Wang }
52871b4c226SZixu Wang 
52971b4c226SZixu Wang void SymbolGraphSerializer::serializeRelationship(RelationshipKind Kind,
5309b36e126SZixu Wang                                                   SymbolReference Source,
5319b36e126SZixu Wang                                                   SymbolReference Target) {
53271b4c226SZixu Wang   Object Relationship;
53371b4c226SZixu Wang   Relationship["source"] = Source.USR;
53471b4c226SZixu Wang   Relationship["target"] = Target.USR;
53571b4c226SZixu Wang   Relationship["kind"] = getRelationshipString(Kind);
53671b4c226SZixu Wang 
53771b4c226SZixu Wang   Relationships.emplace_back(std::move(Relationship));
53871b4c226SZixu Wang }
53971b4c226SZixu Wang 
540*236b6a0eSDaniel Grumberg void SymbolGraphSerializer::serializeGlobalFunctionRecord(
541*236b6a0eSDaniel Grumberg     const GlobalFunctionRecord &Record) {
54228d79314SDaniel Grumberg   auto GlobalPathComponentGuard = makePathComponentGuard(Record.Name);
54328d79314SDaniel Grumberg 
54489f6b26fSZixu Wang   auto Obj = serializeAPIRecord(Record);
54589f6b26fSZixu Wang   if (!Obj)
54689f6b26fSZixu Wang     return;
54789f6b26fSZixu Wang 
548*236b6a0eSDaniel Grumberg   Symbols.emplace_back(std::move(*Obj));
549*236b6a0eSDaniel Grumberg }
550*236b6a0eSDaniel Grumberg 
551*236b6a0eSDaniel Grumberg void SymbolGraphSerializer::serializeGlobalVariableRecord(
552*236b6a0eSDaniel Grumberg     const GlobalVariableRecord &Record) {
553*236b6a0eSDaniel Grumberg   auto GlobalPathComponentGuard = makePathComponentGuard(Record.Name);
554*236b6a0eSDaniel Grumberg 
555*236b6a0eSDaniel Grumberg   auto Obj = serializeAPIRecord(Record);
556*236b6a0eSDaniel Grumberg   if (!Obj)
557*236b6a0eSDaniel Grumberg     return;
55889f6b26fSZixu Wang 
55989f6b26fSZixu Wang   Symbols.emplace_back(std::move(*Obj));
56089f6b26fSZixu Wang }
56189f6b26fSZixu Wang 
56271b4c226SZixu Wang void SymbolGraphSerializer::serializeEnumRecord(const EnumRecord &Record) {
56328d79314SDaniel Grumberg   auto EnumPathComponentGuard = makePathComponentGuard(Record.Name);
56471b4c226SZixu Wang   auto Enum = serializeAPIRecord(Record);
56571b4c226SZixu Wang   if (!Enum)
56671b4c226SZixu Wang     return;
56771b4c226SZixu Wang 
56871b4c226SZixu Wang   Symbols.emplace_back(std::move(*Enum));
569178aad9bSZixu Wang   serializeMembers(Record, Record.Constants);
57071b4c226SZixu Wang }
57171b4c226SZixu Wang 
5725bb5704cSZixu Wang void SymbolGraphSerializer::serializeStructRecord(const StructRecord &Record) {
57328d79314SDaniel Grumberg   auto StructPathComponentGuard = makePathComponentGuard(Record.Name);
5745bb5704cSZixu Wang   auto Struct = serializeAPIRecord(Record);
5755bb5704cSZixu Wang   if (!Struct)
5765bb5704cSZixu Wang     return;
5775bb5704cSZixu Wang 
5785bb5704cSZixu Wang   Symbols.emplace_back(std::move(*Struct));
579178aad9bSZixu Wang   serializeMembers(Record, Record.Fields);
5805bb5704cSZixu Wang }
5815bb5704cSZixu Wang 
5829b36e126SZixu Wang void SymbolGraphSerializer::serializeObjCContainerRecord(
5839b36e126SZixu Wang     const ObjCContainerRecord &Record) {
58428d79314SDaniel Grumberg   auto ObjCContainerPathComponentGuard = makePathComponentGuard(Record.Name);
5859b36e126SZixu Wang   auto ObjCContainer = serializeAPIRecord(Record);
5869b36e126SZixu Wang   if (!ObjCContainer)
5879b36e126SZixu Wang     return;
5889b36e126SZixu Wang 
5899b36e126SZixu Wang   Symbols.emplace_back(std::move(*ObjCContainer));
5909b36e126SZixu Wang 
591178aad9bSZixu Wang   serializeMembers(Record, Record.Ivars);
592178aad9bSZixu Wang   serializeMembers(Record, Record.Methods);
593178aad9bSZixu Wang   serializeMembers(Record, Record.Properties);
5949b36e126SZixu Wang 
5959b36e126SZixu Wang   for (const auto &Protocol : Record.Protocols)
5969b36e126SZixu Wang     // Record that Record conforms to Protocol.
5979b36e126SZixu Wang     serializeRelationship(RelationshipKind::ConformsTo, Record, Protocol);
5989b36e126SZixu Wang 
599178aad9bSZixu Wang   if (auto *ObjCInterface = dyn_cast<ObjCInterfaceRecord>(&Record)) {
6009b36e126SZixu Wang     if (!ObjCInterface->SuperClass.empty())
6019b36e126SZixu Wang       // If Record is an Objective-C interface record and it has a super class,
6029b36e126SZixu Wang       // record that Record is inherited from SuperClass.
6039b36e126SZixu Wang       serializeRelationship(RelationshipKind::InheritsFrom, Record,
6049b36e126SZixu Wang                             ObjCInterface->SuperClass);
605178aad9bSZixu Wang 
606178aad9bSZixu Wang     // Members of categories extending an interface are serialized as members of
607178aad9bSZixu Wang     // the interface.
608178aad9bSZixu Wang     for (const auto *Category : ObjCInterface->Categories) {
609178aad9bSZixu Wang       serializeMembers(Record, Category->Ivars);
610178aad9bSZixu Wang       serializeMembers(Record, Category->Methods);
611178aad9bSZixu Wang       serializeMembers(Record, Category->Properties);
612178aad9bSZixu Wang 
613178aad9bSZixu Wang       // Surface the protocols of the the category to the interface.
614178aad9bSZixu Wang       for (const auto &Protocol : Category->Protocols)
615178aad9bSZixu Wang         serializeRelationship(RelationshipKind::ConformsTo, Record, Protocol);
616178aad9bSZixu Wang     }
617178aad9bSZixu Wang   }
6189b36e126SZixu Wang }
6199b36e126SZixu Wang 
620529a0570SDaniel Grumberg void SymbolGraphSerializer::serializeMacroDefinitionRecord(
621529a0570SDaniel Grumberg     const MacroDefinitionRecord &Record) {
62228d79314SDaniel Grumberg   auto MacroPathComponentGuard = makePathComponentGuard(Record.Name);
623529a0570SDaniel Grumberg   auto Macro = serializeAPIRecord(Record);
62428d79314SDaniel Grumberg 
625529a0570SDaniel Grumberg   if (!Macro)
626529a0570SDaniel Grumberg     return;
627529a0570SDaniel Grumberg 
628529a0570SDaniel Grumberg   Symbols.emplace_back(std::move(*Macro));
629529a0570SDaniel Grumberg }
630529a0570SDaniel Grumberg 
6319fc45ca0SDaniel Grumberg void SymbolGraphSerializer::serializeTypedefRecord(
6329fc45ca0SDaniel Grumberg     const TypedefRecord &Record) {
6339fc45ca0SDaniel Grumberg   // Typedefs of anonymous types have their entries unified with the underlying
6349fc45ca0SDaniel Grumberg   // type.
6359fc45ca0SDaniel Grumberg   bool ShouldDrop = Record.UnderlyingType.Name.empty();
6369fc45ca0SDaniel Grumberg   // enums declared with `NS_OPTION` have a named enum and a named typedef, with
6379fc45ca0SDaniel Grumberg   // the same name
6389fc45ca0SDaniel Grumberg   ShouldDrop |= (Record.UnderlyingType.Name == Record.Name);
6399fc45ca0SDaniel Grumberg   if (ShouldDrop)
6409fc45ca0SDaniel Grumberg     return;
6419fc45ca0SDaniel Grumberg 
6429fc45ca0SDaniel Grumberg   auto TypedefPathComponentGuard = makePathComponentGuard(Record.Name);
6439fc45ca0SDaniel Grumberg   auto Typedef = serializeAPIRecord(Record);
6449fc45ca0SDaniel Grumberg   if (!Typedef)
6459fc45ca0SDaniel Grumberg     return;
6469fc45ca0SDaniel Grumberg 
6479fc45ca0SDaniel Grumberg   (*Typedef)["type"] = Record.UnderlyingType.USR;
6489fc45ca0SDaniel Grumberg 
6499fc45ca0SDaniel Grumberg   Symbols.emplace_back(std::move(*Typedef));
6509fc45ca0SDaniel Grumberg }
6519fc45ca0SDaniel Grumberg 
65228d79314SDaniel Grumberg SymbolGraphSerializer::PathComponentGuard
65328d79314SDaniel Grumberg SymbolGraphSerializer::makePathComponentGuard(StringRef Component) {
65428d79314SDaniel Grumberg   return PathComponentGuard(PathComponents, Component);
65528d79314SDaniel Grumberg }
65628d79314SDaniel Grumberg 
65789f6b26fSZixu Wang Object SymbolGraphSerializer::serialize() {
65889f6b26fSZixu Wang   Object Root;
65989f6b26fSZixu Wang   serializeObject(Root, "metadata", serializeMetadata());
66089f6b26fSZixu Wang   serializeObject(Root, "module", serializeModule());
66189f6b26fSZixu Wang 
662*236b6a0eSDaniel Grumberg   // Serialize global variables in the API set.
663*236b6a0eSDaniel Grumberg   for (const auto &GlobalVar : API.getGlobalVariables())
664*236b6a0eSDaniel Grumberg     serializeGlobalVariableRecord(*GlobalVar.second);
665*236b6a0eSDaniel Grumberg 
666*236b6a0eSDaniel Grumberg   for (const auto &GlobalFunction : API.getGlobalFunctions())
667*236b6a0eSDaniel Grumberg     serializeGlobalFunctionRecord(*GlobalFunction.second);
66889f6b26fSZixu Wang 
66971b4c226SZixu Wang   // Serialize enum records in the API set.
67071b4c226SZixu Wang   for (const auto &Enum : API.getEnums())
67171b4c226SZixu Wang     serializeEnumRecord(*Enum.second);
67271b4c226SZixu Wang 
6735bb5704cSZixu Wang   // Serialize struct records in the API set.
6745bb5704cSZixu Wang   for (const auto &Struct : API.getStructs())
6755bb5704cSZixu Wang     serializeStructRecord(*Struct.second);
6765bb5704cSZixu Wang 
6779b36e126SZixu Wang   // Serialize Objective-C interface records in the API set.
6789b36e126SZixu Wang   for (const auto &ObjCInterface : API.getObjCInterfaces())
6799b36e126SZixu Wang     serializeObjCContainerRecord(*ObjCInterface.second);
6809b36e126SZixu Wang 
681d1d34bafSZixu Wang   // Serialize Objective-C protocol records in the API set.
682d1d34bafSZixu Wang   for (const auto &ObjCProtocol : API.getObjCProtocols())
683d1d34bafSZixu Wang     serializeObjCContainerRecord(*ObjCProtocol.second);
684d1d34bafSZixu Wang 
685529a0570SDaniel Grumberg   for (const auto &Macro : API.getMacros())
686529a0570SDaniel Grumberg     serializeMacroDefinitionRecord(*Macro.second);
687529a0570SDaniel Grumberg 
6889fc45ca0SDaniel Grumberg   for (const auto &Typedef : API.getTypedefs())
6899fc45ca0SDaniel Grumberg     serializeTypedefRecord(*Typedef.second);
6909fc45ca0SDaniel Grumberg 
69189f6b26fSZixu Wang   Root["symbols"] = std::move(Symbols);
69228d79314SDaniel Grumberg   Root["relationships"] = std::move(Relationships);
69389f6b26fSZixu Wang 
69489f6b26fSZixu Wang   return Root;
69589f6b26fSZixu Wang }
69689f6b26fSZixu Wang 
69789f6b26fSZixu Wang void SymbolGraphSerializer::serialize(raw_ostream &os) {
69889f6b26fSZixu Wang   Object root = serialize();
69989f6b26fSZixu Wang   if (Options.Compact)
70089f6b26fSZixu Wang     os << formatv("{0}", Value(std::move(root))) << "\n";
70189f6b26fSZixu Wang   else
70289f6b26fSZixu Wang     os << formatv("{0:2}", Value(std::move(root))) << "\n";
70389f6b26fSZixu Wang }
704