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 51 // override them. 52 Size GetSize(bool without_payload = false) const; 53 54 // Get the offset until the field is reached, if there is no field 55 // returns an empty Size. from_end requests the offset to the field 56 // starting from the end() iterator. If there is a field with an unknown 57 // size along the traversal, then an empty size is returned. 58 Size GetOffsetForField(std::string field_name, bool from_end = false) const; 59 60 FieldList GetParamList() const; 61 62 void GenMembers(std::ostream& s) const; 63 64 void GenSize(std::ostream& s) const; 65 66 void GenSerialize(std::ostream& s) const; 67 68 void GenInstanceOf(std::ostream& s) const; 69 70 const ParentDef* GetRootDef() const; 71 72 bool HasAncestorNamed(std::string name) const; 73 74 std::map<std::string, std::variant<int64_t, std::string>> GetAllConstraints() const; 75 76 std::vector<const ParentDef*> GetAncestors() const; 77 78 std::string FindConstraintField() const; 79 80 std::map<const ParentDef*, const std::variant<int64_t, std::string>> 81 FindDescendantsWithConstraint(std::string constraint_name) const; 82 std::vector<const ParentDef*> FindPathToDescendant(std::string descendant) const; 83 84 FieldList fields_; 85 86 ParentDef* parent_{nullptr}; 87 88 ParentDef* complement_{nullptr}; 89 90 std::vector<ParentDef*> children_; 91 92 std::set<std::string> test_cases_; 93 std::map<std::string, std::variant<int64_t, std::string>> parent_constraints_; 94 bool is_little_endian_; 95 96 bool HasChildEnums() const; 97 98 void GenSizeRetVal(std::ostream& s) const; 99 }; 100