• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/struct_field.h"
18 #include "util.h"
19 
20 const std::string StructField::kFieldType = "StructField";
21 
StructField(std::string name,std::string type_name,Size size,ParseLocation loc)22 StructField::StructField(std::string name, std::string type_name, Size size, ParseLocation loc)
23     : PacketField(name, loc), type_name_(type_name), size_(size) {}
24 
GetFieldType() const25 const std::string& StructField::GetFieldType() const {
26   return StructField::kFieldType;
27 }
28 
GetSize() const29 Size StructField::GetSize() const {
30   return size_;
31 }
32 
GetBuilderSize() const33 Size StructField::GetBuilderSize() const {
34   std::string ret = "(" + GetName() + "_.size() * 8)";
35   return ret;
36 }
37 
GetDataType() const38 std::string StructField::GetDataType() const {
39   return type_name_;
40 }
41 
GenExtractor(std::ostream & s,int,bool) const42 void StructField::GenExtractor(std::ostream& s, int, bool) const {
43   s << GetName() << "_it = ";
44   s << GetDataType() << "::Parse(" << GetName() << "_ptr, " << GetName() << "_it);";
45 }
46 
GetGetterFunctionName() const47 std::string StructField::GetGetterFunctionName() const {
48   std::stringstream ss;
49   ss << "Get" << util::UnderscoreToCamelCase(GetName());
50   return ss.str();
51 }
52 
GenGetter(std::ostream & s,Size start_offset,Size end_offset) const53 void StructField::GenGetter(std::ostream& s, Size start_offset, Size end_offset) const {
54   s << GetDataType() << " " << GetGetterFunctionName() << "() const {";
55   s << "ASSERT(was_validated_);";
56   s << "size_t end_index = size();";
57   s << "auto to_bound = begin();";
58   int num_leading_bits = GenBounds(s, start_offset, end_offset, GetSize());
59   s << GetDataType() << " " << GetName() << "_value{};";
60   s << GetDataType() << "* " << GetName() << "_ptr = &" << GetName() << "_value;";
61   GenExtractor(s, num_leading_bits, false);
62 
63   s << "return " << GetName() << "_value;";
64   s << "}\n";
65 }
66 
GetBuilderParameterType() const67 std::string StructField::GetBuilderParameterType() const {
68   return GetDataType();
69 }
70 
HasParameterValidator() const71 bool StructField::HasParameterValidator() const {
72   return false;
73 }
74 
GenParameterValidator(std::ostream &) const75 void StructField::GenParameterValidator(std::ostream&) const {
76   // Validated at compile time.
77 }
78 
GenInserter(std::ostream & s) const79 void StructField::GenInserter(std::ostream& s) const {
80   s << GetName() << "_.Serialize(i);";
81 }
82 
GenValidator(std::ostream &) const83 void StructField::GenValidator(std::ostream&) const {
84   // Do nothing
85 }
86 
GenStringRepresentation(std::ostream & s,std::string accessor) const87 void StructField::GenStringRepresentation(std::ostream& s, std::string accessor) const {
88   s << accessor << ".ToString()";
89 }
90 
GetRustDataType() const91 std::string StructField::GetRustDataType() const {
92   return GetDataType();
93 }
94 
GenBoundsCheck(std::ostream &,Size,Size,std::string) const95 void StructField::GenBoundsCheck(std::ostream&, Size, Size, std::string) const {
96   // implicitly checked by the struct parser
97 }
98 
GenRustGetter(std::ostream & s,Size start_offset,Size) const99 void StructField::GenRustGetter(std::ostream& s, Size start_offset, Size) const {
100   s << "let " << GetName() << " = ";
101   s << GetRustDataType() << "::parse(&bytes[" << start_offset.bytes() << "..";
102   s << start_offset.bytes() + GetSize().bytes() << "]).unwrap();";
103 }
104 
GenRustWriter(std::ostream & s,Size start_offset,Size) const105 void StructField::GenRustWriter(std::ostream& s, Size start_offset, Size) const {
106   s << "let " << GetName() << " = &mut buffer[" << start_offset.bytes();
107   s << ".." << start_offset.bytes() + GetSize().bytes() << "];";
108   s << "self." << GetName() << ".write_to(" << GetName() << ");";
109 }
110