From 3e53e5002803c5d1dc537ed8fb4376099f38805a Mon Sep 17 00:00:00 2001 From: Hwankyu Jhun Date: Mon, 5 Mar 2018 14:57:34 +0900 Subject: [PATCH] Fix C# generator - Fixes calling serialize/deserialize methods Change-Id: I090cbeb7a530f2b8e8a7323babd57bc11fd7cc40 Signed-off-by: Hwankyu Jhun --- idlc/cs_gen/cs_gen_base.cc | 37 +++++++++++++++++++++++-------------- idlc/cs_gen/cs_gen_base.h | 15 ++++++++++----- 2 files changed, 33 insertions(+), 19 deletions(-) diff --git a/idlc/cs_gen/cs_gen_base.cc b/idlc/cs_gen/cs_gen_base.cc index ba9d1a3..155721f 100644 --- a/idlc/cs_gen/cs_gen_base.cc +++ b/idlc/cs_gen/cs_gen_base.cc @@ -256,7 +256,7 @@ std::string CsGeneratorBase::ConvertTypeToParcelType(const std::string& key) { std::string CsGeneratorBase::ConvertTypeToDeserializer( const BaseType& type, std::string id, std::string parcel, - bool make_new_type) { + bool make_new_type, std::string iface_id) { if (type.IsUserDefinedType() || type.GetMetaType() != nullptr) { std::string n; @@ -277,6 +277,8 @@ std::string CsGeneratorBase::ConvertTypeToDeserializer( } else { ret += id + " = new " + n +"();\n"; } + if (iface_id != "") + ret += iface_id + "."; ret += "Deserialize(" + parcel + ", " + id +");\n"; return ret; } @@ -299,18 +301,22 @@ std::string CsGeneratorBase::ConvertTypeToDeserializer( } std::string CsGeneratorBase::ConvertTypeToSerializer( - const BaseType& type, std::string id, std::string parcel) { + const BaseType& type, std::string id, std::string parcel, + std::string iface_id) { + std::string ret; if (type.IsUserDefinedType() || type.GetMetaType() != nullptr) { if (IsDelegateType(type.ToString())) return "CallbackBase.Serialize(" + parcel + ", " + id + ");\n"; - return "Serialize(" + parcel + ", " + id + ");\n"; + if (iface_id != "") + ret += iface_id + "."; + ret += "Serialize(" + parcel + ", " + id + ");\n"; + return ret; } - std::string ret = "Interop.LibRPCPort.Parcel.Write" + - parcel_type_map_[type.ToString()] + - "(" + parcel + ", " + id + ");\n"; + ret += "Interop.LibRPCPort.Parcel.Write" + + parcel_type_map_[type.ToString()] + "(" + parcel + ", " + id + ");\n"; return ret; } @@ -453,12 +459,13 @@ void CsGeneratorBase::GenCallbacks(std::ofstream& stream, for (auto& i : iface.GetDeclarations().GetDecls()) { if (i->GetMethodType() != Declaration::MethodType::DELEGATE) continue; - GenCallback(stream, *i); + GenCallback(stream, *i, iface.GetID()); } } void CsGeneratorBase::GenCallback(std::ofstream& stream, - const Declaration& decl) { + const Declaration& decl, + const std::string& id) { stream << Tab(3) << "public sealed class " << decl.GetID() << " : CallbackBase" << NLine(1); GenBrace(stream, TAB_SIZE * 3, [&]() { @@ -496,14 +503,15 @@ void CsGeneratorBase::GenCallback(std::ofstream& stream, stream << ");" << NLine(1); stream << Tab(4) << "public event Callback Received;" << NLine(2); - GenReceivedEvent(stream, decl); - GenInvokeMethod(stream, decl); + GenReceivedEvent(stream, decl, id); + GenInvokeMethod(stream, decl, id); }); stream << NLine(1); } void CsGeneratorBase::GenReceivedEvent(std::ofstream& stream, - const Declaration& decl) { + const Declaration& decl, + const std::string& id) { stream << Tab(4) << "internal override void OnReceivedEvent(IntPtr parcel)" << NLine(1); GenBrace(stream, TAB_SIZE * 4, [&]() { @@ -511,7 +519,7 @@ void CsGeneratorBase::GenReceivedEvent(std::ofstream& stream, for (auto& i : decl.GetParameters().GetParams()) { std::string v = "param" + std::to_string(cnt); std::string c = ConvertTypeToDeserializer( - i->GetParameterType().GetBaseType(), v, "parcel"); + i->GetParameterType().GetBaseType(), v, "parcel", true, id); stream << AddIndent(TAB_SIZE * 5, c); cnt++; } @@ -532,7 +540,8 @@ void CsGeneratorBase::GenReceivedEvent(std::ofstream& stream, } void CsGeneratorBase::GenInvokeMethod(std::ofstream& stream, - const Declaration& decl) { + const Declaration& decl, + const std::string& id) { const char* pre = "if (_port == IntPtr.Zero || _service == null)\n" \ " throw new InvalidOperationException(\"Not connected!\");\n" \ @@ -556,7 +565,7 @@ void CsGeneratorBase::GenInvokeMethod(std::ofstream& stream, std::string m; for (auto& i : decl.GetParameters().GetParams()) { auto& pt = i->GetParameterType(); - m += ConvertTypeToSerializer(pt.GetBaseType(), i->GetID(), "p"); + m += ConvertTypeToSerializer(pt.GetBaseType(), i->GetID(), "p", id); } stream << AddIndent(TAB_SIZE * 5, m) << NLine(1); diff --git a/idlc/cs_gen/cs_gen_base.h b/idlc/cs_gen/cs_gen_base.h index d5865a2..7547853 100644 --- a/idlc/cs_gen/cs_gen_base.h +++ b/idlc/cs_gen/cs_gen_base.h @@ -48,9 +48,11 @@ class CsGeneratorBase : public Generator { std::string ConvertTypeToString(const BaseType& type); std::string ConvertTypeToDeserializer(const BaseType& type, std::string id, std::string parcel, - bool make_new_type = true); + bool make_new_type = true, + const std::string iface_id = ""); std::string ConvertTypeToSerializer(const BaseType& type, - std::string id, std::string parcel); + std::string id, std::string parcel, + const std::string iface_id = ""); std::string ConvertTypeToParcelType(const std::string& key); bool IsDelegateType(const std::string type_name); std::string Tab(int cnt); @@ -62,9 +64,12 @@ class CsGeneratorBase : public Generator { private: void GenWriteBundle(std::ofstream& stream, const std::string& id); void AddSerializerList(const BaseType& type); - void GenCallback(std::ofstream& stream, const Declaration& decl); - void GenReceivedEvent(std::ofstream& stream, const Declaration& decl); - void GenInvokeMethod(std::ofstream& stream, const Declaration& decl); + void GenCallback(std::ofstream& stream, const Declaration& decl, + const std::string& id); + void GenReceivedEvent(std::ofstream& stream, const Declaration& decl, + const std::string& id); + void GenInvokeMethod(std::ofstream& stream, const Declaration& decl, + const std::string& id); private: std::map type_map_; -- 2.7.4