Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 322f9d4e authored by Myles Watson's avatar Myles Watson Committed by android-build-merger
Browse files

Merge "PDL: Add GetStructOffset and GetStructSize" am: dc98c3b2

am: 3a11d74a

Change-Id: I75646f0784fe88f7c8453429278f67a967286c82
parents 4a6c0a6a 3a11d74a
Loading
Loading
Loading
Loading
+14 −0
Original line number Diff line number Diff line
@@ -65,6 +65,20 @@ Size ArrayField::GetBuilderSize() const {
  }
}

Size ArrayField::GetStructSize() const {
  if (!element_size_.empty() && !element_size_.has_dynamic()) {
    return GetSize();
  } else if (element_field_->BuilderParameterMustBeMoved()) {
    std::string ret = "[this](){ size_t length = 0; for (const auto& elem : to_fill->" + GetName() +
                      "_) { length += elem->size() * 8; } return length; }()";
    return ret;
  } else {
    std::string ret = "[this](){ size_t length = 0; for (const auto& elem : to_fill->" + GetName() +
                      "_) { length += elem.size() * 8; } return length; }()";
    return ret;
  }
}

std::string ArrayField::GetDataType() const {
  return "std::array<" + element_field_->GetDataType() + "," + std::to_string(array_size_) + ">";
}
+2 −0
Original line number Diff line number Diff line
@@ -34,6 +34,8 @@ class ArrayField : public PacketField {

  virtual Size GetBuilderSize() const override;

  virtual Size GetStructSize() const override;

  virtual std::string GetDataType() const override;

  virtual void GenExtractor(std::ostream& s, int num_leading_bits, bool for_struct) const override;
+4 −0
Original line number Diff line number Diff line
@@ -36,6 +36,10 @@ Size PacketField::GetBuilderSize() const {
  return GetSize();
}

Size PacketField::GetStructSize() const {
  return GetSize();
}

int PacketField::GenBounds(std::ostream& s, Size start_offset, Size end_offset, Size size) const {
  // In order to find field_begin and field_end, we must have two of the three Sizes.
  if ((start_offset.empty() && size.empty()) || (start_offset.empty() && end_offset.empty()) ||
+4 −0
Original line number Diff line number Diff line
@@ -40,6 +40,10 @@ class PacketField : public Loggable {
  // For most field types, this will be the same as GetSize();
  virtual Size GetBuilderSize() const;

  // Returns the size of the field in bits given the information in the parsed struct.
  // For most field types, this will be the same as GetSize();
  virtual Size GetStructSize() const;

  // Get the type of the field to be used in the builders constructor and
  // variables.
  virtual std::string GetDataType() const = 0;
+25 −3
Original line number Diff line number Diff line
@@ -52,14 +52,14 @@ Size VectorField::GetSize() const {

  // size_field_ is of type SIZE
  if (size_field_->GetFieldType() == SizeField::kFieldType) {
    std::string ret = "(Get" + util::UnderscoreToCamelCase(size_field_->GetName()) + "() * 8)";
    std::string ret = "(static_cast<size_t>(Get" + util::UnderscoreToCamelCase(size_field_->GetName()) + "()) * 8)";
    if (!size_modifier_.empty()) ret += size_modifier_;
    return ret;
  }

  // size_field_ is of type COUNT and elements have a fixed size
  if (!element_size_.empty() && !element_size_.has_dynamic()) {
    return "(Get" + util::UnderscoreToCamelCase(size_field_->GetName()) + "() * " +
    return "(static_cast<size_t>(Get" + util::UnderscoreToCamelCase(size_field_->GetName()) + "()) * " +
           std::to_string(element_size_.bits()) + ")";
  }

@@ -68,7 +68,7 @@ Size VectorField::GetSize() const {

Size VectorField::GetBuilderSize() const {
  if (!element_size_.empty() && !element_size_.has_dynamic()) {
    std::string ret = "(" + GetName() + "_.size() * " + std::to_string(element_size_.bits()) + ")";
    std::string ret = "(static_cast<size_t>(" + GetName() + "_.size()) * " + std::to_string(element_size_.bits()) + ")";
    return ret;
  } else if (element_field_->BuilderParameterMustBeMoved()) {
    std::string ret = "[this](){ size_t length = 0; for (const auto& elem : " + GetName() +
@@ -81,6 +81,28 @@ Size VectorField::GetBuilderSize() const {
  }
}

Size VectorField::GetStructSize() const {
  // If there is no size field, then it is of unknown size.
  if (size_field_ == nullptr) {
    return Size();
  }

  // size_field_ is of type SIZE
  if (size_field_->GetFieldType() == SizeField::kFieldType) {
    std::string ret = "(static_cast<size_t>(" + size_field_->GetName() + "_extracted) * 8)";
    if (!size_modifier_.empty()) ret += size_modifier_;
    return ret;
  }

  // size_field_ is of type COUNT and elements have a fixed size
  if (!element_size_.empty() && !element_size_.has_dynamic()) {
    return "(static_cast<size_t>(" + size_field_->GetName() + "_extracted) * " + std::to_string(element_size_.bits()) +
           ")";
  }

  return Size();
}

std::string VectorField::GetDataType() const {
  return "std::vector<" + element_field_->GetDataType() + ">";
}
Loading