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 "fields/variable_length_struct_field.h"
18 #include "util.h"
19
20 const std::string VariableLengthStructField::kFieldType = "VariableLengthStructField";
21
VariableLengthStructField(std::string name,std::string type_name,ParseLocation loc)22 VariableLengthStructField::VariableLengthStructField(std::string name, std::string type_name, ParseLocation loc)
23 : PacketField(name, loc), type_name_(type_name) {}
24
GetFieldType() const25 const std::string& VariableLengthStructField::GetFieldType() const {
26 return VariableLengthStructField::kFieldType;
27 }
28
GetSize() const29 Size VariableLengthStructField::GetSize() const {
30 return Size();
31 }
32
GetBuilderSize() const33 Size VariableLengthStructField::GetBuilderSize() const {
34 std::string ret = "(" + GetName() + "_->size() * 8) ";
35 return ret;
36 }
37
GetDataType() const38 std::string VariableLengthStructField::GetDataType() const {
39 std::string ret = "std::unique_ptr<" + type_name_ + ">";
40 return ret;
41 }
42
GenExtractor(std::ostream & s,int,bool) const43 void VariableLengthStructField::GenExtractor(std::ostream& s, int, bool) const {
44 s << GetName() << "_ptr = Parse" << type_name_ << "(" << GetName() << "_it);";
45 s << "if (" << GetName() << "_ptr != nullptr) {";
46 s << GetName() << "_it = " << GetName() << "_it + " << GetName() << "_ptr->size();";
47 s << "} else {";
48 s << GetName() << "_it = " << GetName() << "_it + " << GetName() << "_it.NumBytesRemaining();";
49 s << "}";
50 }
51
GetGetterFunctionName() const52 std::string VariableLengthStructField::GetGetterFunctionName() const {
53 std::stringstream ss;
54 ss << "Get" << util::UnderscoreToCamelCase(GetName());
55 return ss.str();
56 }
57
GenGetter(std::ostream & s,Size start_offset,Size end_offset) const58 void VariableLengthStructField::GenGetter(std::ostream& s, Size start_offset, Size end_offset) const {
59 s << GetDataType() << " " << GetGetterFunctionName() << "() const {";
60 s << "ASSERT(was_validated_);";
61 s << "size_t end_index = size();";
62 s << "auto to_bound = begin();";
63 int num_leading_bits = GenBounds(s, start_offset, end_offset, GetSize());
64 s << GetDataType() << " " << GetName() << "_ptr{};";
65 GenExtractor(s, num_leading_bits, false);
66 s << "return " << GetName() << "_ptr;";
67 s << "}\n";
68 }
69
GetBuilderParameterType() const70 std::string VariableLengthStructField::GetBuilderParameterType() const {
71 return GetDataType();
72 }
73
BuilderParameterMustBeMoved() const74 bool VariableLengthStructField::BuilderParameterMustBeMoved() const {
75 return true;
76 }
77
HasParameterValidator() const78 bool VariableLengthStructField::HasParameterValidator() const {
79 return false;
80 }
81
GenParameterValidator(std::ostream &) const82 void VariableLengthStructField::GenParameterValidator(std::ostream&) const {
83 // Validated at compile time.
84 }
85
GenInserter(std::ostream & s) const86 void VariableLengthStructField::GenInserter(std::ostream& s) const {
87 s << GetName() << "_->Serialize(i);";
88 }
89
GenValidator(std::ostream &) const90 void VariableLengthStructField::GenValidator(std::ostream&) const {
91 // Do nothing
92 }
93
GetRustDataType() const94 std::string VariableLengthStructField::GetRustDataType() const {
95 std::string ret = "std::boxed::Box<" + type_name_ + ">";
96 return ret;
97 }
98
GenRustGetter(std::ostream &,Size,Size) const99 void VariableLengthStructField::GenRustGetter(std::ostream&, Size, Size) const {
100 }
101
GenRustWriter(std::ostream &,Size,Size) const102 void VariableLengthStructField::GenRustWriter(std::ostream&, Size, Size) const {}
103