std::string t_hack_generator::type_to_typehint()

in thrift/compiler/generate/t_hack_generator.cc [5425:5524]


std::string t_hack_generator::type_to_typehint(
    const t_type* ttype,
    bool nullable,
    bool shape,
    bool immutable_collections,
    bool ignore_adapter) {
  if (!ignore_adapter) {
    // Check the adapter before resolving typedefs.
    if (const auto* adapter = find_hack_adapter(ttype)) {
      return *adapter + "::THackType";
    }
  }
  ttype = ttype->get_true_type();
  immutable_collections = immutable_collections || const_collections_;
  if (ttype->is_base_type()) {
    switch (((t_base_type*)ttype)->get_base()) {
      case t_base_type::TYPE_VOID:
        return "void";
      case t_base_type::TYPE_STRING:
      case t_base_type::TYPE_BINARY:
        return "string";
      case t_base_type::TYPE_BOOL:
        return "bool";
      case t_base_type::TYPE_BYTE:
      case t_base_type::TYPE_I16:
      case t_base_type::TYPE_I32:
      case t_base_type::TYPE_I64:
        return "int";
      case t_base_type::TYPE_DOUBLE:
      case t_base_type::TYPE_FLOAT:
        return "float";
      default:
        return "mixed";
    }
  } else if (const auto* tenum = dynamic_cast<const t_enum*>(ttype)) {
    if (is_bitmask_enum(tenum)) {
      return "int";
    } else {
      return (nullable ? "?" : "") + hack_name(ttype);
    }
  } else if (ttype->is_struct() || ttype->is_xception()) {
    return (nullable ? "?" : "") + hack_name(ttype) + (shape ? "::TShape" : "");
  } else if (const auto* tlist = dynamic_cast<const t_list*>(ttype)) {
    std::string prefix;
    if (arrays_) {
      prefix = "vec";
    } else if (no_use_hack_collections_) {
      prefix = "varray";
    } else if (shape) {
      prefix = array_migration_ ? "varray" : "vec";
    } else {
      prefix = immutable_collections ? "\\ConstVector" : "Vector";
    }
    return prefix + "<" +
        type_to_typehint(
               tlist->get_elem_type(), false, shape, immutable_collections) +
        ">";
  } else if (const auto* tmap = dynamic_cast<const t_map*>(ttype)) {
    std::string prefix;
    if (arrays_) {
      prefix = "dict";
    } else if (no_use_hack_collections_) {
      prefix = "darray";
    } else if (shape) {
      prefix = array_keyword_;
    } else {
      prefix = immutable_collections ? "\\ConstMap" : "Map";
    }
    std::string key_type = type_to_typehint(
        tmap->get_key_type(), false, shape, immutable_collections);
    if (shape && shape_arraykeys_ && key_type == "string") {
      key_type = "arraykey";
    } else if (!is_type_arraykey(tmap->get_key_type())) {
      key_type = "arraykey";
    }
    return prefix + "<" + key_type + ", " +
        type_to_typehint(
               tmap->get_val_type(), false, shape, immutable_collections) +
        ">";
  } else if (const auto* tset = dynamic_cast<const t_set*>(ttype)) {
    std::string prefix;
    if (arraysets_) {
      prefix = array_keyword_;
    } else if (arrays_) {
      prefix = "keyset";
    } else if (shape) {
      prefix = array_keyword_;
    } else {
      prefix = immutable_collections ? "\\ConstSet" : "Set";
    }
    std::string suffix = (arraysets_ || (shape && !arrays_)) ? ", bool>" : ">";
    std::string key_type = !is_type_arraykey(tset->get_elem_type())
        ? "arraykey"
        : type_to_typehint(
              tset->get_elem_type(), false, shape, immutable_collections);
    return prefix + "<" + key_type + suffix;
  } else {
    return "mixed";
  }
}