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