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 
19 #include "util.h"
20 
21 const std::string VariableLengthStructField::kFieldType = "VariableLengthStructField";
22 
VariableLengthStructField(std::string name,std::string type_name,ParseLocation loc)23 VariableLengthStructField::VariableLengthStructField(std::string name, std::string type_name,
24                                                      ParseLocation loc)
25     : PacketField(name, loc), type_name_(type_name) {}
26 
GetFieldType() const27 const std::string& VariableLengthStructField::GetFieldType() const {
28   return VariableLengthStructField::kFieldType;
29 }
30 
GetSize() const31 Size VariableLengthStructField::GetSize() const { return Size(); }
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,
59                                           Size end_offset) const {
60   s << GetDataType() << " " << GetGetterFunctionName() << "() const {";
61   s << "ASSERT(was_validated_);";
62   s << "size_t end_index = size();";
63   s << "auto to_bound = begin();";
64   int num_leading_bits = GenBounds(s, start_offset, end_offset, GetSize());
65   s << GetDataType() << " " << GetName() << "_ptr{};";
66   GenExtractor(s, num_leading_bits, false);
67   s << "return " << GetName() << "_ptr;";
68   s << "}\n";
69 }
70 
GetBuilderParameterType() const71 std::string VariableLengthStructField::GetBuilderParameterType() const { return GetDataType(); }
72 
BuilderParameterMustBeMoved() const73 bool VariableLengthStructField::BuilderParameterMustBeMoved() const { return true; }
74 
HasParameterValidator() const75 bool VariableLengthStructField::HasParameterValidator() const { return false; }
76 
GenParameterValidator(std::ostream &) const77 void VariableLengthStructField::GenParameterValidator(std::ostream&) const {
78   // Validated at compile time.
79 }
80 
GenInserter(std::ostream & s) const81 void VariableLengthStructField::GenInserter(std::ostream& s) const {
82   s << GetName() << "_->Serialize(i);";
83 }
84 
GenValidator(std::ostream &) const85 void VariableLengthStructField::GenValidator(std::ostream&) const {
86   // Do nothing
87 }
88