xref: /freebsd/contrib/llvm-project/clang/utils/TableGen/ClangASTNodesEmitter.cpp (revision a7dea1671b87c07d2d266f836bfa8b58efc7c134)
10b57cec5SDimitry Andric //=== ClangASTNodesEmitter.cpp - Generate Clang AST node tables -*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // These tablegen backends emit Clang AST node tables
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
13*a7dea167SDimitry Andric #include "TableGenBackends.h"
14*a7dea167SDimitry Andric 
150b57cec5SDimitry Andric #include "llvm/TableGen/Record.h"
160b57cec5SDimitry Andric #include "llvm/TableGen/TableGenBackend.h"
170b57cec5SDimitry Andric #include <cctype>
180b57cec5SDimitry Andric #include <map>
190b57cec5SDimitry Andric #include <set>
200b57cec5SDimitry Andric #include <string>
210b57cec5SDimitry Andric using namespace llvm;
220b57cec5SDimitry Andric 
230b57cec5SDimitry Andric /// ClangASTNodesEmitter - The top-level class emits .inc files containing
240b57cec5SDimitry Andric ///  declarations of Clang statements.
250b57cec5SDimitry Andric ///
260b57cec5SDimitry Andric namespace {
270b57cec5SDimitry Andric class ClangASTNodesEmitter {
280b57cec5SDimitry Andric   // A map from a node to each of its derived nodes.
290b57cec5SDimitry Andric   typedef std::multimap<Record*, Record*> ChildMap;
300b57cec5SDimitry Andric   typedef ChildMap::const_iterator ChildIterator;
310b57cec5SDimitry Andric 
320b57cec5SDimitry Andric   RecordKeeper &Records;
330b57cec5SDimitry Andric   Record Root;
340b57cec5SDimitry Andric   const std::string &BaseSuffix;
350b57cec5SDimitry Andric 
360b57cec5SDimitry Andric   // Create a macro-ized version of a name
370b57cec5SDimitry Andric   static std::string macroName(std::string S) {
380b57cec5SDimitry Andric     for (unsigned i = 0; i < S.size(); ++i)
390b57cec5SDimitry Andric       S[i] = std::toupper(S[i]);
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric     return S;
420b57cec5SDimitry Andric   }
430b57cec5SDimitry Andric 
440b57cec5SDimitry Andric   // Return the name to be printed in the base field. Normally this is
450b57cec5SDimitry Andric   // the record's name plus the base suffix, but if it is the root node and
460b57cec5SDimitry Andric   // the suffix is non-empty, it's just the suffix.
470b57cec5SDimitry Andric   std::string baseName(Record &R) {
480b57cec5SDimitry Andric     if (&R == &Root && !BaseSuffix.empty())
490b57cec5SDimitry Andric       return BaseSuffix;
500b57cec5SDimitry Andric 
510b57cec5SDimitry Andric     return R.getName().str() + BaseSuffix;
520b57cec5SDimitry Andric   }
530b57cec5SDimitry Andric 
540b57cec5SDimitry Andric   std::pair<Record *, Record *> EmitNode (const ChildMap &Tree, raw_ostream& OS,
550b57cec5SDimitry Andric                                           Record *Base);
560b57cec5SDimitry Andric public:
570b57cec5SDimitry Andric   explicit ClangASTNodesEmitter(RecordKeeper &R, const std::string &N,
580b57cec5SDimitry Andric                                 const std::string &S)
590b57cec5SDimitry Andric     : Records(R), Root(N, SMLoc(), R), BaseSuffix(S)
600b57cec5SDimitry Andric     {}
610b57cec5SDimitry Andric 
620b57cec5SDimitry Andric   // run - Output the .inc file contents
630b57cec5SDimitry Andric   void run(raw_ostream &OS);
640b57cec5SDimitry Andric };
650b57cec5SDimitry Andric } // end anonymous namespace
660b57cec5SDimitry Andric 
670b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
680b57cec5SDimitry Andric // Statement Node Tables (.inc file) generation.
690b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
700b57cec5SDimitry Andric 
710b57cec5SDimitry Andric // Returns the first and last non-abstract subrecords
720b57cec5SDimitry Andric // Called recursively to ensure that nodes remain contiguous
730b57cec5SDimitry Andric std::pair<Record *, Record *> ClangASTNodesEmitter::EmitNode(
740b57cec5SDimitry Andric                                                            const ChildMap &Tree,
750b57cec5SDimitry Andric                                                            raw_ostream &OS,
760b57cec5SDimitry Andric                                                            Record *Base) {
770b57cec5SDimitry Andric   std::string BaseName = macroName(Base->getName());
780b57cec5SDimitry Andric 
790b57cec5SDimitry Andric   ChildIterator i = Tree.lower_bound(Base), e = Tree.upper_bound(Base);
800b57cec5SDimitry Andric 
810b57cec5SDimitry Andric   Record *First = nullptr, *Last = nullptr;
820b57cec5SDimitry Andric   // This might be the pseudo-node for Stmt; don't assume it has an Abstract
830b57cec5SDimitry Andric   // bit
840b57cec5SDimitry Andric   if (Base->getValue("Abstract") && !Base->getValueAsBit("Abstract"))
850b57cec5SDimitry Andric     First = Last = Base;
860b57cec5SDimitry Andric 
870b57cec5SDimitry Andric   for (; i != e; ++i) {
880b57cec5SDimitry Andric     Record *R = i->second;
890b57cec5SDimitry Andric     bool Abstract = R->getValueAsBit("Abstract");
900b57cec5SDimitry Andric     std::string NodeName = macroName(R->getName());
910b57cec5SDimitry Andric 
920b57cec5SDimitry Andric     OS << "#ifndef " << NodeName << "\n";
930b57cec5SDimitry Andric     OS << "#  define " << NodeName << "(Type, Base) "
940b57cec5SDimitry Andric         << BaseName << "(Type, Base)\n";
950b57cec5SDimitry Andric     OS << "#endif\n";
960b57cec5SDimitry Andric 
970b57cec5SDimitry Andric     if (Abstract)
980b57cec5SDimitry Andric       OS << "ABSTRACT_" << macroName(Root.getName()) << "(" << NodeName << "("
990b57cec5SDimitry Andric           << R->getName() << ", " << baseName(*Base) << "))\n";
1000b57cec5SDimitry Andric     else
1010b57cec5SDimitry Andric       OS << NodeName << "(" << R->getName() << ", "
1020b57cec5SDimitry Andric           << baseName(*Base) << ")\n";
1030b57cec5SDimitry Andric 
1040b57cec5SDimitry Andric     if (Tree.find(R) != Tree.end()) {
1050b57cec5SDimitry Andric       const std::pair<Record *, Record *> &Result
1060b57cec5SDimitry Andric         = EmitNode(Tree, OS, R);
1070b57cec5SDimitry Andric       if (!First && Result.first)
1080b57cec5SDimitry Andric         First = Result.first;
1090b57cec5SDimitry Andric       if (Result.second)
1100b57cec5SDimitry Andric         Last = Result.second;
1110b57cec5SDimitry Andric     } else {
1120b57cec5SDimitry Andric       if (!Abstract) {
1130b57cec5SDimitry Andric         Last = R;
1140b57cec5SDimitry Andric 
1150b57cec5SDimitry Andric         if (!First)
1160b57cec5SDimitry Andric           First = R;
1170b57cec5SDimitry Andric       }
1180b57cec5SDimitry Andric     }
1190b57cec5SDimitry Andric 
1200b57cec5SDimitry Andric     OS << "#undef " << NodeName << "\n\n";
1210b57cec5SDimitry Andric   }
1220b57cec5SDimitry Andric 
1230b57cec5SDimitry Andric   if (First) {
1240b57cec5SDimitry Andric     assert (Last && "Got a first node but not a last node for a range!");
1250b57cec5SDimitry Andric     if (Base == &Root)
1260b57cec5SDimitry Andric       OS << "LAST_" << macroName(Root.getName()) << "_RANGE(";
1270b57cec5SDimitry Andric     else
1280b57cec5SDimitry Andric       OS << macroName(Root.getName()) << "_RANGE(";
1290b57cec5SDimitry Andric     OS << Base->getName() << ", " << First->getName() << ", "
1300b57cec5SDimitry Andric        << Last->getName() << ")\n\n";
1310b57cec5SDimitry Andric   }
1320b57cec5SDimitry Andric 
1330b57cec5SDimitry Andric   return std::make_pair(First, Last);
1340b57cec5SDimitry Andric }
1350b57cec5SDimitry Andric 
1360b57cec5SDimitry Andric void ClangASTNodesEmitter::run(raw_ostream &OS) {
1370b57cec5SDimitry Andric   emitSourceFileHeader("List of AST nodes of a particular kind", OS);
1380b57cec5SDimitry Andric 
1390b57cec5SDimitry Andric   // Write the preamble
1400b57cec5SDimitry Andric   OS << "#ifndef ABSTRACT_" << macroName(Root.getName()) << "\n";
1410b57cec5SDimitry Andric   OS << "#  define ABSTRACT_" << macroName(Root.getName()) << "(Type) Type\n";
1420b57cec5SDimitry Andric   OS << "#endif\n";
1430b57cec5SDimitry Andric 
1440b57cec5SDimitry Andric   OS << "#ifndef " << macroName(Root.getName()) << "_RANGE\n";
1450b57cec5SDimitry Andric   OS << "#  define "
1460b57cec5SDimitry Andric      << macroName(Root.getName()) << "_RANGE(Base, First, Last)\n";
1470b57cec5SDimitry Andric   OS << "#endif\n\n";
1480b57cec5SDimitry Andric 
1490b57cec5SDimitry Andric   OS << "#ifndef LAST_" << macroName(Root.getName()) << "_RANGE\n";
1500b57cec5SDimitry Andric   OS << "#  define LAST_"
1510b57cec5SDimitry Andric      << macroName(Root.getName()) << "_RANGE(Base, First, Last) "
1520b57cec5SDimitry Andric      << macroName(Root.getName()) << "_RANGE(Base, First, Last)\n";
1530b57cec5SDimitry Andric   OS << "#endif\n\n";
1540b57cec5SDimitry Andric 
1550b57cec5SDimitry Andric   // Emit statements
1560b57cec5SDimitry Andric   const std::vector<Record*> Stmts
1570b57cec5SDimitry Andric     = Records.getAllDerivedDefinitions(Root.getName());
1580b57cec5SDimitry Andric 
1590b57cec5SDimitry Andric   ChildMap Tree;
1600b57cec5SDimitry Andric 
1610b57cec5SDimitry Andric   for (unsigned i = 0, e = Stmts.size(); i != e; ++i) {
1620b57cec5SDimitry Andric     Record *R = Stmts[i];
1630b57cec5SDimitry Andric 
1640b57cec5SDimitry Andric     if (R->getValue("Base"))
1650b57cec5SDimitry Andric       Tree.insert(std::make_pair(R->getValueAsDef("Base"), R));
1660b57cec5SDimitry Andric     else
1670b57cec5SDimitry Andric       Tree.insert(std::make_pair(&Root, R));
1680b57cec5SDimitry Andric   }
1690b57cec5SDimitry Andric 
1700b57cec5SDimitry Andric   EmitNode(Tree, OS, &Root);
1710b57cec5SDimitry Andric 
1720b57cec5SDimitry Andric   OS << "#undef " << macroName(Root.getName()) << "\n";
1730b57cec5SDimitry Andric   OS << "#undef " << macroName(Root.getName()) << "_RANGE\n";
1740b57cec5SDimitry Andric   OS << "#undef LAST_" << macroName(Root.getName()) << "_RANGE\n";
1750b57cec5SDimitry Andric   OS << "#undef ABSTRACT_" << macroName(Root.getName()) << "\n";
1760b57cec5SDimitry Andric }
1770b57cec5SDimitry Andric 
178*a7dea167SDimitry Andric void clang::EmitClangASTNodes(RecordKeeper &RK, raw_ostream &OS,
1790b57cec5SDimitry Andric                               const std::string &N, const std::string &S) {
1800b57cec5SDimitry Andric   ClangASTNodesEmitter(RK, N, S).run(OS);
1810b57cec5SDimitry Andric }
1820b57cec5SDimitry Andric 
1830b57cec5SDimitry Andric // Emits and addendum to a .inc file to enumerate the clang declaration
1840b57cec5SDimitry Andric // contexts.
185*a7dea167SDimitry Andric void clang::EmitClangDeclContext(RecordKeeper &Records, raw_ostream &OS) {
1860b57cec5SDimitry Andric   // FIXME: Find a .td file format to allow for this to be represented better.
1870b57cec5SDimitry Andric 
1880b57cec5SDimitry Andric   emitSourceFileHeader("List of AST Decl nodes", OS);
1890b57cec5SDimitry Andric 
1900b57cec5SDimitry Andric   OS << "#ifndef DECL_CONTEXT\n";
1910b57cec5SDimitry Andric   OS << "#  define DECL_CONTEXT(DECL)\n";
1920b57cec5SDimitry Andric   OS << "#endif\n";
1930b57cec5SDimitry Andric 
1940b57cec5SDimitry Andric   OS << "#ifndef DECL_CONTEXT_BASE\n";
1950b57cec5SDimitry Andric   OS << "#  define DECL_CONTEXT_BASE(DECL) DECL_CONTEXT(DECL)\n";
1960b57cec5SDimitry Andric   OS << "#endif\n";
1970b57cec5SDimitry Andric 
1980b57cec5SDimitry Andric   typedef std::set<Record*> RecordSet;
1990b57cec5SDimitry Andric   typedef std::vector<Record*> RecordVector;
2000b57cec5SDimitry Andric 
2010b57cec5SDimitry Andric   RecordVector DeclContextsVector
2020b57cec5SDimitry Andric     = Records.getAllDerivedDefinitions("DeclContext");
2030b57cec5SDimitry Andric   RecordVector Decls = Records.getAllDerivedDefinitions("Decl");
2040b57cec5SDimitry Andric   RecordSet DeclContexts (DeclContextsVector.begin(), DeclContextsVector.end());
2050b57cec5SDimitry Andric 
2060b57cec5SDimitry Andric   for (RecordVector::iterator i = Decls.begin(), e = Decls.end(); i != e; ++i) {
2070b57cec5SDimitry Andric     Record *R = *i;
2080b57cec5SDimitry Andric 
2090b57cec5SDimitry Andric     if (R->getValue("Base")) {
2100b57cec5SDimitry Andric       Record *B = R->getValueAsDef("Base");
2110b57cec5SDimitry Andric       if (DeclContexts.find(B) != DeclContexts.end()) {
2120b57cec5SDimitry Andric         OS << "DECL_CONTEXT_BASE(" << B->getName() << ")\n";
2130b57cec5SDimitry Andric         DeclContexts.erase(B);
2140b57cec5SDimitry Andric       }
2150b57cec5SDimitry Andric     }
2160b57cec5SDimitry Andric   }
2170b57cec5SDimitry Andric 
2180b57cec5SDimitry Andric   // To keep identical order, RecordVector may be used
2190b57cec5SDimitry Andric   // instead of RecordSet.
2200b57cec5SDimitry Andric   for (RecordVector::iterator
2210b57cec5SDimitry Andric          i = DeclContextsVector.begin(), e = DeclContextsVector.end();
2220b57cec5SDimitry Andric        i != e; ++i)
2230b57cec5SDimitry Andric     if (DeclContexts.find(*i) != DeclContexts.end())
2240b57cec5SDimitry Andric       OS << "DECL_CONTEXT(" << (*i)->getName() << ")\n";
2250b57cec5SDimitry Andric 
2260b57cec5SDimitry Andric   OS << "#undef DECL_CONTEXT\n";
2270b57cec5SDimitry Andric   OS << "#undef DECL_CONTEXT_BASE\n";
2280b57cec5SDimitry Andric }
229