Skip to content

Commit

Permalink
variante t_specialized_generic ... zwischenstand
Browse files Browse the repository at this point in the history
  • Loading branch information
Jens-G committed Jan 26, 2025
1 parent 4ed0e17 commit fcd7672
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 15 deletions.
17 changes: 13 additions & 4 deletions compiler/cpp/src/thrift/parse/t_program.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#include "thrift/parse/t_enum.h"
#include "thrift/parse/t_const.h"
#include "thrift/parse/t_struct.h"
#include "thrift/parse/t_generic_struct.h"
#include "thrift/parse/t_specialized_generic.h"
#include "thrift/parse/t_service.h"
#include "thrift/parse/t_list.h"
#include "thrift/parse/t_map.h"
Expand Down Expand Up @@ -363,19 +363,28 @@ class t_program : public t_doc {

bool get_recursive() const { return recursive_; }

virtual t_generic_struct* instantiate_template_type(t_type* type, std::vector<t_type*>* tmpl_type) {
virtual t_specialized_generic* instantiate_template_type(t_type* type, std::vector<t_type*>* tmpl_type) {
if ((tmpl_type == nullptr) || (tmpl_type->size() == 0)) {
return nullptr;
}

if (type->is_struct() || type->is_xception()) {
t_struct* declaration = (t_struct*)type;
t_generic_struct* instance = new t_generic_struct(declaration, type->get_name(), tmpl_type);
t_specialized_generic* instance = new t_specialized_generic(declaration, tmpl_type);
scope()->add_type(instance->get_symbolic(), instance);
return instance;
}

pwarning(0, "Unable to create generic type instance from %s: struct, union or exception expected", type->get_name());
if (type->is_specialized_generic()) {
t_specialized_generic* instance = (t_specialized_generic*)type;
instance = new t_specialized_generic(instance, tmpl_type);
scope()->add_type(instance->get_symbolic(), instance);
return instance;
}

pwarning(0,
"Unable to create generic type instance from %s: struct, union or exception expected",
type->get_name());
return nullptr;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,27 @@ class t_program;
*/

/**
* A struct is a container for a set of member fields that has a name. Structs
* are also used to implement exception types.
*
* A (partially or fully) specialized generic type
*/
class t_generic_struct : public t_struct {
class t_specialized_generic : public t_type {
public:
t_generic_struct(t_struct* declaration, const std::string& name, std::vector<t_type*>* instantiation)
: t_struct(declaration->get_program(), declaration->get_name()),
tmpl_inst_type_(instantiation) {}
t_specialized_generic(t_struct* declaration, std::vector<t_type*>* instantiation)
: t_type(declaration->get_program(), declaration->get_name()),
generic_declaration_(declaration),
partially_specialized_(nullptr),
tmpl_inst_type_(instantiation) {}

t_specialized_generic(t_specialized_generic* partial, std::vector<t_type*>* instantiation)
: t_type(partial->get_program(), partial->get_name()),
generic_declaration_(nullptr),
partially_specialized_(partial),
tmpl_inst_type_(instantiation) {}

virtual bool is_specialized_generic() const { return true; }

// we can be a lot actually
virtual bool is_struct() const { return get_underlying_type()->is_struct(); }
virtual bool is_xception() const { return get_underlying_type()->is_xception(); }

/*
mapped_type get_generic_type(std::map<std::string, mapped_type>* generic = nullptr);
Expand Down Expand Up @@ -103,8 +115,21 @@ class t_generic_struct : public t_struct {
virtual std::vector<t_type*>* get_template_instance_type() const { return tmpl_inst_type_; }

private:
std::vector<t_type*>* tmpl_inst_type_;
std::map<std::string, mapped_type> tmpl_mapped_generic_types_;
t_struct* generic_declaration_; // original generic type decl (maybe null)
t_specialized_generic* partially_specialized_; // partial specialisation this one is based upon (maybe null)
std::vector<t_type*>* tmpl_inst_type_; // types applied during specialization
std::map<std::string, mapped_type> tmpl_mapped_generic_types_; // cached mapping

t_type* get_underlying_type() const {
if (partially_specialized_ != nullptr) {
return partially_specialized_;
}
if (generic_declaration_ != nullptr) {
return generic_declaration_;
}
printf("Unexpected state at generic type %s\n", name_.c_str());
exit(1);
}

void validate_template_instantiation(const std::vector<std::string>* decls) const {
std::vector<t_type*>* instance = get_template_instance_type();
Expand Down
4 changes: 2 additions & 2 deletions compiler/cpp/src/thrift/parse/t_struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,8 @@ class t_struct : public t_type {
bool union_validated_;
bool xcepts_validated_;
int members_with_value_;
std::vector<std::string>* tmpl_decl_type_;
std::map<std::string, mapped_type> tmpl_mapped_decls_;
std::vector<std::string>* tmpl_decl_type_; // generic type placeholders
std::map<std::string, mapped_type> tmpl_mapped_decls_; // cached mapping

bool xsd_all_;

Expand Down
1 change: 1 addition & 0 deletions compiler/cpp/src/thrift/parse/t_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ class t_type : public t_doc {
virtual bool is_set() const { return false; }
virtual bool is_map() const { return false; }
virtual bool is_service() const { return false; }
virtual bool is_specialized_generic() const { return false; }

t_program* get_program() { return program_; }

Expand Down

0 comments on commit fcd7672

Please sign in to comment.