diff options
Diffstat (limited to 'cpp/src')
-rw-r--r-- | cpp/src/slice2swift/Gen.cpp | 61 | ||||
-rw-r--r-- | cpp/src/slice2swift/SwiftUtil.cpp | 106 | ||||
-rw-r--r-- | cpp/src/slice2swift/SwiftUtil.h | 3 |
3 files changed, 94 insertions, 76 deletions
diff --git a/cpp/src/slice2swift/Gen.cpp b/cpp/src/slice2swift/Gen.cpp index d64a6626613..4c86f427b1b 100644 --- a/cpp/src/slice2swift/Gen.cpp +++ b/cpp/src/slice2swift/Gen.cpp @@ -347,7 +347,7 @@ Gen::TypesVisitor::visitExceptionStart(const ExceptionPtr& p) out << sb; out << nl << "public override func type() -> " << getUnqualified("Ice.UserException.Type", swiftModule); out << sb; - out << nl << "return " << name << ".self"; + out << nl << "return " << fixIdent(name) << ".self"; out << eb; out << eb; @@ -363,10 +363,10 @@ Gen::TypesVisitor::visitExceptionStart(const ExceptionPtr& p) } out << sp; - out << nl << "open class " << name << ": "; + out << nl << "open class " << fixIdent(name) << ": "; if(base) { - out << getUnqualified(getAbsolute(base), swiftModule); + out << fixIdent(getUnqualified(getAbsolute(base), swiftModule)); } else if(p->isLocal()) { @@ -426,7 +426,7 @@ Gen::TypesVisitor::visitExceptionStart(const ExceptionPtr& p) out << nl << "open override func _iceWriteImpl(to ostr: " << getUnqualified("Ice.OutputStream", swiftModule) << ")"; out << sb; - out << nl << "ostr.startSlice(typeId: " << name << ".ice_staticId(), compactId: -1, last: " + out << nl << "ostr.startSlice(typeId: " << fixIdent(name) << ".ice_staticId(), compactId: -1, last: " << (!base ? "true" : "false") << ")"; for(DataMemberList::const_iterator i = members.begin(); i != members.end(); ++i) { @@ -602,7 +602,7 @@ Gen::TypesVisitor::visitSequence(const SequencePtr& p) BuiltinPtr builtin = BuiltinPtr::dynamicCast(p->type()); out << sp; - out << nl << "public typealias " << name << " = "; + out << nl << "public typealias " << fixIdent(name) << " = "; if(builtin && builtin->kind() == Builtin::KindByte) { @@ -632,13 +632,14 @@ Gen::TypesVisitor::visitSequence(const SequencePtr& p) out << nl << "public struct " << name << "Helper"; out << sb; - out << nl << "public static func read(from istr: " << istr << ") throws -> " << name; + out << nl << "public static func read(from istr: " << istr << ") throws -> " + << fixIdent(name); out << sb; out << nl << "let sz = try istr.readAndCheckSeqSize(minSize: " << p->type()->minWireSize() << ")"; if(isClassType(type)) { - out << nl << "var v = " << name << "(repeating: nil, count: sz)"; + out << nl << "var v = " << fixIdent(name) << "(repeating: nil, count: sz)"; out << nl << "for i in 0 ..< sz"; out << sb; out << nl << "let p = UnsafeMutablePointer<" << typeToString(p->type(), p) << ">(&v[i])"; @@ -647,7 +648,7 @@ Gen::TypesVisitor::visitSequence(const SequencePtr& p) } else { - out << nl << "var v = " << name << "()"; + out << nl << "var v = " << fixIdent(name) << "()"; out << nl << "v.reserveCapacity(sz)"; out << nl << "for _ in 0 ..< sz"; out << sb; @@ -659,7 +660,8 @@ Gen::TypesVisitor::visitSequence(const SequencePtr& p) out << nl << "return v"; out << eb; - out << nl << "public static func read(from istr: " << istr << ", tag: Swift.Int32) throws -> " << name << "?"; + out << nl << "public static func read(from istr: " << istr << ", tag: Swift.Int32) throws -> " + << fixIdent(name) << "?"; out << sb; out << nl << "guard try istr.readOptional(tag: tag, expectedFormat: " << optionalFormat << ") else"; out << sb; @@ -677,7 +679,7 @@ Gen::TypesVisitor::visitSequence(const SequencePtr& p) out << eb; out << sp; - out << nl << "public static func write(to ostr: " << ostr << ", value v: " << name << ")"; + out << nl << "public static func write(to ostr: " << ostr << ", value v: " << fixIdent(name) << ")"; out << sb; out << nl << "ostr.write(size: v.count)"; out << nl << "for item in v"; @@ -687,7 +689,8 @@ Gen::TypesVisitor::visitSequence(const SequencePtr& p) out << eb; out << sp; - out << nl << "public static func write(to ostr: " << ostr << ", tag: Swift.Int32, value v: "<< name << "?)"; + out << nl << "public static func write(to ostr: " << ostr << ", tag: Swift.Int32, value v: " + << fixIdent(name) << "?)"; out << sb; out << nl << "guard let val = v else"; out << sb; @@ -732,7 +735,7 @@ Gen::TypesVisitor::visitDictionary(const DictionaryPtr& p) const string keyType = typeToString(p->keyType(), p, p->keyMetaData(), false, typeCtx); const string valueType = typeToString(p->valueType(), p, p->valueMetaData(), false, typeCtx); out << sp; - out << nl << "public typealias " << name << " = [" << keyType << ": " << valueType << "]"; + out << nl << "public typealias " << fixIdent(name) << " = [" << keyType << ": " << valueType << "]"; if(p->isLocal()) { @@ -750,10 +753,10 @@ Gen::TypesVisitor::visitDictionary(const DictionaryPtr& p) out << nl << "public struct " << name << "Helper"; out << sb; - out << nl << "public static func read(from istr: " << istr << ") throws -> " << name; + out << nl << "public static func read(from istr: " << istr << ") throws -> " << fixIdent(name); out << sb; out << nl << "let sz = try Swift.Int(istr.readSize())"; - out << nl << "var v = " << name << "()"; + out << nl << "var v = " << fixIdent(name) << "()"; if(isClassType(p->valueType())) { out << nl << "let e = " << getUnqualified("Ice.DictEntryArray", swiftModule) << "<" << keyType << ", " @@ -789,7 +792,8 @@ Gen::TypesVisitor::visitDictionary(const DictionaryPtr& p) out << nl << "return v"; out << eb; - out << nl << "public static func read(from istr: " << istr << ", tag: Swift.Int32) throws -> " << name << "?"; + out << nl << "public static func read(from istr: " << istr << ", tag: Swift.Int32) throws -> " + << fixIdent(name) << "?"; out << sb; out << nl << "guard try istr.readOptional(tag: tag, expectedFormat: " << optionalFormat << ") else"; out << sb; @@ -807,7 +811,7 @@ Gen::TypesVisitor::visitDictionary(const DictionaryPtr& p) out << eb; out << sp; - out << nl << "public static func write(to ostr: " << ostr << ", value v: " << name << ")"; + out << nl << "public static func write(to ostr: " << ostr << ", value v: " << fixIdent(name) << ")"; out << sb; out << nl << "ostr.write(size: v.count)"; out << nl << "for (key, value) in v"; @@ -818,7 +822,8 @@ Gen::TypesVisitor::visitDictionary(const DictionaryPtr& p) out << eb; out << sp; - out << nl << "public static func write(to ostr: " << ostr << ", tag: Swift.Int32, value v: "<< name << "?)"; + out << nl << "public static func write(to ostr: " << ostr << ", tag: Swift.Int32, value v: " + << fixIdent(name) << "?)"; out << sb; out << nl << "guard let val = v else"; out << sb; @@ -849,7 +854,7 @@ void Gen::TypesVisitor::visitEnum(const EnumPtr& p) { const string swiftModule = getSwiftModule(getTopLevelModule(ContainedPtr::dynamicCast(p))); - const string name = getUnqualified(getAbsolute(p), swiftModule); + const string name = fixIdent(getUnqualified(getAbsolute(p), swiftModule)); const EnumeratorList enumerators = p->enumerators(); const string enumType = p->maxValue() <= 0xFF ? "Swift.UInt8" : "Swift.Int32"; const string optionalFormat = getOptionalFormat(p); @@ -1110,7 +1115,7 @@ Gen::ValueVisitor::visitClassDefStart(const ClassDefPtr& p) out << sb; out << nl << "public override func type() -> " << getUnqualified("Ice.Value.Type", swiftModule); out << sb; - out << nl << "return " << name << ".self"; + out << nl << "return " << fixIdent(name) << ".self"; out << eb; out << eb; @@ -1156,10 +1161,10 @@ Gen::ValueVisitor::visitClassDefStart(const ClassDefPtr& p) out << eb; out << sp; - out << nl << "open class " << name << ": "; + out << nl << "open class " << fixIdent(name) << ": "; if(base) { - out << getUnqualified(getAbsolute(base), swiftModule); + out << fixIdent(getUnqualified(getAbsolute(base), swiftModule)); } else { @@ -1225,8 +1230,8 @@ Gen::ValueVisitor::visitClassDefStart(const ClassDefPtr& p) out << nl << "open override func _iceWriteImpl(to ostr: " << getUnqualified("Ice.OutputStream", swiftModule) << ")"; out << sb; - out << nl << "ostr.startSlice(typeId: " << name << ".ice_staticId(), compactId: " << p->compactId() << ", last: " - << (!base ? "true" : "false") << ")"; + 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) { DataMemberPtr member = *i; @@ -1443,7 +1448,7 @@ Gen::ObjectExtVisitor::visitClassDefStart(const ClassDefPtr& p) ids << "]"; out << sp; - out << nl << "public extension " << name; + out << nl << "public extension " << fixIdent(name); out << sb; @@ -1636,7 +1641,7 @@ Gen::LocalObjectVisitor::visitClassDefStart(const ClassDefPtr& p) // Local interfaces and local classes map to Swift protocol // out << sp; - out << nl << "public protocol " << name << ":"; + out << nl << "public protocol " << fixIdent(name) << ":"; if(bases.empty()) { out << " Swift.AnyObject"; @@ -1645,7 +1650,7 @@ Gen::LocalObjectVisitor::visitClassDefStart(const ClassDefPtr& p) { for(ClassList::const_iterator i = bases.begin(); i != bases.end();) { - out << " " << getUnqualified(getAbsolute(*i), swiftModule); + out << " " << fixIdent(getUnqualified(getAbsolute(*i), swiftModule)); if(++i != bases.end()) { out << ","; @@ -1684,7 +1689,7 @@ Gen::LocalObjectVisitor::visitOperation(const OperationPtr& p) { s << "_ "; } - s << fixIdent(param->name()) << ": " + s << param->name() << ": " << typeToString(type, p, param->getMetaData(), param->optional(), typeCtx); out << s.str(); } @@ -1718,7 +1723,7 @@ Gen::LocalObjectVisitor::visitOperation(const OperationPtr& p) ParamDeclPtr param = *i; if(param->name() == "returnValue") { - returnValueS = "_returnValue"; + returnValueS = "returnValue_"; break; } } diff --git a/cpp/src/slice2swift/SwiftUtil.cpp b/cpp/src/slice2swift/SwiftUtil.cpp index a60155c8c21..96f21495c05 100644 --- a/cpp/src/slice2swift/SwiftUtil.cpp +++ b/cpp/src/slice2swift/SwiftUtil.cpp @@ -25,11 +25,15 @@ lookupKwd(const string& name) // static const string keywordList[] = { - "Any", "as", "associatedtype", "break", "case", "catch", "class", "continue", "default", "defer", "deinit", - "do", "else", "enum", "extension", "fallthrough", "false", "fileprivate", "for", "func", "guard", "if", - "import", "in", "init", "inout", "internal", "is", "let", "nil", "open", "operator", "private", "protocol", - "public", "repeat", "rethrows", "return", "self", "Self", "static", "struct", "subscript", "super", "Swift", - "switch", "throw", "throws", "true", "try", "typealias", "var", "where", "while" + "Any", "as", "associatedtype", "associativity", "break", + "case", "catch", "class", "continue", "convenience", "default", "defer", "deinit", + "didSet", "do", "dynamic", "else", "enum", "extension", "fallthrough", "false", + "fileprivate", "final", "for", "func", "get", "guard", "if", "import", "in", "indirect", + "infix", "init", "inout", "internal", "is", "lazy", "left", "let", "mutating", "nil", + "none", "nonmutating", "open", "operator", "optional", "override", "postfix", "precedence", + "prefix", "private", "protocol", "public", "repeat", "required", "rethrows", "return", + "right", "self", "set", "static", "struct", "subscript", "super", "switch", "throw", "throws", + "true", "try", "Type", "typealias", "unowned", "var", "weak", "where", "while", "willSet" }; bool found = binary_search(&keywordList[0], &keywordList[sizeof(keywordList) / sizeof(*keywordList)], @@ -428,7 +432,7 @@ SwiftGenerator::typeToString(const TypePtr& type, { if(cl->isInterface() && !cl->isLocal()) { - t = getUnqualified(builtinTable[Builtin::KindValue], currentModule); + t = fixIdent(getUnqualified(builtinTable[Builtin::KindValue], currentModule)); } else { @@ -440,7 +444,7 @@ SwiftGenerator::typeToString(const TypePtr& type, { t = "@escaping "; } - t += getUnqualified(getAbsoluteImpl(cl), currentModule); + t += fixIdent(getUnqualified(getAbsoluteImpl(cl), currentModule)); } } else if(prx) @@ -457,7 +461,7 @@ SwiftGenerator::typeToString(const TypePtr& type, } else if(cont) { - t = getUnqualified(getAbsoluteImpl(cont), currentModule); + t = fixIdent(getUnqualified(getAbsoluteImpl(cont), currentModule)); } if(!nonnull && (optional || isNullableType(type))) @@ -1214,19 +1218,18 @@ SwiftGenerator::MetaDataVisitor::visitModuleStart(const ModulePtr& p) } string -SwiftGenerator::operationReturnTypeLabel(const OperationPtr& op) +SwiftGenerator::paramLabel(const string& param, const ParamDeclList& params) { - string returnValueS = "returnValue"; - ParamDeclList outParams = op->outParameters(); - for(ParamDeclList::iterator q = outParams.begin(); q != outParams.end(); ++q) + string s = param; + for(ParamDeclList::const_iterator q = params.begin(); q != params.end(); ++q) { - if((*q)->name() == "returnValue") + if((*q)->name() == param) { - returnValueS = "_returnValue"; + s += "_"; break; } } - return returnValueS; + return s; } bool @@ -1246,17 +1249,17 @@ SwiftGenerator::operationReturnType(const OperationPtr& op) os << "("; } + ParamDeclList outParams = op->outParameters(); TypePtr returnType = op->returnType(); if(returnType) { if(returnIsTuple) { - os << operationReturnTypeLabel(op) << ": "; + os << paramLabel("returnValue", outParams) << ": "; } os << typeToString(returnType, op, op->getMetaData(), op->returnIsOptional()); } - ParamDeclList outParams = op->outParameters(); for(ParamDeclList::const_iterator q = outParams.begin(); q != outParams.end(); ++q) { if(returnType || q != outParams.begin()) @@ -1295,7 +1298,7 @@ SwiftGenerator::operationReturnDeclaration(const OperationPtr& op) if(returnType) { - os << operationReturnTypeLabel(op); + os << ("iceP_" + paramLabel("returnValue", outParams)); } for(ParamDeclList::const_iterator q = outParams.begin(); q != outParams.end(); ++q) @@ -1305,7 +1308,7 @@ SwiftGenerator::operationReturnDeclaration(const OperationPtr& op) os << ", "; } - os << (*q)->name(); + os << ("iceP_" + (*q)->name()); } if(returnIsTuple) @@ -1337,7 +1340,7 @@ SwiftGenerator::operationInParamsDeclaration(const OperationPtr& op) os << ", "; } - os << "iceP_" + (*q)->name(); + os << ("iceP_" + (*q)->name()); } if(isTuple) { @@ -1384,7 +1387,6 @@ SwiftGenerator::getAllInParams(const OperationPtr& op) { ParamInfo info; info.name = (*p)->name(); - info.fixedName = fixIdent((*p)->name()); info.type = (*p)->type(); info.typeStr = typeToString(info.type, op, (*p)->getMetaData(), (*p)->optional()); info.optional = (*p)->optional(); @@ -1435,7 +1437,6 @@ SwiftGenerator::getAllOutParams(const OperationPtr& op) { ParamInfo info; info.name = (*p)->name(); - info.fixedName = fixIdent((*p)->name()); info.type = (*p)->type(); info.typeStr = typeToString(info.type, op, (*p)->getMetaData(), (*p)->optional()); info.optional = (*p)->optional(); @@ -1447,8 +1448,7 @@ SwiftGenerator::getAllOutParams(const OperationPtr& op) if(op->returnType()) { ParamInfo info; - info.name = operationReturnTypeLabel(op); - info.fixedName = info.name; + info.name = paramLabel("returnValue", params); info.type = op->returnType(); info.typeStr = typeToString(info.type, op, op->getMetaData(), op->returnIsOptional()); info.optional = op->returnIsOptional(); @@ -1505,12 +1505,12 @@ SwiftGenerator::writeMarshalInParams(::IceUtilInternal::Output& out, const Opera for(ParamInfoList::const_iterator q = requiredInParams.begin(); q != requiredInParams.end(); ++q) { - writeMarshalUnmarshalCode(out, q->type, op, q->fixedName, true); + writeMarshalUnmarshalCode(out, q->type, op, "iceP_" + q->name, true); } for(ParamInfoList::const_iterator q = optionalInParams.begin(); q != optionalInParams.end(); ++q) { - writeMarshalUnmarshalCode(out, q->type, op, q->fixedName, true, q->tag); + writeMarshalUnmarshalCode(out, q->type, op, "iceP_" + q->name, true, q->tag); } if(op->sendsClasses(false)) @@ -1537,12 +1537,12 @@ SwiftGenerator::writeMarshalOutParams(::IceUtilInternal::Output& out, const Oper for(ParamInfoList::const_iterator q = requiredOutParams.begin(); q != requiredOutParams.end(); ++q) { - writeMarshalUnmarshalCode(out, q->type, op, q->fixedName, true); + writeMarshalUnmarshalCode(out, q->type, op, "iceP_" + q->name, true); } for(ParamInfoList::const_iterator q = optionalOutParams.begin(); q != optionalOutParams.end(); ++q) { - writeMarshalUnmarshalCode(out, q->type, op, q->fixedName, true, q->tag); + writeMarshalUnmarshalCode(out, q->type, op, "iceP_" + q->name, true, q->tag); } if(op->returnsClasses(false)) @@ -1575,12 +1575,12 @@ SwiftGenerator::writeUnmarshalOutParams(::IceUtilInternal::Output& out, const Op string param; if(isClassType(q->type)) { - out << nl << "var " << q->fixedName << ": " << q->typeStr; - param = q->fixedName; + out << nl << "var iceP_" << q->name << ": " << q->typeStr; + param = "iceP_" + q->name; } else { - param = "let " + q->fixedName + ": " + q->typeStr; + param = "let iceP_" + q->name + ": " + q->typeStr; } writeMarshalUnmarshalCode(out, q->type, op, param, false); } @@ -1590,12 +1590,12 @@ SwiftGenerator::writeUnmarshalOutParams(::IceUtilInternal::Output& out, const Op string param; if(isClassType(q->type)) { - out << nl << "var " << q->fixedName << ": " << q->typeStr; - param = q->fixedName; + out << nl << "var iceP_" << q->name << ": " << q->typeStr; + param = "iceP_" + q->name; } else { - param = "let " + q->fixedName + ": " + q->typeStr; + param = "let iceP_" + q->name + ": " + q->typeStr; } writeMarshalUnmarshalCode(out, q->type, op, param, false, q->tag); } @@ -1613,14 +1613,14 @@ SwiftGenerator::writeUnmarshalOutParams(::IceUtilInternal::Output& out, const Op if(returnType) { - out << operationReturnTypeLabel(op); + out << ("iceP_" + paramLabel("returnValue", op->outParameters())); } for(ParamInfoList::const_iterator q = allOutParams.begin(); q != allOutParams.end(); ++q) { if(q->param) { - out << q->fixedName; + out << ("iceP_" + q->name); } } @@ -1653,12 +1653,12 @@ SwiftGenerator::writeUnmarshalInParams(::IceUtilInternal::Output& out, const Ope string param; if(isClassType(q->type)) { - out << nl << "var " << q->fixedName << ": " << q->typeStr; - param = q->fixedName; + out << nl << "var iceP_" << q->name << ": " << q->typeStr; + param = "iceP_" + q->name; } else { - param = "let " + q->fixedName + ": " + q->typeStr; + param = "let iceP_" + q->name + ": " + q->typeStr; } writeMarshalUnmarshalCode(out, q->type, op, param, false); } @@ -1669,12 +1669,12 @@ SwiftGenerator::writeUnmarshalInParams(::IceUtilInternal::Output& out, const Ope string param; if(isClassType(q->type)) { - out << nl << "var " << q->fixedName << ": " << q->typeStr; - param = q->fixedName; + out << nl << "var iceP_" << q->name << ": " << q->typeStr; + param = "iceP_" + q->name; } else { - param = "let " + q->fixedName + ": " + q->typeStr; + param = "let iceP_" + q->name + ": " + q->typeStr; } writeMarshalUnmarshalCode(out, q->type, op, param, false, q->tag); } @@ -1692,7 +1692,7 @@ SwiftGenerator::writeUnmarshalInParams(::IceUtilInternal::Output& out, const Ope for(ParamInfoList::const_iterator q = allInParams.begin(); q != allInParams.end(); ++q) { - out << q->fixedName; + out << ("iceP_" + q->name); } if(allInParams.size() > 1) @@ -1745,7 +1745,14 @@ SwiftGenerator::writeProxyOperation(::IceUtilInternal::Output& out, const Operat out << spar; for(ParamInfoList::const_iterator q = allInParams.begin(); q != allInParams.end(); ++q) { - out << ((allInParams.size() == 1 ? "_ " : "") + q->name + ": " + q->typeStr); + if(allInParams.size() == 1) + { + out << ("_ iceP_" + q->name + ": " + q->typeStr); + } + else + { + out << (q->name + " iceP_" + q->name + ": " + q->typeStr); + } } out << ("context: " + getUnqualified("Ice.Context", swiftModule) + "? = nil"); @@ -1823,7 +1830,14 @@ SwiftGenerator::writeProxyAsyncOperation(::IceUtilInternal::Output& out, const O out << spar; for(ParamInfoList::const_iterator q = allInParams.begin(); q != allInParams.end(); ++q) { - out << ((allInParams.size() == 1 ? "_ " : "") + q->name + ": " + q->typeStr); + if(allInParams.size() == 1) + { + out << ("_ iceP_" + q->name + ": " + q->typeStr); + } + else + { + out << (q->name + " iceP_" + q->name + ": " + q->typeStr); + } } out << "context: " + getUnqualified("Ice.Context", swiftModule) + "? = nil"; out << "sent: ((Swift.Bool) -> Swift.Void)? = nil"; @@ -1932,7 +1946,7 @@ SwiftGenerator::writeDispatchOperation(::IceUtilInternal::Output& out, const Ope { out << "let " << operationReturnDeclaration(op) << " = "; } - out << "try " << fixIdent(opName); + out << "try self." << fixIdent(opName); out << spar; for(ParamInfoList::const_iterator q = allInParams.begin(); q != allInParams.end(); ++q) diff --git a/cpp/src/slice2swift/SwiftUtil.h b/cpp/src/slice2swift/SwiftUtil.h index 0c26526ba19..edf29dbd625 100644 --- a/cpp/src/slice2swift/SwiftUtil.h +++ b/cpp/src/slice2swift/SwiftUtil.h @@ -28,7 +28,6 @@ StringList splitScopedName(const std::string&); struct ParamInfo { std::string name; - std::string fixedName; TypePtr type; std::string typeStr; bool optional; @@ -48,9 +47,9 @@ public: protected: + std::string paramLabel(const std::string&, const ParamDeclList&); std::string operationReturnType(const OperationPtr&); bool operationReturnIsTuple(const OperationPtr&); - std::string operationReturnTypeLabel(const OperationPtr&); std::string operationReturnDeclaration(const OperationPtr&); std::string operationInParamsDeclaration(const OperationPtr&); |