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 = dynamic_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() << "_value;";
48     s << child->packet_field_->GetName() << "_value.reset(new ";
49     s << child->struct_def_->name_ << "(*" << field->GetName() << "_value));";
50     if (child->struct_def_->fields_.HasBody()) {
51       s << "auto optional_it = ";
52       s << child->struct_def_->name_ << "::Parse( " << child->packet_field_->GetName() << "_value.get(), ";
53       s << "to_bound, false);";
54       s << "if (optional_it) {";
55       s << "} else { return " << field->GetName() << "_value;}";
56     } else {
57       s << child->struct_def_->name_ << "::Parse( " << child->packet_field_->GetName() << "_value.get(), ";
58       s << "to_bound, false);";
59     }
60     explore_children(*child, s);
61     s << field->GetName() << "_value = std::move(" << child->packet_field_->GetName() << "_value);";
62 
63     s << " }";
64   }
65 }
66 
Generate(std::ostream & s) const67 void StructParserGenerator::Generate(std::ostream& s) const {
68   for (const auto& node : variable_struct_fields_) {
69     if (node.children_.empty()) {
70       continue;
71     }
72     auto field = node.packet_field_;
73     s << "inline std::unique_ptr<" << node.struct_def_->name_ << "> Parse" << node.struct_def_->name_;
74     if (is_little_endian) {
75       s << "(Iterator<kLittleEndian> to_bound) {";
76     } else {
77       s << "(Iterator<!kLittleEndian> to_bound) {";
78     }
79     s << field->GetDataType() << " " << field->GetName() << "_value = ";
80     s << "std::make_unique<" << node.struct_def_->name_ << ">();";
81 
82     s << "auto " << field->GetName() << "_it = to_bound;";
83     s << "auto parent_optional_it = ";
84     s << node.struct_def_->name_ << "::Parse( " << field->GetName() << "_value.get(), ";
85     s << field->GetName() << "_it";
86     if (node.struct_def_->parent_ != nullptr) {
87       s << ", true);";
88     } else {
89       s << ");";
90     }
91     s << "if (parent_optional_it) {";
92     s << field->GetName() << "_it = *parent_optional_it;";
93     s << "} else { return nullptr; }";
94 
95     explore_children(node, s);
96     s << "return " << field->GetName() << "_value; }";
97   }
98 }
99