diff --git a/cpp/src/slice2js/JsUtil.cpp b/cpp/src/slice2js/JsUtil.cpp index 8bdadda8daf..7861710d867 100644 --- a/cpp/src/slice2js/JsUtil.cpp +++ b/cpp/src/slice2js/JsUtil.cpp @@ -93,11 +93,7 @@ lookupKwd(const string& name) "interface", "let", "new", "null", "package", "private", "protected", "public", "return", "static", "super", "switch", "this", "throw", "true", "try", "typeof", "var", "void", "while", "with", "yield"}; - bool found = binary_search( - &keywordList[0], - &keywordList[sizeof(keywordList) / sizeof(*keywordList)], - name, - Slice::CICompare()); + bool found = binary_search(&keywordList[0], &keywordList[sizeof(keywordList) / sizeof(*keywordList)], name); if (found) { return "_" + name; diff --git a/cpp/src/slice2swift/Gen.cpp b/cpp/src/slice2swift/Gen.cpp index 4ca39d81e22..dfb04f22434 100644 --- a/cpp/src/slice2swift/Gen.cpp +++ b/cpp/src/slice2swift/Gen.cpp @@ -525,9 +525,9 @@ Gen::TypesVisitor::visitStructStart(const StructPtr& p) out << nl << "func read() throws -> " << name; out << sb; out << nl << (usesClasses ? "let" : "var") << " v = " << name << "()"; - for (DataMemberList::const_iterator q = members.begin(); q != members.end(); ++q) + for (const auto& member : members) { - writeMarshalUnmarshalCode(out, (*q)->type(), p, "v." + fixIdent((*q)->name()), false); + writeMarshalUnmarshalCode(out, member->type(), p, "v." + fixIdent(member->name()), false); } out << nl << "return v"; out << eb; @@ -566,9 +566,9 @@ Gen::TypesVisitor::visitStructStart(const StructPtr& p) out << nl << "///"; out << nl << "/// - parameter _: `" << name << "` - The value to write to the stream."; out << nl << "func write(_ v: " << name << ")" << sb; - for (DataMemberList::const_iterator q = members.begin(); q != members.end(); ++q) + for (const auto& member : members) { - writeMarshalUnmarshalCode(out, (*q)->type(), p, "v." + fixIdent((*q)->name()), true); + writeMarshalUnmarshalCode(out, member->type(), p, "v." + fixIdent(member->name()), true); } out << eb; @@ -632,7 +632,7 @@ Gen::TypesVisitor::visitSequence(const SequencePtr& p) const string ostr = getUnqualified("Ice.OutputStream", swiftModule); const string istr = getUnqualified("Ice.InputStream", swiftModule); - const string optionalFormat = getUnqualified(getOptionalFormat(p), swiftModule); + const string optionalFormat = getOptionalFormat(p); out << sp; out << nl << "/// Helper class to read and write `" << fixIdent(name) << "` sequence values from"; @@ -774,7 +774,7 @@ Gen::TypesVisitor::visitDictionary(const DictionaryPtr& p) const string ostr = getUnqualified("Ice.OutputStream", swiftModule); const string istr = getUnqualified("Ice.InputStream", swiftModule); - const string optionalFormat = getUnqualified(getOptionalFormat(p), swiftModule); + const string optionalFormat = getOptionalFormat(p); const bool isVariableLength = p->keyType()->isVariableLength() || p->valueType()->isVariableLength(); const size_t minWireSize = p->keyType()->minWireSize() + p->valueType()->minWireSize(); @@ -925,16 +925,16 @@ Gen::TypesVisitor::visitEnum(const EnumPtr& p) out << nl << "public enum " << name << ": " << enumType; out << sb; - for (EnumeratorList::const_iterator en = enumerators.begin(); en != enumerators.end(); ++en) + for (const auto& enumerator : enumerators) { - StringList sl = splitComment((*en)->comment()); - out << nl << "/// " << fixIdent((*en)->name()); + StringList sl = splitComment(enumerator->comment()); + out << nl << "/// " << fixIdent(enumerator->name()); if (!sl.empty()) { out << " "; writeDocLines(out, sl, false); } - out << nl << "case " << fixIdent((*en)->name()) << " = " << (*en)->value(); + out << nl << "case " << fixIdent(enumerator->name()) << " = " << enumerator->value(); } out << nl << "public init()"; @@ -1325,17 +1325,16 @@ Gen::ValueVisitor::visitClassDefStart(const ClassDefPtr& p) << ") throws"; out << sb; out << nl << "_ = try istr.startSlice()"; - for (DataMemberList::const_iterator i = members.begin(); i != members.end(); ++i) + for (const auto& member : members) { - DataMemberPtr member = *i; if (!member->optional()) { writeMarshalUnmarshalCode(out, member->type(), p, "self." + fixIdent(member->name()), false); } } - for (DataMemberList::const_iterator d = optionalMembers.begin(); d != optionalMembers.end(); ++d) + for (const auto& member : optionalMembers) { - writeMarshalUnmarshalCode(out, (*d)->type(), p, "self." + fixIdent((*d)->name()), false, (*d)->tag()); + writeMarshalUnmarshalCode(out, member->type(), p, "self." + fixIdent(member->name()), false, member->tag()); } out << nl << "try istr.endSlice()"; if (base) @@ -1349,18 +1348,17 @@ Gen::ValueVisitor::visitClassDefStart(const ClassDefPtr& p) out << sb; out << nl << "ostr.startSlice(typeId: " << fixIdent(name) << ".ice_staticId(), compactId: " << p->compactId() << ", last: " << (!base ? "true" : "false") << ")"; - for (DataMemberList::const_iterator i = members.begin(); i != members.end(); ++i) + for (const auto& member : members) { - DataMemberPtr member = *i; TypePtr type = member->type(); if (!member->optional()) { writeMarshalUnmarshalCode(out, member->type(), p, "self." + fixIdent(member->name()), true); } } - for (DataMemberList::const_iterator d = optionalMembers.begin(); d != optionalMembers.end(); ++d) + for (const auto& member : optionalMembers) { - writeMarshalUnmarshalCode(out, (*d)->type(), p, "self." + fixIdent((*d)->name()), true, (*d)->tag()); + writeMarshalUnmarshalCode(out, member->type(), p, "self." + fixIdent(member->name()), true, member->tag()); } out << nl << "ostr.endSlice()"; if (base) diff --git a/cpp/src/slice2swift/SwiftUtil.cpp b/cpp/src/slice2swift/SwiftUtil.cpp index fa4f30c3aec..1fa4e9300c1 100644 --- a/cpp/src/slice2swift/SwiftUtil.cpp +++ b/cpp/src/slice2swift/SwiftUtil.cpp @@ -156,27 +156,32 @@ namespace } } -// // Check the given identifier against Swift's list of reserved words. If it matches // a reserved word, then an escaped version is returned with a leading underscore. // +// The provided identifier must be a _Swift_ identifier (Ice.Foo) and not a Slice identifier (Ice::Foo). string Slice::fixIdent(const string& ident) { - if (ident[0] != ':') - { - return lookupKwd(ident); - } - vector ids = splitScopedName(ident); + assert(ident.find(':' == string::npos)); - transform(ids.begin(), ids.end(), ids.begin(), [](const string& id) -> string { return lookupKwd(id); }); + // Swift namespaces are flat, so we don't need to check for multiple periods, there can be at most 1. + string::size_type separator_pos = ident.find('.'); + if (separator_pos != string::npos) + { + // If this identifier is scoped, we break it up and escape each piece separately. + auto scope = ident.substr(0, separator_pos); + auto name = ident.substr(separator_pos + 1); - ostringstream result; - for (vector::const_iterator i = ids.begin(); i != ids.end(); ++i) + ostringstream result; + result << lookupKwd(scope) << "." << lookupKwd(name); + return result.str(); + } + else { - result << "::" + *i; + // If this identifier isn't scoped, we directly escape it. + return lookupKwd(ident); } - return result.str(); } string @@ -2224,9 +2229,10 @@ SwiftGenerator::writeUnmarshalUserException(::IceInternal::Output& out, const Op out << sb; out << nl << "throw ex"; out << eb; - for (ExceptionList::const_iterator q = throws.begin(); q != throws.end(); ++q) + + for (const auto& thrown : throws) { - out << " catch let error as " << getRelativeTypeString(*q, swiftModule) << sb; + out << " catch let error as " << fixIdent(getRelativeTypeString(thrown, swiftModule)) << sb; out << nl << "throw error"; out << eb; } diff --git a/js/test/Slice/macros/Client.ts b/js/test/Slice/macros/Client.ts index 74ea127ff32..52e258c7e4d 100644 --- a/js/test/Slice/macros/Client.ts +++ b/js/test/Slice/macros/Client.ts @@ -13,7 +13,7 @@ export class Client extends TestHelper { const out = this.getWriter(); out.write("testing Slice predefined macros... "); - const d = new Test._Default(); + const d = new Test.Default(); test(d.x == 10); test(d.y == 10); diff --git a/swift/test/Slice/escape/Key.ice b/swift/test/Slice/escape/Key.ice index bb5db65fda8..89bbf38bcec 100644 --- a/swift/test/Slice/escape/Key.ice +++ b/swift/test/Slice/escape/Key.ice @@ -55,14 +55,12 @@ exception as extends return int static; int switch; } -/* TODO: reenable when #1617 is fixed interface friend { guard goto(continue if, guard d, defer inline, switch private, do mutable, break* namespace, func* new, switch* not, do* operator, int or, int protected, int public, int register) throws return, as; } -*/ const int is = 0; const int self = 0;