Skip to content

Refactor: Create shared type mapping utilities #147

Description

@clank-hayen

Problem

Type mapping from Bishop types to C++ types is done in multiple places with slightly different implementations:

  1. codegen/codegen.hpp has `map_type()` function
  2. codegen/emit_function.cpp has `get_cpp_return_type()`
  3. Various files have inline handling for Result, optional types, etc.

Example duplication:

// In emit_function.cpp
static string get_cpp_return_type(const string& return_type, const string& error_type) {
    if (error_type.empty()) {
        return return_type.empty() ? "void" : map_type(return_type);
    }
    if (return_type.empty()) {
        return "bishop::rt::Result<void>";
    }
    return "bishop::rt::Result<" + map_type(return_type) + ">";
}

This logic for handling fallible functions is repeated in:

  • `generate_method_declaration`
  • `generate_standalone_method`
  • `method_def`
  • `static_method_def`

Suggested Solution

Create a centralized type mapping module:

// codegen/type_mapping.hpp

namespace codegen {

struct CppType {
    string base;
    bool is_result = false;
    bool is_optional = false;
};

// Main entry point for all Bishop -> C++ type conversion
CppType to_cpp_type(const string& bishop_type, 
                     const string& error_type = "");

// Utility to format CppType as string
string format_cpp_type(const CppType& type);

// Common patterns
string result_type(const string& inner);    // bishop::rt::Result<T>
string optional_type(const string& inner);  // std::optional<T>
string list_type(const string& elem);       // std::vector<T>
// ...

}

Impact

  • Single source of truth for type mapping
  • Easier to change C++ representations
  • Consistent handling of special types (Result, Optional, etc.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions