Rosica Dejanovska | 8575a84 | 2022-09-01 02:05:30 -0700 | [diff] [blame] | 1 | // 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 Dejanovska | 5b2e813 | 2022-09-20 02:11:51 -0700 | [diff] [blame] | 10 | #include "rs_bindings_from_cc/bazel_types.h" |
Rosica Dejanovska | 8575a84 | 2022-09-01 02:05:30 -0700 | [diff] [blame] | 11 | #include "rs_bindings_from_cc/ir.h" |
| 12 | #include "llvm/Support/JSON.h" |
| 13 | |
| 14 | namespace 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 Dejanovska | abe406f | 2022-09-02 07:06:50 -0700 | [diff] [blame] | 21 | struct NamespaceNode { |
Rosica Dejanovska | 8575a84 | 2022-09-01 02:05:30 -0700 | [diff] [blame] | 22 | llvm::json::Value ToJson() const; |
| 23 | |
| 24 | std::string name; |
Rosica Dejanovska | abe406f | 2022-09-02 07:06:50 -0700 | [diff] [blame] | 25 | std::vector<NamespaceNode> children; |
Rosica Dejanovska | 8575a84 | 2022-09-01 02:05:30 -0700 | [diff] [blame] | 26 | }; |
| 27 | |
Rosica Dejanovska | abe406f | 2022-09-02 07:06:50 -0700 | [diff] [blame] | 28 | inline std::ostream& operator<<(std::ostream& o, const NamespaceNode& ns) { |
Rosica Dejanovska | 8575a84 | 2022-09-01 02:05:30 -0700 | [diff] [blame] | 29 | return o << std::string(llvm::formatv("{0:2}", ns.ToJson())); |
| 30 | } |
| 31 | |
| 32 | // Representation of all C++ namespaces within the current target. |
Rosica Dejanovska | abe406f | 2022-09-02 07:06:50 -0700 | [diff] [blame] | 33 | struct NamespacesHierarchy { |
Rosica Dejanovska | 8575a84 | 2022-09-01 02:05:30 -0700 | [diff] [blame] | 34 | llvm::json::Value ToJson() const; |
| 35 | |
Rosica Dejanovska | 5b2e813 | 2022-09-20 02:11:51 -0700 | [diff] [blame] | 36 | BazelLabel label; |
Rosica Dejanovska | abe406f | 2022-09-02 07:06:50 -0700 | [diff] [blame] | 37 | std::vector<NamespaceNode> namespaces; |
Rosica Dejanovska | 8575a84 | 2022-09-01 02:05:30 -0700 | [diff] [blame] | 38 | }; |
| 39 | |
| 40 | inline std::ostream& operator<<(std::ostream& o, |
Rosica Dejanovska | abe406f | 2022-09-02 07:06:50 -0700 | [diff] [blame] | 41 | const NamespacesHierarchy& all) { |
Rosica Dejanovska | 8575a84 | 2022-09-01 02:05:30 -0700 | [diff] [blame] | 42 | 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 Dejanovska | abe406f | 2022-09-02 07:06:50 -0700 | [diff] [blame] | 46 | NamespacesHierarchy CollectNamespaces(const IR& ir); |
Rosica Dejanovska | 8575a84 | 2022-09-01 02:05:30 -0700 | [diff] [blame] | 47 | |
Rosica Dejanovska | abe406f | 2022-09-02 07:06:50 -0700 | [diff] [blame] | 48 | inline std::string NamespacesAsJson(const NamespacesHierarchy& topLevel) { |
Rosica Dejanovska | 8575a84 | 2022-09-01 02:05:30 -0700 | [diff] [blame] | 49 | return llvm::formatv("{0:2}", topLevel.ToJson()); |
| 50 | } |
| 51 | } // namespace crubit |
| 52 | |
| 53 | #endif // THIRD_PARTY_CRUBIT_RS_BINDINGS_FROM_CC_COLLECT_NAMESPACES_H_ |