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 #pragma once 18 19 #include <cstdint> 20 #include <map> 21 #include <set> 22 #include <variant> 23 #include <vector> 24 25 #include "enum_def.h" 26 #include "field_list.h" 27 #include "fields/all_fields.h" 28 #include "fields/packet_field.h" 29 #include "parse_location.h" 30 #include "type_def.h" 31 32 class ParentDef : public TypeDef { 33 public: 34 ParentDef(std::string name, FieldList fields); 35 ParentDef(std::string name, FieldList fields, ParentDef* parent); 36 37 void AddParentConstraint(std::string field_name, std::variant<int64_t, std::string> value); 38 39 void AddTestCase(std::string packet_bytes); 40 41 // Assign all size fields to their corresponding variable length fields. 42 // Will crash if 43 // - there aren't any fields that don't match up to a field. 44 // - the size field points to a fixed size field. 45 // - if the size field comes after the variable length field. 46 void AssignSizeFields(); 47 48 void SetEndianness(bool is_little_endian); 49 50 // Get the size. You scan specify without_payload to exclude payload and body fields as children override them. 51 Size GetSize(bool without_payload = false) const; 52 53 // Get the offset until the field is reached, if there is no field 54 // returns an empty Size. from_end requests the offset to the field 55 // starting from the end() iterator. If there is a field with an unknown 56 // size along the traversal, then an empty size is returned. 57 Size GetOffsetForField(std::string field_name, bool from_end = false) const; 58 59 FieldList GetParamList() const; 60 61 void GenMembers(std::ostream& s) const; 62 63 void GenSize(std::ostream& s) const; 64 65 void GenSerialize(std::ostream& s) const; 66 67 void GenInstanceOf(std::ostream& s) const; 68 69 const ParentDef* GetRootDef() const; 70 71 bool HasAncestorNamed(std::string name) const; 72 73 std::map<std::string, std::variant<int64_t, std::string>> GetAllConstraints() const; 74 75 std::vector<const ParentDef*> GetAncestors() const; 76 77 std::string FindConstraintField() const; 78 79 std::map<const ParentDef*, const std::variant<int64_t, std::string>> 80 FindDescendantsWithConstraint(std::string constraint_name) const; 81 std::vector<const ParentDef*> FindPathToDescendant(std::string descendant) const; 82 83 FieldList fields_; 84 85 ParentDef* parent_{nullptr}; 86 87 ParentDef* complement_{nullptr}; 88 89 std::vector<ParentDef*> children_; 90 91 std::set<std::string> test_cases_; 92 std::map<std::string, std::variant<int64_t, std::string>> parent_constraints_; 93 bool is_little_endian_; 94 95 bool HasChildEnums() const; 96 97 void GenSizeRetVal(std::ostream& s) const; 98 }; 99