blob: d244ed531866a86e133f4dd6e204158fa8fab7b7 [file] [log] [blame]
Rosica Dejanovska8575a842022-09-01 02:05:30 -07001// Part of the Crubit project, under the Apache License v2.0 with LLVM
2// Exceptions. See /LICENSE for license information.
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef THIRD_PARTY_CRUBIT_RS_BINDINGS_FROM_CC_COLLECT_NAMESPACES_H_
6#define THIRD_PARTY_CRUBIT_RS_BINDINGS_FROM_CC_COLLECT_NAMESPACES_H_
7
8#include <string>
9
10#include "rs_bindings_from_cc/ir.h"
11#include "llvm/Support/JSON.h"
12
13namespace crubit {
14
15// Representation of a C++ namespace for JSON serialization.
16// This structure differs from the Namespace struct in ir.h in that it only
17// stores the names of the namespace children, as it is the only information
18// that the cc_import! macro needs in order to be able to merge namespaces
19// across targets.
Rosica Dejanovskaabe406f2022-09-02 07:06:50 -070020struct NamespaceNode {
Rosica Dejanovska8575a842022-09-01 02:05:30 -070021 llvm::json::Value ToJson() const;
22
23 std::string name;
Rosica Dejanovskaabe406f2022-09-02 07:06:50 -070024 std::vector<NamespaceNode> children;
Rosica Dejanovska8575a842022-09-01 02:05:30 -070025};
26
Rosica Dejanovskaabe406f2022-09-02 07:06:50 -070027inline std::ostream& operator<<(std::ostream& o, const NamespaceNode& ns) {
Rosica Dejanovska8575a842022-09-01 02:05:30 -070028 return o << std::string(llvm::formatv("{0:2}", ns.ToJson()));
29}
30
31// Representation of all C++ namespaces within the current target.
Rosica Dejanovskaabe406f2022-09-02 07:06:50 -070032struct NamespacesHierarchy {
Rosica Dejanovska8575a842022-09-01 02:05:30 -070033 llvm::json::Value ToJson() const;
34
Rosica Dejanovskaabe406f2022-09-02 07:06:50 -070035 std::vector<NamespaceNode> namespaces;
Rosica Dejanovska8575a842022-09-01 02:05:30 -070036};
37
38inline std::ostream& operator<<(std::ostream& o,
Rosica Dejanovskaabe406f2022-09-02 07:06:50 -070039 const NamespacesHierarchy& all) {
Rosica Dejanovska8575a842022-09-01 02:05:30 -070040 return o << std::string(llvm::formatv("{0:2}", all.ToJson()));
41}
42
43// Returns the current target's namespace hierarchy in JSON serializable format.
Rosica Dejanovskaabe406f2022-09-02 07:06:50 -070044NamespacesHierarchy CollectNamespaces(const IR& ir);
Rosica Dejanovska8575a842022-09-01 02:05:30 -070045
Rosica Dejanovskaabe406f2022-09-02 07:06:50 -070046inline std::string NamespacesAsJson(const NamespacesHierarchy& topLevel) {
Rosica Dejanovska8575a842022-09-01 02:05:30 -070047 return llvm::formatv("{0:2}", topLevel.ToJson());
48}
49} // namespace crubit
50
51#endif // THIRD_PARTY_CRUBIT_RS_BINDINGS_FROM_CC_COLLECT_NAMESPACES_H_