Files
carbon-lang/toolchain/semantics/node_store.h
T
Jon Ross-Perkins a23f15e901 Refactoring Semantics towards a more instruction-like model (#1349)
This is how I'm interpreting discussion:

- Basic elements are getting set to an ID.
- SetName exists to assign a name (which can then be referred to later with an identifier expression) to an ID.
- Expressions are broken down into a series of operations which operate on IDs.

So with something like the last test:

```
fn Main() { return 12 + 34; }
```

This becomes:

```
Function(%0,
  {IntegerLiteral(%3, 12),
   IntegerLiteral(%2, 34),
   BinaryOperator(%1, +, %3, %2),
   Return(%1),
  })
SetName(`Main`, %0)
```

Note I'm treating blocks as fairly equal to the top of a file now, and basically eliminating boundaries between things. That's because we have discussed also supporting code like:

```
fn Foo() {
  fn Bar() {}
  Bar();
}
```

Here a declaration of a function is occurring inside a code block, so it felt like eliminating the difference was the best choice.

I know you'd commented on the separation of nodes to individual files before; I still think we're going to have a lot of different types of nodes, and so separating them out into individual files makes them easier to browse.
2022-07-06 11:08:47 -07:00

60 lines
2.0 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#ifndef CARBON_TOOLCHAIN_SEMANTICS_NODE_STORE_H_
#define CARBON_TOOLCHAIN_SEMANTICS_NODE_STORE_H_
#include <tuple>
#include "common/check.h"
#include "llvm/ADT/SmallVector.h"
#include "toolchain/semantics/node_ref.h"
#include "toolchain/semantics/nodes/binary_operator.h"
#include "toolchain/semantics/nodes/function.h"
#include "toolchain/semantics/nodes/integer_literal.h"
#include "toolchain/semantics/nodes/return.h"
#include "toolchain/semantics/nodes/set_name.h"
namespace Carbon::Semantics {
// Provides storage for nodes, indexed by Nodes.
//
// This uses templating versus either a macro or repeated functions to provide
// per-type storage.
template <typename... StoredNodeT>
class NodeStoreBase {
public:
// Stores the provided node, returning a pointer to it.
template <typename NodeT>
auto Store(NodeT node) -> NodeRef {
auto& node_store = std::get<static_cast<size_t>(NodeT::Kind)>(node_stores_);
int32_t index = node_store.size();
node_store.push_back(node);
return NodeRef(NodeT::Kind, index);
}
// Returns the requested node. Requires that the pointer is valid for this
// store.
template <typename NodeT>
auto Get(NodeRef node_ref) const -> const NodeT& {
CARBON_CHECK(node_ref.index_ >= 0);
CARBON_CHECK(node_ref.kind_ == NodeT::Kind)
<< "Kind mismatch: " << static_cast<int>(node_ref.kind_) << " vs "
<< static_cast<int>(NodeT::Kind);
auto& node_store = std::get<static_cast<size_t>(NodeT::Kind)>(node_stores_);
CARBON_CHECK(static_cast<size_t>(node_ref.index_) < node_store.size());
return node_store[node_ref.index_];
}
private:
std::tuple<llvm::SmallVector<StoredNodeT, 0>...> node_stores_;
};
using NodeStore =
NodeStoreBase<BinaryOperator, Function, IntegerLiteral, Return, SetName>;
} // namespace Carbon::Semantics
#endif // CARBON_TOOLCHAIN_SEMANTICS_NODE_STORE_H_