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
19 #include "util.h"
20
21 const std::string StructField::kFieldType = "StructField";
22
StructField(std::string name,std::string type_name,Size size,ParseLocation loc)23 StructField::StructField(std::string name, std::string type_name, Size size, ParseLocation loc)
24 : PacketField(name, loc), type_name_(type_name), size_(size) {}
25
GetFieldType() const26 const std::string& StructField::GetFieldType() const { return StructField::kFieldType; }
27
GetSize() const28 Size StructField::GetSize() const { return size_; }
29
GetBuilderSize() const30 Size StructField::GetBuilderSize() const {
31 std::string ret = "(" + GetName() + "_.size() * 8)";
32 return ret;
33 }
34
GetDataType() const35 std::string StructField::GetDataType() const { return type_name_; }
36
GenExtractor(std::ostream & s,int,bool) const37 void StructField::GenExtractor(std::ostream& s, int, bool) const {
38 s << GetName() << "_it = ";
39 s << GetDataType() << "::Parse(" << GetName() << "_ptr, " << GetName() << "_it);";
40 }
41
GetGetterFunctionName() const42 std::string StructField::GetGetterFunctionName() const {
43 std::stringstream ss;
44 ss << "Get" << util::UnderscoreToCamelCase(GetName());
45 return ss.str();
46 }
47
GenGetter(std::ostream & s,Size start_offset,Size end_offset) const48 void StructField::GenGetter(std::ostream& s, Size start_offset, Size end_offset) const {
49 s << GetDataType() << " " << GetGetterFunctionName() << "() const {";
50 s << "ASSERT(was_validated_);";
51 s << "size_t end_index = size();";
52 s << "auto to_bound = begin();";
53 int num_leading_bits = GenBounds(s, start_offset, end_offset, GetSize());
54 s << GetDataType() << " " << GetName() << "_value{};";
55 s << GetDataType() << "* " << GetName() << "_ptr = &" << GetName() << "_value;";
56 GenExtractor(s, num_leading_bits, false);
57
58 s << "return " << GetName() << "_value;";
59 s << "}\n";
60 }
61
GetBuilderParameterType() const62 std::string StructField::GetBuilderParameterType() const { return GetDataType(); }
63
HasParameterValidator() const64 bool StructField::HasParameterValidator() const { return false; }
65
GenParameterValidator(std::ostream &) const66 void StructField::GenParameterValidator(std::ostream&) const {
67 // Validated at compile time.
68 }
69
GenInserter(std::ostream & s) const70 void StructField::GenInserter(std::ostream& s) const { s << GetName() << "_.Serialize(i);"; }
71
GenValidator(std::ostream &) const72 void StructField::GenValidator(std::ostream&) const {
73 // Do nothing
74 }
75
GenStringRepresentation(std::ostream & s,std::string accessor) const76 void StructField::GenStringRepresentation(std::ostream& s, std::string accessor) const {
77 s << accessor << ".ToString()";
78 }
79