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 "struct_parser_generator.h"
18
StructParserGenerator(const Declarations & decls)19 StructParserGenerator::StructParserGenerator(const Declarations& decls) {
20 is_little_endian = decls.is_little_endian;
21 for (const auto& s : decls.type_defs_queue_) {
22 if (s.second->GetDefinitionType() == TypeDef::Type::STRUCT) {
23 const auto* struct_def = static_cast<const StructDef*>(s.second);
24 variable_struct_fields_.emplace_back(struct_def);
25 }
26 }
27 for (const auto& node : variable_struct_fields_) {
28 if (node.struct_def_->parent_ != nullptr) {
29 for (auto& parent : variable_struct_fields_) {
30 if (node.struct_def_->parent_->name_ == parent.struct_def_->name_) {
31 parent.children_.push_back(&node);
32 }
33 }
34 }
35 }
36 }
37
explore_children(const TreeNode & node,std::ostream & s) const38 void StructParserGenerator::explore_children(const TreeNode& node, std::ostream& s) const {
39 auto field = node.packet_field_;
40 if (!node.children_.empty()) {
41 s << "bool " << field->GetName() << "_child_found = false; /* Greedy match */";
42 }
43 for (const auto& child : node.children_) {
44 s << "if (!" << field->GetName() << "_child_found && ";
45 s << child->struct_def_->name_ << "::IsInstance(*" << field->GetName() << "_value.get())) {";
46 s << field->GetName() << "_child_found = true;";
47 s << "std::unique_ptr<" << child->struct_def_->name_ << "> " << child->packet_field_->GetName()
48 << "_value;";
49 s << child->packet_field_->GetName() << "_value.reset(new ";
50 s << child->struct_def_->name_ << "(*" << field->GetName() << "_value));";
51 if (child->struct_def_->fields_.HasBody()) {
52 s << "auto optional_it = ";
53 s << child->struct_def_->name_ << "::Parse( " << child->packet_field_->GetName()
54 << "_value.get(), ";
55 s << "to_bound, false);";
56 s << "if (optional_it) {";
57 s << "} else { return " << field->GetName() << "_value;}";
58 } else {
59 s << child->struct_def_->name_ << "::Parse( " << child->packet_field_->GetName()
60 << "_value.get(), ";
61 s << "to_bound, false);";
62 }
63 explore_children(*child, s);
64 s << field->GetName() << "_value = std::move(" << child->packet_field_->GetName() << "_value);";
65
66 s << " }";
67 }
68 }
69
Generate(std::ostream & s) const70 void StructParserGenerator::Generate(std::ostream& s) const {
71 for (const auto& node : variable_struct_fields_) {
72 if (node.children_.empty()) {
73 continue;
74 }
75 auto field = node.packet_field_;
76 s << "inline std::unique_ptr<" << node.struct_def_->name_ << "> Parse"
77 << node.struct_def_->name_;
78 if (is_little_endian) {
79 s << "(Iterator<kLittleEndian> to_bound) {";
80 } else {
81 s << "(Iterator<!kLittleEndian> to_bound) {";
82 }
83 s << field->GetDataType() << " " << field->GetName() << "_value = ";
84 s << "std::make_unique<" << node.struct_def_->name_ << ">();";
85
86 s << "auto " << field->GetName() << "_it = to_bound;";
87 s << "auto parent_optional_it = ";
88 s << node.struct_def_->name_ << "::Parse( " << field->GetName() << "_value.get(), ";
89 s << field->GetName() << "_it";
90 if (node.struct_def_->parent_ != nullptr) {
91 s << ", true);";
92 } else {
93 s << ");";
94 }
95 s << "if (parent_optional_it) {";
96 s << field->GetName() << "_it = *parent_optional_it;";
97 s << "} else { return nullptr; }";
98
99 explore_children(node, s);
100 s << "return " << field->GetName() << "_value; }";
101 }
102 }
103