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/custom_field.h"
18 #include "util.h"
19 
20 const std::string CustomField::kFieldType = "CustomField";
21 
CustomField(std::string name,std::string type_name,ParseLocation loc)22 CustomField::CustomField(std::string name, std::string type_name, ParseLocation loc)
23     : PacketField(name, loc), type_name_(type_name) {}
24 
GetFieldType() const25 const std::string& CustomField::GetFieldType() const {
26   return CustomField::kFieldType;
27 }
28 
GetSize() const29 Size CustomField::GetSize() const {
30   return Size();
31 }
32 
GetBuilderSize() const33 Size CustomField::GetBuilderSize() const {
34   std::string ret = "(" + GetName() + "_.size() * 8) ";
35   return ret;
36 }
37 
GetDataType() const38 std::string CustomField::GetDataType() const {
39   return type_name_;
40 }
41 
GenExtractor(std::ostream & s,int,bool) const42 void CustomField::GenExtractor(std::ostream& s, int, bool) const {
43   s << "auto optional_it = ";
44   s << GetDataType() << "::Parse( " << GetName() << "_ptr, " << GetName() << "_it);";
45   s << "if (optional_it) {";
46   s << GetName() << "_it = *optional_it;";
47   s << "} else {";
48   s << GetName() << "_it = " << GetName() << "_it + " << GetName() << "_it.NumBytesRemaining();";
49   s << GetName() << "_ptr = nullptr;";
50   s << "}";
51 }
52 
GetGetterFunctionName() const53 std::string CustomField::GetGetterFunctionName() const {
54   std::stringstream ss;
55   ss << "Get" << util::UnderscoreToCamelCase(GetName());
56   return ss.str();
57 }
58 
GenGetter(std::ostream & s,Size start_offset,Size end_offset) const59 void CustomField::GenGetter(std::ostream& s, Size start_offset, Size end_offset) const {
60   s << "std::unique_ptr<" << GetDataType() << "> " << GetGetterFunctionName() << "() const {";
61   s << "ASSERT(was_validated_);";
62   s << "size_t end_index = size();";
63   s << "auto to_bound = begin();";
64 
65   int num_leading_bits = GenBounds(s, start_offset, end_offset, GetSize());
66   s << "std::unique_ptr<" << GetDataType() << "> " << GetName() << "_value";
67   s << " = std::make_unique<" << GetDataType() << ">();";
68   s << GetDataType() << "* " << GetName() << "_ptr = " << GetName() << "_value.get();";
69   GenExtractor(s, num_leading_bits, false);
70   s << "if (" << GetName() << "_ptr == nullptr) {" << GetName() << "_value.reset(); }";
71   s << "return " << GetName() << "_value;";
72   s << "}\n";
73 }
74 
GetBuilderParameterType() const75 std::string CustomField::GetBuilderParameterType() const {
76   return GetDataType();
77 }
78 
HasParameterValidator() const79 bool CustomField::HasParameterValidator() const {
80   return false;
81 }
82 
GenParameterValidator(std::ostream &) const83 void CustomField::GenParameterValidator(std::ostream&) const {
84   // Do nothing.
85 }
86 
GenInserter(std::ostream & s) const87 void CustomField::GenInserter(std::ostream& s) const {
88   s << GetName() << "_.Serialize(i);";
89 }
90 
GenValidator(std::ostream &) const91 void CustomField::GenValidator(std::ostream&) const {
92   // Do nothing.
93 }
94 
GenStringRepresentation(std::ostream & s,std::string accessor) const95 void CustomField::GenStringRepresentation(std::ostream& s, std::string accessor) const {
96   s << accessor << "->ToString()";
97 }
98 
GenBuilderParameterFromView(std::ostream & s) const99 void CustomField::GenBuilderParameterFromView(std::ostream& s) const {
100   s << "*view.Get" << util::UnderscoreToCamelCase(GetName()) << "()";
101 }
102 
GetRustDataType() const103 std::string CustomField::GetRustDataType() const {
104   return type_name_;
105 }
106 
GenRustGetter(std::ostream &,Size,Size) const107 void CustomField::GenRustGetter(std::ostream&, Size, Size) const {
108 }
109 
GenRustWriter(std::ostream &,Size,Size) const110 void CustomField::GenRustWriter(std::ostream&, Size, Size) const {}
111