Skip to content

Commit

Permalink
Zwischenstand: ab hier müßte der Code zur Laufzeit testen, welcher Fa…
Browse files Browse the repository at this point in the history
…ll vorliegt, um den richtigen Code zur Serialisierung/Deserialisierung zu wählen
  • Loading branch information
Jens-G committed Feb 5, 2025
1 parent dc4cda2 commit b7b9368
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 7 deletions.
6 changes: 3 additions & 3 deletions compiler/cpp/src/thrift/generate/t_delphi_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2637,7 +2637,8 @@ void t_delphi_generator::generate_deserialize_field(ostream& out,
string prefix,
ostream& local_vars,
std::map<std::string, mapped_type>* generic) {
t_type* type = tfield->get_type()->get_true_type(generic);
const mapped_type* mapped = tfield->get_type()->try_get_true_type(generic);
t_type* type = mapped->get_type() != nullptr ? mapped->get_type() : tfield->get_type();

if (type->is_void()) {
throw "CANNOT GENERATE DESERIALIZE CODE FOR void TYPE: " + prefix + tfield->get_name();
Expand Down Expand Up @@ -3081,8 +3082,7 @@ string t_delphi_generator::type_name(t_type* ttype,
return type_name(resolved->get_type(), b_cls, b_no_postfix, generic);
} else if(resolved->is_generic_decl()) {
return std::string("<" + normalize_name(resolved->symbolic()) + ">");
}
else {
} else {
throw "Unresolved forward declaration: Used type never defined: " + tdef->get_symbolic();
}
} else {
Expand Down
18 changes: 17 additions & 1 deletion compiler/cpp/src/thrift/parse/parse.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,24 @@ t_type* t_type::get_true_type(std::map<std::string, mapped_type>* generic) {

const t_type* t_type::get_true_type(std::map<std::string, mapped_type>* generic) const {
const t_type* type = this;
while (type->is_typedef()) {
const t_type* prev = nullptr;
while (type->is_typedef() && (prev != type)) {
prev = type;
type = ((t_typedef*)type)->get_type(generic);
}
return type;
}

mapped_type* t_type::try_get_true_type(std::map<std::string, mapped_type>* generic) {
return const_cast<mapped_type*>(const_cast<const t_type*>(this)->try_get_true_type(generic));
}

const mapped_type* t_type::try_get_true_type(std::map<std::string, mapped_type>* generic) const {
const mapped_type* mapped = new mapped_type(get_name(), this);
const t_type* type = mapped->get_type();
while ((type != nullptr) && type->is_typedef()) {
mapped = ((t_typedef*)type)->get_generic_type(generic);
type = type = mapped->get_type();
}
return mapped;
}
5 changes: 4 additions & 1 deletion compiler/cpp/src/thrift/parse/t_scope.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ class t_scope {
public:
t_scope() = default;

void add_type(std::string name, t_type* type) { types_[name] = type; }
void add_type(std::string name, t_type* type) {
types_[name] = type;
type->mark_as_declaration();
}

t_type* get_type(std::string name) { return types_[name]; }

Expand Down
8 changes: 8 additions & 0 deletions compiler/cpp/src/thrift/parse/t_struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ class t_struct : public t_type {

virtual std::vector<std::string>* get_template_decl_type() const { return tmpl_decl_type_; }

virtual void mark_as_declaration() {
members_type::const_iterator m_iter;
for (m_iter = members_.begin(); m_iter != members_.end(); ++m_iter) {
(*m_iter)->get_type()->mark_as_declaration();
}
};


bool is_generic_type() const {
return (tmpl_decl_type_ != nullptr) && (tmpl_decl_type_->size() > 0);
}
Expand Down
6 changes: 6 additions & 0 deletions compiler/cpp/src/thrift/parse/t_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,19 @@ class t_type : public t_doc {
virtual bool is_service() const { return false; }
virtual bool is_specialized_generic() const { return false; }

virtual void mark_as_declaration() {};

t_program* get_program() { return program_; }

const t_program* get_program() const { return program_; }

t_type* get_true_type(std::map<std::string, mapped_type>* generic = nullptr);
const t_type* get_true_type(std::map<std::string, mapped_type>* generic = nullptr) const;

mapped_type* t_type::try_get_true_type(std::map<std::string, mapped_type>* generic);
const mapped_type* t_type::try_get_true_type(std::map<std::string, mapped_type>* generic) const;


// This function will break (maybe badly) unless 0 <= num <= 16.
static char nybble_to_xdigit(int num) {
if (num < 10) {
Expand Down
6 changes: 5 additions & 1 deletion compiler/cpp/src/thrift/parse/t_typedef.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ const t_type* t_typedef::get_type(std::map<std::string, mapped_type>* generic) c
return mapped->get_type();
}

if (is_declaration()) {
return this; // generic type declaration is fully specialized at usage, not declaration
}

printf("Type \"%s\" not defined\n", symbolic_.c_str());
exit(1);
}
Expand All @@ -52,6 +56,6 @@ const mapped_type* t_typedef::get_generic_type(std::map<std::string, mapped_type
}
}

return new mapped_type(symbolic_, false);
return new mapped_type(symbolic_, is_declaration());
}

5 changes: 4 additions & 1 deletion compiler/cpp/src/thrift/parse/t_typedef.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,18 @@ class t_typedef : public t_type {

const std::string get_symbolic() const {return symbolic_; }


bool is_forward_typedef() const { return forward_; }

bool is_typedef() const override { return true; }

bool is_declaration() const { return is_type_decl_; }
virtual void mark_as_declaration() { is_type_decl_ = true; };

private:
t_type* type_;
std::string symbolic_;
bool forward_;
bool is_type_decl_; // true for declarations, e.g. type of a (possibly generic) struct field, false for usage helpers

};

Expand Down

0 comments on commit b7b9368

Please sign in to comment.