Path: blob/main/contrib/llvm-project/llvm/lib/TableGen/JSONBackend.cpp
35232 views
//===- JSONBackend.cpp - Generate a JSON dump of all records. -*- C++ -*-=====//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This TableGen back end generates a machine-readable representation9// of all the classes and records defined by the input, in JSON format.10//11//===----------------------------------------------------------------------===//1213#include "llvm/Support/Casting.h"14#include "llvm/Support/Debug.h"15#include "llvm/Support/ErrorHandling.h"16#include "llvm/Support/JSON.h"17#include "llvm/TableGen/Error.h"18#include "llvm/TableGen/Record.h"1920#define DEBUG_TYPE "json-emitter"2122using namespace llvm;2324namespace {2526class JSONEmitter {27private:28RecordKeeper &Records;2930json::Value translateInit(const Init &I);3132public:33JSONEmitter(RecordKeeper &R);3435void run(raw_ostream &OS);36};3738} // end anonymous namespace3940JSONEmitter::JSONEmitter(RecordKeeper &R) : Records(R) {}4142json::Value JSONEmitter::translateInit(const Init &I) {4344// Init subclasses that we return as JSON primitive values of one45// kind or another.4647if (isa<UnsetInit>(&I)) {48return nullptr;49} else if (auto *Bit = dyn_cast<BitInit>(&I)) {50return Bit->getValue() ? 1 : 0;51} else if (auto *Bits = dyn_cast<BitsInit>(&I)) {52json::Array array;53for (unsigned i = 0, limit = Bits->getNumBits(); i < limit; i++)54array.push_back(translateInit(*Bits->getBit(i)));55return std::move(array);56} else if (auto *Int = dyn_cast<IntInit>(&I)) {57return Int->getValue();58} else if (auto *Str = dyn_cast<StringInit>(&I)) {59return Str->getValue();60} else if (auto *List = dyn_cast<ListInit>(&I)) {61json::Array array;62for (auto *val : *List)63array.push_back(translateInit(*val));64return std::move(array);65}6667// Init subclasses that we return as JSON objects containing a68// 'kind' discriminator. For these, we also provide the same69// translation back into TableGen input syntax that -print-records70// would give.7172json::Object obj;73obj["printable"] = I.getAsString();7475if (auto *Def = dyn_cast<DefInit>(&I)) {76obj["kind"] = "def";77obj["def"] = Def->getDef()->getName();78return std::move(obj);79} else if (auto *Var = dyn_cast<VarInit>(&I)) {80obj["kind"] = "var";81obj["var"] = Var->getName();82return std::move(obj);83} else if (auto *VarBit = dyn_cast<VarBitInit>(&I)) {84if (auto *Var = dyn_cast<VarInit>(VarBit->getBitVar())) {85obj["kind"] = "varbit";86obj["var"] = Var->getName();87obj["index"] = VarBit->getBitNum();88return std::move(obj);89}90} else if (auto *Dag = dyn_cast<DagInit>(&I)) {91obj["kind"] = "dag";92obj["operator"] = translateInit(*Dag->getOperator());93if (auto name = Dag->getName())94obj["name"] = name->getAsUnquotedString();95json::Array args;96for (unsigned i = 0, limit = Dag->getNumArgs(); i < limit; ++i) {97json::Array arg;98arg.push_back(translateInit(*Dag->getArg(i)));99if (auto argname = Dag->getArgName(i))100arg.push_back(argname->getAsUnquotedString());101else102arg.push_back(nullptr);103args.push_back(std::move(arg));104}105obj["args"] = std::move(args);106return std::move(obj);107}108109// Final fallback: anything that gets past here is simply given a110// kind field of 'complex', and the only other field is the standard111// 'printable' representation.112113assert(!I.isConcrete());114obj["kind"] = "complex";115return std::move(obj);116}117118void JSONEmitter::run(raw_ostream &OS) {119json::Object root;120121root["!tablegen_json_version"] = 1;122123// Prepare the arrays that will list the instances of every class.124// We mostly fill those in by iterating over the superclasses of125// each def, but we also want to ensure we store an empty list for a126// class with no instances at all, so we do a preliminary iteration127// over the classes, invoking std::map::operator[] to default-128// construct the array for each one.129std::map<std::string, json::Array> instance_lists;130for (const auto &C : Records.getClasses()) {131const auto Name = C.second->getNameInitAsString();132(void)instance_lists[Name];133}134135// Main iteration over the defs.136for (const auto &D : Records.getDefs()) {137const auto Name = D.second->getNameInitAsString();138auto &Def = *D.second;139140json::Object obj;141json::Array fields;142143for (const RecordVal &RV : Def.getValues()) {144if (!Def.isTemplateArg(RV.getNameInit())) {145auto Name = RV.getNameInitAsString();146if (RV.isNonconcreteOK())147fields.push_back(Name);148obj[Name] = translateInit(*RV.getValue());149}150}151152obj["!fields"] = std::move(fields);153154json::Array superclasses;155for (const auto &SuperPair : Def.getSuperClasses())156superclasses.push_back(SuperPair.first->getNameInitAsString());157obj["!superclasses"] = std::move(superclasses);158159obj["!name"] = Name;160obj["!anonymous"] = Def.isAnonymous();161162json::Array locs;163for (const SMLoc Loc : Def.getLoc())164locs.push_back(SrcMgr.getFormattedLocationNoOffset(Loc));165obj["!locs"] = std::move(locs);166167root[Name] = std::move(obj);168169// Add this def to the instance list for each of its superclasses.170for (const auto &SuperPair : Def.getSuperClasses()) {171auto SuperName = SuperPair.first->getNameInitAsString();172instance_lists[SuperName].push_back(Name);173}174}175176// Make a JSON object from the std::map of instance lists.177json::Object instanceof;178for (auto kv: instance_lists)179instanceof[kv.first] = std::move(kv.second);180root["!instanceof"] = std::move(instanceof);181182// Done. Write the output.183OS << json::Value(std::move(root)) << "\n";184}185186namespace llvm {187188void EmitJSON(RecordKeeper &RK, raw_ostream &OS) { JSONEmitter(RK).run(OS); }189} // end namespace llvm190191192