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/packet_field.h"
18
19 #include "util.h"
20
PacketField(std::string name,ParseLocation loc)21 PacketField::PacketField(std::string name, ParseLocation loc) : loc_(loc), name_(name) {}
22
GetDebugName() const23 std::string PacketField::GetDebugName() const {
24 return "Field{Type:" + GetFieldType() + ", Name:" + GetName() + "}";
25 }
26
GetLocation() const27 ParseLocation PacketField::GetLocation() const { return loc_; }
28
GetName() const29 std::string PacketField::GetName() const { return name_; }
30
GetBuilderSize() const31 Size PacketField::GetBuilderSize() const { return GetSize(); }
32
GetStructSize() const33 Size PacketField::GetStructSize() const { return GetSize(); }
34
GenBounds(std::ostream & s,Size start_offset,Size end_offset,Size size) const35 int PacketField::GenBounds(std::ostream& s, Size start_offset, Size end_offset, Size size) const {
36 // In order to find field_begin and field_end, we must have two of the three Sizes.
37 if ((start_offset.empty() && size.empty()) || (start_offset.empty() && end_offset.empty()) ||
38 (end_offset.empty() && size.empty())) {
39 ERROR(this) << "GenBounds called without enough information. " << start_offset << end_offset
40 << size;
41 }
42
43 if (start_offset.bits() % 8 != 0 || end_offset.bits() % 8 != 0) {
44 ERROR(this) << "Can not find the bounds of a field at a non byte-aligned offset."
45 << start_offset << end_offset;
46 }
47
48 if (!start_offset.empty()) {
49 s << "size_t field_begin = (" << start_offset << ") / 8;";
50 } else {
51 s << "size_t field_begin = end_index - (" << end_offset << " + " << size << ") / 8;";
52 }
53
54 if (!end_offset.empty()) {
55 s << "size_t field_end = end_index - (" << end_offset << ") / 8;";
56 // If the field has a known size, use the minimum for the end
57 if (!size.empty()) {
58 s << "size_t field_sized_end = field_begin + (" << size << ") / 8;";
59 s << "if (field_sized_end < field_end) { field_end = field_sized_end; }";
60 }
61 } else {
62 s << "size_t field_end = field_begin + (" << size << ") / 8;";
63 s << "if (field_end > end_index) { field_end = end_index; }";
64 }
65 s << "auto " << name_ << "_it = to_bound.Subrange(field_begin, field_end - field_begin); ";
66 return 0; // num_leading_bits
67 }
68
GenBuilderParameter(std::ostream & s) const69 bool PacketField::GenBuilderParameter(std::ostream& s) const {
70 auto param_type = GetBuilderParameterType();
71 if (param_type.empty()) {
72 return false;
73 }
74 s << param_type << " " << GetName();
75 return true;
76 }
77
BuilderParameterMustBeMoved() const78 bool PacketField::BuilderParameterMustBeMoved() const { return false; }
79
GenBuilderMember(std::ostream & s) const80 bool PacketField::GenBuilderMember(std::ostream& s) const { return GenBuilderParameter(s); }
81
GenBuilderParameterFromView(std::ostream & s) const82 void PacketField::GenBuilderParameterFromView(std::ostream& s) const {
83 s << "view.Get" << util::UnderscoreToCamelCase(GetName()) << "()";
84 }
85
IsContainerField() const86 bool PacketField::IsContainerField() const { return false; }
87
GetElementField() const88 const PacketField* PacketField::GetElementField() const { return nullptr; }
89
GenStringRepresentation(std::ostream & s,std::string accessor) const90 void PacketField::GenStringRepresentation(std::ostream& s, std::string accessor) const {
91 s << "\"REPRESENTATION_UNIMPLEMENTED " << GetFieldType() << " " << accessor << "\"";
92 }
93