aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/clang16/utils/TableGen/ClangSyntaxEmitter.cpp
blob: a940edbb1d246b72f4e2445b2bd962bdb7e67832 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
//===- ClangSyntaxEmitter.cpp - Generate clang Syntax Tree nodes ----------===//
//
//                     The LLVM Compiler Infrastructure
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// These backends consume the definitions of Syntax Tree nodes.
// See clang/include/clang/Tooling/Syntax/{Syntax,Nodes}.td
//
// The -gen-clang-syntax-node-list backend produces a .inc with macro calls
//   NODE(Kind, BaseKind)
//   ABSTRACT_NODE(Type, Base, FirstKind, LastKind)
// similar to those for AST nodes such as AST/DeclNodes.inc.
//
// The -gen-clang-syntax-node-classes backend produces definitions for the
// syntax::Node subclasses (except those marked as External).
//
// In future, another backend will encode the structure of the various node
// types in tables so their invariants can be checked and enforced.
//
//===----------------------------------------------------------------------===//
#include "TableGenBackends.h"

#include <deque>

#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/TableGen/Record.h"
#include "llvm/TableGen/TableGenBackend.h"

namespace {
using llvm::formatv;

// The class hierarchy of Node types.
// We assemble this in order to be able to define the NodeKind enum in a
// stable and useful way, where abstract Node subclasses correspond to ranges.
class Hierarchy {
public:
  Hierarchy(const llvm::RecordKeeper &Records) {
    for (llvm::Record *T : Records.getAllDerivedDefinitions("NodeType"))
      add(T);
    for (llvm::Record *Derived : Records.getAllDerivedDefinitions("NodeType"))
      if (llvm::Record *Base = Derived->getValueAsOptionalDef("base"))
        link(Derived, Base);
    for (NodeType &N : AllTypes) {
      llvm::sort(N.Derived, [](const NodeType *L, const NodeType *R) {
        return L->Record->getName() < R->Record->getName();
      });
      // Alternatives nodes must have subclasses, External nodes may do.
      assert(N.Record->isSubClassOf("Alternatives") ||
             N.Record->isSubClassOf("External") || N.Derived.empty());
      assert(!N.Record->isSubClassOf("Alternatives") || !N.Derived.empty());
    }
  }

  struct NodeType {
    const llvm::Record *Record = nullptr;
    const NodeType *Base = nullptr;
    std::vector<const NodeType *> Derived;
    llvm::StringRef name() const { return Record->getName(); }
  };

  NodeType &get(llvm::StringRef Name = "Node") {
    auto NI = ByName.find(Name);
    assert(NI != ByName.end() && "no such node");
    return *NI->second;
  }

  // Traverse the hierarchy in pre-order (base classes before derived).
  void visit(llvm::function_ref<void(const NodeType &)> CB,
             const NodeType *Start = nullptr) {
    if (Start == nullptr)
      Start = &get();
    CB(*Start);
    for (const NodeType *D : Start->Derived)
      visit(CB, D);
  }

private:
  void add(const llvm::Record *R) {
    AllTypes.emplace_back();
    AllTypes.back().Record = R;
    bool Inserted = ByName.try_emplace(R->getName(), &AllTypes.back()).second;
    assert(Inserted && "Duplicate node name");
    (void)Inserted;
  }

  void link(const llvm::Record *Derived, const llvm::Record *Base) {
    auto &CN = get(Derived->getName()), &PN = get(Base->getName());
    assert(CN.Base == nullptr && "setting base twice");
    PN.Derived.push_back(&CN);
    CN.Base = &PN;
  }

  std::deque<NodeType> AllTypes;
  llvm::DenseMap<llvm::StringRef, NodeType *> ByName;
};

const Hierarchy::NodeType &firstConcrete(const Hierarchy::NodeType &N) {
  return N.Derived.empty() ? N : firstConcrete(*N.Derived.front());
}
const Hierarchy::NodeType &lastConcrete(const Hierarchy::NodeType &N) {
  return N.Derived.empty() ? N : lastConcrete(*N.Derived.back());
}

struct SyntaxConstraint {
  SyntaxConstraint(const llvm::Record &R) {
    if (R.isSubClassOf("Optional")) {
      *this = SyntaxConstraint(*R.getValueAsDef("inner"));
    } else if (R.isSubClassOf("AnyToken")) {
      NodeType = "Leaf";
    } else if (R.isSubClassOf("NodeType")) {
      NodeType = R.getName().str();
    } else {
      assert(false && "Unhandled Syntax kind");
    }
  }

