aboutsummaryrefslogtreecommitdiff
path: root/gd/packet/parser/fields/struct_field.cc
blob: 75ad1abb05fa3517a05e3d010f559bef2fa9fdc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
 * Copyright 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "fields/struct_field.h"
#include "util.h"

const std::string StructField::kFieldType = "StructField";

StructField::StructField(std::string name, std::string type_name, Size size, ParseLocation loc)
    : PacketField(name, loc), type_name_(type_name), size_(size) {}

const std::string& StructField::GetFieldType() const {
  return StructField::kFieldType;
}

Size StructField::GetSize() const {
  return size_;
}

Size StructField::GetBuilderSize() const {
  std::string ret = "(" + GetName() + "_.size() * 8)";
  return ret;
}

std::string StructField::GetDataType() const {
  return type_name_;
}

void StructField::GenExtractor(std::ostream& s, int, bool) const {
  s << GetName() << "_it = ";
  s << GetDataType() << "::Parse(" << GetName() << "_ptr, " << GetName() << "_it);";
}

std::string StructField::GetGetterFunctionName() const {
  std::stringstream ss;
  ss << "Get" << util::UnderscoreToCamelCase(GetName());
  return ss.str();
}

void StructField::GenGetter(std::ostream& s, Size start_offset, Size end_offset) const {
  s << GetDataType() << " " << GetGetterFunctionName() << "() const {";
  s << "ASSERT(was_validated_);";
  s << "size_t end_index = size();";
  s << "auto to_bound = begin();";
  int num_leading_bits = GenBounds(s, start_offset, end_offset, GetSize());
  s << GetDataType() << " " << GetName() << "_value{};";
  s << GetDataType() << "* " << GetName() << "_ptr = &" << GetName() << "_value;";
  GenExtractor(s, num_leading_bits, false);

  s << "return " << GetName() << "_value;";
  s << "}\n";
}

std::string StructField::GetBuilderParameterType() const {
  return GetDataType();
}

bool StructField::HasParameterValidator() const {
  return false;
}

void StructField::GenParameterValidator(std::ostream&) const {
  // Validated at compile time.
}

void StructField::GenInserter(std::ostream& s) const {
  s << GetName() << "_.Serialize(i);";
}

void StructField::GenValidator(std::ostream&) const {
  // Do nothing
}

void StructField::GenStringRepresentation(std::ostream& s, std::string accessor) const {
  s << accessor << ".ToString()";
}

std::string StructField::GetRustDataType() const {
  return GetDataType();
}

void StructField::GenBoundsCheck(std::ostream&, Size, Size, std::string) const {
  // implicitly checked by the struct parser
}

void StructField::GenRustGetter(std::ostream& s, Size start_offset, Size) const {
  s << "let " << GetName() << " = ";
  s << GetRustDataType() << "::parse(&bytes[" << start_offset.bytes() << "..";
  s << start_offset.bytes() + GetSize().bytes() << "]).unwrap();";
}

void StructField::GenRustWriter(std::ostream& s, Size start_offset, Size) const {
  s << "let " << GetName() << " = &mut buffer[" << start_offset.bytes();
  s << ".." << start_offset.bytes() + GetSize().bytes() << "];";
  s << "self." << GetName() << ".write_to(" << GetName() << ");";
}