summaryrefslogtreecommitdiffstats
path: root/contrib/llvm/utils/TableGen/ClangASTNodesEmitter.cpp
blob: 5d6423da08a37a4425f2f9c4bfb4bbe86ad223fd (plain)
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
//=== ClangASTNodesEmitter.cpp - Generate Clang AST node tables -*- C++ -*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// These tablegen backends emit Clang AST node tables
//
//===----------------------------------------------------------------------===//

#include "ClangASTNodesEmitter.h"
#include "Record.h"
#include <map>
#include <cctype>
using namespace llvm;

//===----------------------------------------------------------------------===//
// Statement Node Tables (.inc file) generation.
//===----------------------------------------------------------------------===//

// Create a macro-ized version of a name
static std::string macroName(std::string S) {
  for (unsigned i = 0; i < S.size(); ++i)
    S[i] = std::toupper(S[i]);

  return S;
}

// A map from a node to each of its derived nodes.
typedef std::multimap<Record*, Record*> ChildMap;
typedef ChildMap::const_iterator ChildIterator;

// Returns the first and last non-abstract subrecords
// Called recursively to ensure that nodes remain contiguous
static std::pair<Record *, Record *> EmitStmtNode(const ChildMap &Tree,
                                                  raw_ostream &OS,
                                                  Record *Base,
						  bool Root = true) {
  std::string BaseName = macroName(Base->getName());

  ChildIterator i = Tree.lower_bound(Base), e = Tree.upper_bound(Base);

  Record *First = 0, *Last = 0;
  // This might be the pseudo-node for Stmt; don't assume it has an Abstract
  // bit
  if (Base->getValue("Abstract") && !Base->getValueAsBit("Abstract"))
    First = Last = Base;

  for (; i != e; ++i) {
    Record *R = i->second;
    bool Abstract = R->getValueAsBit("Abstract");
    std::string NodeName = macroName(R->getName());

    OS << "#ifndef " << NodeName << "\n";
    OS << "#  define " << NodeName << "(Type, Base) "
        << BaseName << "(Type, Base)\n";
    OS << "#endif\n";

    if (Abstract)
      OS << "ABSTRACT_STMT(" << NodeName << "(" << R->getName() << ", "
          << Base->getName() << "))\n";
    else
      OS << NodeName << "(" << R->getName() << ", "
          << Base->getName() << ")\n";

    if (Tree.find(R) != Tree.end()) {
      const std::pair<Record *, Record *> &Result
        = EmitStmtNode(Tree, OS, R, false);
      if (!First && Result.first)
        First = Result.first;
      if (Result.second)
        Last = Result.second;
    } else {
      if (!Abstract) {
        Last = R;

        if (!First)
          First = R;
      }
    }

    OS << "#undef " << NodeName << "\n\n";
  }

  if (First) {
    assert (Last && "Got a first node but not a last node for a range!");
    if (Root)
      OS << "LAST_STMT_RANGE(";
    else
      OS << "STMT_RANGE(";
 
    OS << Base->getName() << ", " << First->getName() << ", "
       << Last->getName() << ")\n\n";
  }

  return std::make_pair(First, Last);
}

void ClangStmtNodesEmitter::run(raw_ostream &OS) {
  // Write the preamble
  OS << "#ifndef ABSTRACT_STMT\n";
  OS << "#  define ABSTRACT_STMT(Stmt) Stmt\n";
  OS << "#endif\n";

  OS << "#ifndef STMT_RANGE\n";
  OS << "#  define STMT_RANGE(Base, First, Last)\n";
  OS << "#endif\n\n";

  OS << "#ifndef LAST_STMT_RANGE\n";
  OS << "#  define LAST_STMT_RANGE(Base, First, Last) "
          "STMT_RANGE(Base, First, Last)\n";
  OS << "#endif\n\n";
 
  // Emit statements
  const std::vector<Record*> Stmts = Records.getAllDerivedDefinitions("Stmt");

  ChildMap Tree;

  // Create a pseudo-record to serve as the Stmt node, which isn't actually
  // output.
  Record Stmt ("Stmt", SMLoc());

  for (unsigned i = 0, e = Stmts.size(); i != e; ++i) {
    Record *R = Stmts[i];

    if (R->getValue("Base"))
      Tree.insert(std::make_pair(R->getValueAsDef("Base"), R));
    else
      Tree.insert(std::make_pair(&Stmt, R));
  }

  EmitStmtNode(Tree, OS, &Stmt);

  OS << "#undef STMT\n";
  OS << "#undef STMT_RANGE\n";
  OS << "#undef LAST_STMT_RANGE\n";
  OS << "#undef ABSTRACT_STMT\n";
}
OpenPOWER on IntegriCloud