  std::string NodeType;
  // optional and leaf types also go here, once we want to use them.
};

} // namespace

void clang::EmitClangSyntaxNodeList(llvm::RecordKeeper &Records,
                                    llvm::raw_ostream &OS) {
  llvm::emitSourceFileHeader("Syntax tree node list", OS);
  Hierarchy H(Records);
  OS << R"cpp(
#ifndef NODE
#define NODE(Kind, Base)
#endif

#ifndef CONCRETE_NODE
#define CONCRETE_NODE(Kind, Base) NODE(Kind, Base)
#endif

#ifndef ABSTRACT_NODE
#define ABSTRACT_NODE(Kind, Base, First, Last) NODE(Kind, Base)
#endif

)cpp";
  H.visit([&](const Hierarchy::NodeType &N) {
    // Don't emit ABSTRACT_NODE for node itself, which has no parent.
    if (N.Base == nullptr)
      return;
    if (N.Derived.empty())
      OS << formatv("CONCRETE_NODE({0},{1})\n", N.name(), N.Base->name());
    else
      OS << formatv("ABSTRACT_NODE({0},{1},{2},{3})\n", N.name(),
                    N.Base->name(), firstConcrete(N).name(),
                    lastConcrete(N).name());
  });
  OS << R"cpp(
#undef NODE
#undef CONCRETE_NODE
#undef ABSTRACT_NODE
)cpp";
}

// Format a documentation string as a C++ comment.
// Trims leading whitespace handling since comments come from a TableGen file:
//    documentation = [{
//      This is a widget. Example:
//        widget.explode()
//    }];
// and should be formatted as:
//    /// This is a widget. Example:
//    ///   widget.explode()
// Leading and trailing whitespace lines are stripped.
// The indentation of the first line is stripped from all lines.
static void printDoc(llvm::StringRef Doc, llvm::raw_ostream &OS) {
  Doc = Doc.rtrim();
  llvm::StringRef Line;
  while (Line.trim().empty() && !Doc.empty())
    std::tie(Line, Doc) = Doc.split('\n');
  llvm::StringRef Indent = Line.take_while(llvm::isSpace);
  for (; !Line.empty() || !Doc.empty(); std::tie(Line, Doc) = Doc.split('\n')) {
    Line.consume_front(Indent);
    OS << "/// " << Line << "\n";
  }
}

void clang::EmitClangSyntaxNodeClasses(llvm::RecordKeeper &Records,
                                       llvm::raw_ostream &OS) {
  llvm::emitSourceFileHeader("Syntax tree node list", OS);
  Hierarchy H(Records);

  OS << "\n// Forward-declare node types so we don't have to carefully "
        "sequence definitions.\n";
  H.visit([&](const Hierarchy::NodeType &N) {
    OS << "class " << N.name() << ";\n";
  });

  OS << "\n// Node definitions\n\n";
  H.visit([&](const Hierarchy::NodeType &N) {
    if (N.Record->isSubClassOf("External"))
      return;
    printDoc(N.Record->getValueAsString("documentation"), OS);
    OS << formatv("class {0}{1} : public {2} {{\n", N.name(),
                  N.Derived.empty() ? " final" : "", N.Base->name());

    // Constructor.
    if (N.Derived.empty())
      OS << formatv("public:\n  {0}() : {1}(NodeKind::{0}) {{}\n", N.name(),
                    N.Base->name());
    else
      OS << formatv("protected:\n  {0}(NodeKind K) : {1}(K) {{}\npublic:\n",
                    N.name(), N.Base->name());

    if (N.Record->isSubClassOf("Sequence")) {
      // Getters for sequence elements.
      for (const auto &C : N.Record->getValueAsListOfDefs("children")) {
        assert(C->isSubClassOf("Role"));
        llvm::StringRef Role = C->getValueAsString("role");
        SyntaxConstraint Constraint(*C->getValueAsDef("syntax"));
        for (const char *Const : {"", "const "})
          OS << formatv(
              "  {2}{1} *get{0}() {2} {{\n"
              "    return llvm::cast_or_null<{1}>(findChild(NodeRole::{0}));\n"
              "  }\n",
              Role, Constraint.NodeType, Const);
      }
    }

    // classof. FIXME: move definition inline once ~all nodes are generated.
    OS << "  static bool classof(const Node *N);\n";

    OS << "};\n\n";
  });
}