blob: b3c3354d8123949b7042900d8933e278839182ee [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
Rosica Dejanovska5b2e8132022-09-20 02:11:51 -070010#include "rs_bindings_from_cc/bazel_types.h"
Rosica Dejanovska8575a842022-09-01 02:05:30 -070011#include "rs_bindings_from_cc/ir.h"
12#include "llvm/Support/JSON.h"
13
14namespace crubit {
15
16// Representation of a C++ namespace for JSON serialization.
17// This structure differs from the Namespace struct in ir.h in that it only
18// stores the names of the namespace children, as it is the only information
19// that the cc_import! macro needs in order to be able to merge namespaces
20// across targets.
Rosica Dejanovskaabe406f2022-09-02 07:06:50 -070021struct NamespaceNode {
Rosica Dejanovska8575a842022-09-01 02:05:30 -070022 llvm::json::Value ToJson() const;
23
24 std::string name;
Rosica Dejanovskaabe406f2022-09-02 07:06:50 -070025 std::vector<NamespaceNode> children;
Rosica Dejanovska8575a842022-09-01 02:05:30 -070026};
27
Rosica Dejanovskaabe406f2022-09-02 07:06:50 -070028inline std::ostream& operator<<(std::ostream& o, const NamespaceNode& ns) {
Rosica Dejanovska8575a842022-09-01 02:05:30 -070029 return o << std::string(llvm::formatv("{0:2}", ns.ToJson()));
30}
31
32// Representation of all C++ namespaces within the current target.
Rosica Dejanovskaabe406f2022-09-02 07:06:50 -070033struct NamespacesHierarchy {
Rosica Dejanovska8575a842022-09-01 02:05:30 -070034 llvm::json::Value ToJson() const;
35
Rosica Dejanovska5b2e8132022-09-20 02:11:51 -070036 BazelLabel label;
Rosica Dejanovskaabe406f2022-09-02 07:06:50 -070037 std::vector<NamespaceNode> namespaces;
Rosica Dejanovska8575a842022-09-01 02:05:30 -070038};
39
40inline std::ostream& operator<<(std::ostream& o,
Rosica Dejanovskaabe406f2022-09-02 07:06:50 -070041 const NamespacesHierarchy& all) {
Rosica Dejanovska8575a842022-09-01 02:05:30 -070042 return o << std::string(llvm::formatv("{0:2}", all.ToJson()));
43}
44
45// Returns the current target's namespace hierarchy in JSON serializable format.
Rosica Dejanovskaabe406f2022-09-02 07:06:50 -070046NamespacesHierarchy CollectNamespaces(const IR& ir);
Rosica Dejanovska8575a842022-09-01 02:05:30 -070047
Rosica Dejanovskaabe406f2022-09-02 07:06:50 -070048inline std::string NamespacesAsJson(const NamespacesHierarchy& topLevel) {
Rosica Dejanovska8575a842022-09-01 02:05:30 -070049 return llvm::formatv("{0:2}", topLevel.ToJson());
50}
51} // namespace crubit
52
53#endif // THIRD_PARTY_CRUBIT_RS_BINDINGS_FROM_CC_COLLECT_NAMESPACES_H_