• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "enum_gen.h"
18 
19 #include <iostream>
20 
21 #include "util.h"
22 
EnumGen(EnumDef e)23 EnumGen::EnumGen(EnumDef e) : e_(std::move(e)) {}
24 
GenDefinition(std::ostream & stream)25 void EnumGen::GenDefinition(std::ostream& stream) {
26   stream << "enum class ";
27   stream << e_.name_;
28   stream << " : " << util::GetTypeForSize(e_.size_);
29   stream << " {";
30   for (const auto& pair : e_.constants_) {
31     stream << pair.second << " = 0x" << std::hex << pair.first << std::dec << ",";
32   }
33   stream << "};\n";
34 }
35 
GenDefinitionPybind11(std::ostream & stream)36 void EnumGen::GenDefinitionPybind11(std::ostream& stream) {
37   stream << "py::enum_<" << e_.name_ << ">(m, \"" << e_.name_ << "\")";
38   for (const auto& pair : e_.constants_) {
39     stream << ".value(\"" << pair.second << "\", " << e_.name_ << "::" << pair.second << ")";
40   }
41   stream << ";\n";
42 }
43 
GenLogging(std::ostream & stream)44 void EnumGen::GenLogging(std::ostream& stream) {
45   // Print out the switch statement that converts all the constants to strings.
46   stream << "inline std::string " << e_.name_ << "Text(const " << e_.name_ << "& param) {";
47   stream << "switch (param) {";
48   for (const auto& pair : e_.constants_) {
49     stream << "case " << e_.name_ << "::" << pair.second << ":";
50     stream << "  return \"" << pair.second << "\";";
51   }
52   stream << "default:";
53   stream << "  return std::string(\"Unknown " << e_.name_ << ": \") + std::to_string(static_cast<int>(param));";
54   stream << "}";
55   stream << "}\n\n";
56 
57   // Print out the stream operator so that the constant can be written to streams.
58   stream << "inline std::ostream& operator<<(std::ostream& os, const " << e_.name_ << "& param) {";
59   stream << "  return os << " << e_.name_ << "Text(param);";
60   stream << "}\n";
61 }
62 
GenRustDef(std::ostream & stream)63 void EnumGen::GenRustDef(std::ostream& stream) {
64   stream << "#[derive(FromPrimitive, ToPrimitive, Debug, Hash, Eq, PartialEq, Clone, Copy)]\n";
65   stream << "#[repr(u64)]\n";
66   stream << "pub enum " << e_.name_ << " {";
67   for (const auto& pair : e_.constants_) {
68     stream << util::ConstantCaseToCamelCase(pair.second) << " = 0x" << std::hex << pair.first << std::dec << ",";
69   }
70   stream << "}";
71 
72   stream << "impl fmt::Display for " << e_.name_ << " {";
73   stream << "fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {";
74   stream << "match self {";
75   for (const auto& pair : e_.constants_) {
76     stream << e_.name_ << "::" << util::ConstantCaseToCamelCase(pair.second) << " => "
77            << "write!(f, \"{:#0" << (util::RoundSizeUp(e_.size_) / 4) + 2 << "X} (" << pair.second << ")\", "
78            << "self.to_" << util::GetRustTypeForSize(e_.size_) << "().unwrap()),";
79   }
80   stream << "}}}\n";
81 
82   if (e_.try_from_enum_ != nullptr) {
83     std::vector<std::string> other_items;
84     for (const auto& pair : e_.try_from_enum_->constants_) {
85       other_items.push_back(pair.second);
86     }
87     stream << "impl TryFrom<" << e_.try_from_enum_->name_ << "> for " << e_.name_ << " {";
88     stream << "type Error = &'static str;";
89     stream << "fn try_from(value: " << e_.try_from_enum_->name_ << ") -> std::result::Result<Self, Self::Error> {";
90     stream << "match value {";
91     for (const auto& pair : e_.constants_) {
92       if (std::find(other_items.begin(), other_items.end(), pair.second) == other_items.end()) {
93         continue;
94       }
95       auto constant_name = util::ConstantCaseToCamelCase(pair.second);
96       stream << e_.try_from_enum_->name_ << "::" << constant_name << " => Ok(" << e_.name_ << "::" << constant_name
97              << "),";
98     }
99     stream << "_ => Err(\"No mapping for provided key\"),";
100     stream << "}}}";
101   }
102 }
103