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 << "pub enum " << e_.name_ << " {";
66   for (const auto& pair : e_.constants_) {
67     stream << util::ConstantCaseToCamelCase(pair.second) << " = 0x" << std::hex << pair.first << std::dec << ",";
68   }
69   stream << "}";
70 
71   if (e_.try_from_enum_ != nullptr) {
72     std::vector<std::string> other_items;
73     for (const auto& pair : e_.try_from_enum_->constants_) {
74       other_items.push_back(pair.second);
75     }
76     stream << "impl TryFrom<" << e_.try_from_enum_->name_ << "> for " << e_.name_ << " {";
77     stream << "type Error = &'static str;";
78     stream << "fn try_from(value: " << e_.try_from_enum_->name_ << ") -> std::result::Result<Self, Self::Error> {";
79     stream << "match value {";
80     for (const auto& pair : e_.constants_) {
81       if (std::find(other_items.begin(), other_items.end(), pair.second) == other_items.end()) {
82         continue;
83       }
84       auto constant_name = util::ConstantCaseToCamelCase(pair.second);
85       stream << e_.try_from_enum_->name_ << "::" << constant_name << " => Ok(" << e_.name_ << "::" << constant_name
86              << "),";
87     }
88     stream << "_ => Err(\"No mapping for provided key\"),";
89     stream << "}}}";
90   }
91 }
92