diff options
author | Jose <jose@zeroc.com> | 2019-07-30 12:45:29 +0200 |
---|---|---|
committer | Jose <jose@zeroc.com> | 2019-07-31 13:13:40 +0200 |
commit | 3e71d9e1a29bc8169452b039bb723406ff8ef0fe (patch) | |
tree | cfde39529b1a21d6f4c4e87b2a92a366eafc9993 /cpp | |
parent | Update .npmignore to ignore .tgz files (diff) | |
download | ice-3e71d9e1a29bc8169452b039bb723406ff8ef0fe.tar.bz2 ice-3e71d9e1a29bc8169452b039bb723406ff8ef0fe.tar.xz ice-3e71d9e1a29bc8169452b039bb723406ff8ef0fe.zip |
Fixes for C++ & Swift generated code - Close #458
Diffstat (limited to 'cpp')
-rw-r--r-- | cpp/src/Slice/CPlusPlusUtil.cpp | 6 | ||||
-rw-r--r-- | cpp/src/Slice/CPlusPlusUtil.h | 2 | ||||
-rw-r--r-- | cpp/src/slice2cpp/Gen.cpp | 61 | ||||
-rw-r--r-- | cpp/src/slice2swift/SwiftUtil.cpp | 55 | ||||
-rw-r--r-- | cpp/test/Ice/scope/AllTests.cpp | 150 | ||||
-rw-r--r-- | cpp/test/Ice/scope/Client.cpp | 2 | ||||
-rw-r--r-- | cpp/test/Ice/scope/Server.cpp | 22 | ||||
-rw-r--r-- | cpp/test/Ice/scope/Test.ice | 35 |
8 files changed, 300 insertions, 33 deletions
diff --git a/cpp/src/Slice/CPlusPlusUtil.cpp b/cpp/src/Slice/CPlusPlusUtil.cpp index 0ea48a5a2d1..de253779684 100644 --- a/cpp/src/Slice/CPlusPlusUtil.cpp +++ b/cpp/src/Slice/CPlusPlusUtil.cpp @@ -1619,8 +1619,12 @@ Slice::writeStreamHelpers(Output& out, } void -Slice::writeIceTuple(::IceUtilInternal::Output& out, const string& scope, DataMemberList dataMembers, int typeCtx) +Slice::writeIceTuple(::IceUtilInternal::Output& out, DataMemberList dataMembers, int typeCtx) { + // + // Use an empty scope to get full qualified names from calls to typeToString. + // + const string scope = ""; out << nl << "std::tuple<"; for(DataMemberList::const_iterator q = dataMembers.begin(); q != dataMembers.end(); ++q) { diff --git a/cpp/src/Slice/CPlusPlusUtil.h b/cpp/src/Slice/CPlusPlusUtil.h index b8fa3ad9e46..0380971c617 100644 --- a/cpp/src/Slice/CPlusPlusUtil.h +++ b/cpp/src/Slice/CPlusPlusUtil.h @@ -59,7 +59,7 @@ void writeEndCode(::IceUtilInternal::Output&, const ParamDeclList&, const Operat void writeMarshalUnmarshalDataMemberInHolder(IceUtilInternal::Output&, const std::string&, const DataMemberPtr&, bool); void writeMarshalUnmarshalAllInHolder(IceUtilInternal::Output&, const std::string&, const DataMemberList&, bool, bool); void writeStreamHelpers(::IceUtilInternal::Output&, const ContainedPtr&, DataMemberList, bool, bool, bool); -void writeIceTuple(::IceUtilInternal::Output&, const std::string&, DataMemberList, int); +void writeIceTuple(::IceUtilInternal::Output&, DataMemberList, int); bool findMetaData(const std::string&, const ClassDeclPtr&, std::string&); bool findMetaData(const std::string&, const StringList&, std::string&); diff --git a/cpp/src/slice2cpp/Gen.cpp b/cpp/src/slice2cpp/Gen.cpp index eab9a81b8cb..bab73d8a16e 100644 --- a/cpp/src/slice2cpp/Gen.cpp +++ b/cpp/src/slice2cpp/Gen.cpp @@ -1880,20 +1880,11 @@ Slice::Gen::TypesVisitor::visitDataMember(const DataMemberPtr& p) ContainerPtr container = p->container(); string name = fixKwd(p->name()); - string scope = ""; - StructPtr st = StructPtr::dynamicCast(container); - if(st) - { - scope = fixKwd(st->scope()); - } - - ExceptionPtr ex = ExceptionPtr::dynamicCast(container); - if(ex) - { - scope = fixKwd(ex->scope()); - } - writeDocSummary(H, p); + // + // Use an empty scope to get full qualified names from calls to typeToString. + // + const string scope = ""; H << nl << typeToString(p->type(), p->optional(), scope, p->getMetaData(), _useWstring) << ' ' << name << ';'; } @@ -3910,7 +3901,11 @@ Slice::Gen::ObjectVisitor::emitDataMember(const DataMemberPtr& p) ClassDefPtr cl = ClassDefPtr::dynamicCast(container); int typeContext = cl->isLocal() ? TypeContextLocal | _useWstring : _useWstring; writeDocSummary(H, p); - H << nl << typeToString(p->type(), p->optional(), fixKwd(cl->scope()), p->getMetaData(), typeContext) << ' ' + // + // Use an empty scope to get full qualified names from calls to typeToString. + // + const string scope = ""; + H << nl << typeToString(p->type(), p->optional(), scope, p->getMetaData(), typeContext) << ' ' << name << ';'; } @@ -4101,7 +4096,10 @@ void Slice::Gen::ObjectVisitor::emitOneShotConstructor(const ClassDefPtr& p) { DataMemberList allDataMembers = p->allDataMembers(); - string scope = fixKwd(p->scope()); + // + // Use an empty scope to get full qualified names from calls to typeToString. + // + const string scope = ""; if(!allDataMembers.empty()) { @@ -6447,7 +6445,7 @@ Slice::Gen::Cpp11TypesVisitor::visitExceptionStart(const ExceptionPtr& p) H << nl << " * Obtains a tuple containing all of the exception's data members."; H << nl << " * @return The data members in a tuple."; H << nl << " */"; - writeIceTuple(H, scope, p->allDataMembers(), _useWstring); + writeIceTuple(H, p->allDataMembers(), _useWstring); H << sp; H << nl << "/**"; @@ -6607,7 +6605,7 @@ Slice::Gen::Cpp11TypesVisitor::visitStructEnd(const StructPtr& p) H << nl << " * Obtains a tuple containing all of the struct's data members."; H << nl << " * @return The data members in a tuple."; H << nl << " */"; - writeIceTuple(H, fixKwd(p->scope()), p->dataMembers(), _useWstring); + writeIceTuple(H, p->dataMembers(), _useWstring); H << eb << ';'; _useWstring = resetUseWstring(_useWstringHist); } @@ -6615,7 +6613,10 @@ Slice::Gen::Cpp11TypesVisitor::visitStructEnd(const StructPtr& p) void Slice::Gen::Cpp11TypesVisitor::visitDataMember(const DataMemberPtr& p) { - string scope = fixKwd(ContainedPtr::dynamicCast(p->container())->scope()); + // + // Use an empty scope to get full qualified names from calls to typeToString. + // + const string scope = ""; string name = fixKwd(p->name()); writeDocSummary(H, p); H << nl << typeToString(p->type(), p->optional(), scope, p->getMetaData(), _useWstring | TypeContextCpp11) @@ -6877,7 +6878,10 @@ Slice::Gen::Cpp11ProxyVisitor::visitOperation(const OperationPtr& p) if(ret) { - futureOutParams.push_back(typeToString(ret, retIsOpt, clScope, p->getMetaData(), _useWstring | + // + // Use empty scope to get full qualified names in types used with future declarations. + // + futureOutParams.push_back(typeToString(ret, retIsOpt, "", p->getMetaData(), _useWstring | TypeContextCpp11)); lambdaOutParams.push_back(typeToString(ret, retIsOpt, clScope, p->getMetaData(), _useWstring | @@ -6893,7 +6897,10 @@ Slice::Gen::Cpp11ProxyVisitor::visitOperation(const OperationPtr& p) if((*q)->isOutParam()) { - futureOutParams.push_back(typeToString((*q)->type(), (*q)->optional(), clScope, metaData, + // + // Use empty scope to get full qualified names in types used with future declarations. + // + futureOutParams.push_back(typeToString((*q)->type(), (*q)->optional(), "", metaData, _useWstring | TypeContextCpp11)); lambdaOutParams.push_back(typeToString((*q)->type(), (*q)->optional(), clScope, metaData, _useWstring | TypeContextInParam | TypeContextCpp11)); @@ -6980,7 +6987,7 @@ Slice::Gen::Cpp11ProxyVisitor::visitOperation(const OperationPtr& p) H << "auto _result = "; } - H << "_makePromiseOutgoing<" << getUnqualified(futureT, cl->scoped()) << ">"; + H << "_makePromiseOutgoing<" << futureT << ">"; H << spar << "true, this" << "&" + cl->name() + "Prx::_iceI_" + name; for(ParamDeclList::const_iterator q = inParams.begin(); q != inParams.end(); ++q) @@ -7391,7 +7398,10 @@ Slice::Gen::Cpp11ObjectVisitor::emitDataMember(const DataMemberPtr& p) int typeContext = _useWstring | TypeContextCpp11; ContainerPtr container = p->container(); ClassDefPtr cl = ClassDefPtr::dynamicCast(container); - string scope = fixKwd(cl->scope()); + // + // Use empty scope to get full qualified names in types used with future declarations. + // + string scope = ""; if(cl->isLocal()) { typeContext |= TypeContextLocal; @@ -8512,7 +8522,7 @@ Slice::Gen::Cpp11ValueVisitor::visitClassDefStart(const ClassDefPtr& p) H << nl << " * Obtains a tuple containing all of the value's data members."; H << nl << " * @return The data members in a tuple."; H << nl << " */"; - writeIceTuple(H, fixKwd(p->scope()), p->allDataMembers(), _useWstring); + writeIceTuple(H, p->allDataMembers(), _useWstring); H << sp; H << nl << "/**"; @@ -8751,7 +8761,10 @@ void Slice::Gen::Cpp11ObjectVisitor::emitOneShotConstructor(const ClassDefPtr& p) { DataMemberList allDataMembers = p->allDataMembers(); - string scope = fixKwd(p->scope()); + // + // Use empty scope to get full qualified names in types used with future declarations. + // + string scope = ""; if(!allDataMembers.empty()) { vector<string> allParamDecls; diff --git a/cpp/src/slice2swift/SwiftUtil.cpp b/cpp/src/slice2swift/SwiftUtil.cpp index 626b43847bc..c2d554a836b 100644 --- a/cpp/src/slice2swift/SwiftUtil.cpp +++ b/cpp/src/slice2swift/SwiftUtil.cpp @@ -1579,10 +1579,27 @@ SwiftGenerator::writeMembers(IceUtilInternal::Output& out, { DataMemberPtr member = *q; TypePtr type = member->type(); - string defaultValue = member->defaultValue(); + const string defaultValue = member->defaultValue(); + + const string memberName = fixIdent(member->name()); + string memberType = typeToString(type, p, member->getMetaData(), member->optional(), typeCtx); + + // + // If the member type is equal to the member name, create a local type alias + // to avoid ambiguity. + // + string alias; + if(!protocol && memberName == memberType && (StructPtr::dynamicCast(type) || + SequencePtr::dynamicCast(type) || + DictionaryPtr::dynamicCast(type))) + { + ModulePtr m = getTopLevelModule(type); + alias = m->name() + "_" + memberType; + out << nl << "typealias " << alias << " = " << memberType; + } + writeMemberDoc(out, member); - out << nl << access << "var " << fixIdent(member->name()) << ": " - << typeToString(type, p, member->getMetaData(), member->optional(), typeCtx); + out << nl << access << "var " << memberName << ": " << memberType; if(protocol) { out << " { get set }"; @@ -1590,8 +1607,15 @@ SwiftGenerator::writeMembers(IceUtilInternal::Output& out, else { out << " = "; - writeConstantValue(out, type, member->defaultValueType(), defaultValue, p->getMetaData(), swiftModule, - member->optional()); + if(alias.empty()) + { + writeConstantValue(out, type, member->defaultValueType(), defaultValue, p->getMetaData(), swiftModule, + member->optional()); + } + else + { + out << alias << "()"; + } } } } @@ -1728,7 +1752,26 @@ SwiftGenerator::writeMarshalUnmarshalCode(Output &out, { args += ", value: "; } - args += getUnqualified(getAbsolute(type), swiftModule) + ".self"; + string memberType = getUnqualified(getAbsolute(type), swiftModule); + string memberName; + const string memberPrefix = "self."; + if(param.find(memberPrefix) == 0) + { + memberName = param.substr(memberPrefix.size()); + } + + string alias; + // + // If the member type is equal to the member name, create a local type alias + // to avoid ambiguity. + // + if(memberType == memberName) + { + ModulePtr m = getTopLevelModule(type); + alias = m->name() + "_" + memberType; + out << nl << "typealias " << alias << " = " << memberType; + } + args += (alias.empty() ? memberType : alias) + ".self"; out << nl << "try " << stream << ".read(" << args << ") { " << param << " = $0 " << "}"; } } diff --git a/cpp/test/Ice/scope/AllTests.cpp b/cpp/test/Ice/scope/AllTests.cpp index eed8c7e343b..d14ffaa1453 100644 --- a/cpp/test/Ice/scope/AllTests.cpp +++ b/cpp/test/Ice/scope/AllTests.cpp @@ -58,6 +58,24 @@ public: called(); } + void opE1(Test::E1 e) + { + test(e == Test::ICE_ENUM(E1, v1)); + called(); + } + + void opS1(const Test::S1& s) + { + test(s.s == "S1"); + called(); + } + + void opC1(const Test::C1Ptr& c) + { + test(c->s == "C1"); + called(); + } + void error(const Ice::Exception&) { test(false); @@ -385,6 +403,17 @@ allTests(Test::TestHelper* helper) Test::CMap cmap3 = i->opCMap(cmap1, cmap2); test(cmap2["a"]->s == c1->s); test(cmap3["a"]->s == c1->s); + + Test::E1 e = i->opE1(Test::ICE_ENUM(E1, v1)); + test(e == Test::ICE_ENUM(E1, v1)); + + Test::S1 s; + s.s = "S1"; + s = i->opS1(s); + test(s.s == "S1"); + + Test::C1Ptr c = i->opC1(ICE_MAKE_SHARED(Test::C1, "C1")); + test(c->s == "C1"); } #ifdef ICE_CPP11_MAPPING @@ -441,6 +470,23 @@ allTests(Test::TestHelper* helper) test(Ice::targetEqualTo(result.returnValue["a"], c1)); test(Ice::targetEqualTo(result.c2["a"], c1)); } + + { + auto result = i->opE1Async(Test::E1::v1).get(); + test(result == Test::E1::v1); + } + + { + Test::S1 s; + s.s = "S1"; + s = i->opS1Async(s).get(); + test(s.s == "S1"); + } + + { + auto result = i->opC1Async(ICE_MAKE_SHARED(Test::C1, "C1")).get(); + test(result->s == "C1"); + } } // @@ -616,6 +662,83 @@ allTests(Test::TestHelper* helper) test(false); } } + + { + promise<void> p; + auto f = p.get_future(); + auto result = i->opE1Async(Test::E1::v1, + [&p](Test::E1 v) + { + test(v == Test::E1::v1); + p.set_value(); + }, + [&p](exception_ptr e) + { + p.set_exception(e); + }); + + try + { + f.get(); + } + catch(const exception& ex) + { + cerr << ex.what() << endl; + test(false); + } + } + + { + Test::S1 s; + s.s = "S1"; + promise<void> p; + auto f = p.get_future(); + auto result = i->opS1Async(s, + [&p](Test::S1 v) + { + test(v.s == "S1"); + p.set_value(); + }, + [&p](exception_ptr e) + { + p.set_exception(e); + }); + + try + { + f.get(); + } + catch(const exception& ex) + { + cerr << ex.what() << endl; + test(false); + } + } + + { + promise<void> p; + auto f = p.get_future(); + auto result = i->opC1Async(ICE_MAKE_SHARED(Test::C1, "C1"), + [&p](Test::C1Ptr v) + { + test(v->s == "C1"); + p.set_value(); + }, + [&p](exception_ptr e) + { + p.set_exception(e); + }); + + try + { + f.get(); + } + catch(const exception& ex) + { + cerr << ex.what() << endl; + test(false); + } + } } #else // @@ -665,6 +788,17 @@ allTests(Test::TestHelper* helper) Test::CMap cmap2; Test::CMap cmap3 = i->end_opCMap(cmap2, i->begin_opCMap(cmap1)); + + Test::E1 e = i->end_opE1(i->begin_opE1(Test::ICE_ENUM(E1, v1))); + test(e == Test::ICE_ENUM(E1, v1)); + + Test::S1 s; + s.s = "S1"; + s = i->end_opS1(i->begin_opS1(s)); + test(s.s == "S1"); + + Test::C1Ptr c = i->end_opC1(i->begin_opC1(new Test::C1("C1"))); + test(c->s == "C1"); } // // C++ 98 type safe callbacks @@ -715,6 +849,22 @@ allTests(Test::TestHelper* helper) i->begin_opCMap(cmap1, opCMapCB); cb->check(); + Test::Callback_I_opE1Ptr opE1 = + Test::newCallback_I_opE1(cb, &Test::Callback::opE1, &Test::Callback::error); + i->begin_opE1(Test::ICE_ENUM(E1, v1), opE1); + cb->check(); + + Test::S1 s; + s.s = "S1"; + Test::Callback_I_opS1Ptr opS1 = + Test::newCallback_I_opS1(cb, &Test::Callback::opS1, &Test::Callback::error); + i->begin_opS1(s, opS1); + cb->check(); + + Test::Callback_I_opC1Ptr opC1 = + Test::newCallback_I_opC1(cb, &Test::Callback::opC1, &Test::Callback::error); + i->begin_opC1(new Test::C1("C1"), opC1); + cb->check(); } #endif diff --git a/cpp/test/Ice/scope/Client.cpp b/cpp/test/Ice/scope/Client.cpp index 5056556c80b..8f2105c8291 100644 --- a/cpp/test/Ice/scope/Client.cpp +++ b/cpp/test/Ice/scope/Client.cpp @@ -19,7 +19,7 @@ void Client::run(int argc, char** argv) { Ice::CommunicatorHolder communicator = initialize(argc, argv); - cout << "test same Slice type name in different scopes... " << flush; + cout << "test using same type name in different Slice modules... " << flush; void allTests(Test::TestHelper*); allTests(this); cout << "ok" << endl; diff --git a/cpp/test/Ice/scope/Server.cpp b/cpp/test/Ice/scope/Server.cpp index 1e4d4f9f438..5a8d2668da1 100644 --- a/cpp/test/Ice/scope/Server.cpp +++ b/cpp/test/Ice/scope/Server.cpp @@ -27,6 +27,10 @@ public: virtual Test::CSeq opCSeq(ICE_IN(Test::CSeq), Test::CSeq&, const Ice::Current&); virtual Test::CMap opCMap(ICE_IN(Test::CMap), Test::CMap&, const Ice::Current&); + virtual Test::E1 opE1(Test::E1, const Ice::Current&); + virtual Test::S1 opS1(ICE_IN(Test::S1), const Ice::Current&); + virtual Test::C1Ptr opC1(ICE_IN(Test::C1Ptr), const Ice::Current&); + virtual void shutdown(const Ice::Current&); }; @@ -149,6 +153,24 @@ I1::opCMap(ICE_IN(Test::CMap) c1, Test::CMap& c2, const Ice::Current&) return c1; } +Test::E1 +I1::opE1(Test::E1 e1, const Ice::Current&) +{ + return e1; +} + +Test::S1 +I1::opS1(ICE_IN(Test::S1) s1, const Ice::Current&) +{ + return s1; +} + +Test::C1Ptr +I1::opC1(ICE_IN(Test::C1Ptr) c1, const Ice::Current&) +{ + return c1; +} + void I1::shutdown(const Ice::Current& current) { diff --git a/cpp/test/Ice/scope/Test.ice b/cpp/test/Ice/scope/Test.ice index e2197329562..011badcf15b 100644 --- a/cpp/test/Ice/scope/Test.ice +++ b/cpp/test/Ice/scope/Test.ice @@ -22,6 +22,37 @@ module Test dictionary<string, C> CMap; sequence<C> CSeq; + enum E1 + { + v1, + v2, + v3 + } + + struct S1 + { + string s; + } + + class C1 + { + string s; + } + + struct S2 + { + E1 E1; + S1 S1; + C1 C1; + } + + class C2 + { + E1 E1; + S1 S1; + C1 C1; + } + interface I { S opS(S s1, out S s2); @@ -32,6 +63,10 @@ module Test CSeq opCSeq(CSeq s1, out CSeq s2); CMap opCMap(CMap c1, out CMap c2); + E1 opE1(E1 E1); + S1 opS1(S1 S1); + C1 opC1(C1 C1); + void shutdown(); } |