diff options
Diffstat (limited to 'cpp/src/slice2cs/CsUtil.cpp')
-rw-r--r-- | cpp/src/slice2cs/CsUtil.cpp | 220 |
1 files changed, 165 insertions, 55 deletions
diff --git a/cpp/src/slice2cs/CsUtil.cpp b/cpp/src/slice2cs/CsUtil.cpp index 22a8f7d21ed..bdf939515e8 100644 --- a/cpp/src/slice2cs/CsUtil.cpp +++ b/cpp/src/slice2cs/CsUtil.cpp @@ -100,13 +100,111 @@ splitScopedName(const string& scoped) } string -Slice::CsGenerator::getUnqualified(const string& type, const string& scope) +Slice::CsGenerator::getPackagePrefix(const ContainedPtr& cont) { - if(type.find(".") != string::npos && type.find(scope) == 0 && type.find(".", scope.size()) == string::npos) + // + // Traverse to the top-level module. + // + ModulePtr m; + ContainedPtr p = cont; + while(true) { - return type.substr(scope.size()); + if(ModulePtr::dynamicCast(p)) + { + m = ModulePtr::dynamicCast(p); + } + + ContainerPtr c = p->container(); + p = ContainedPtr::dynamicCast(c); // This cast fails for Unit. + if(!p) + { + break; + } + } + + assert(m); + + // + // The cs:namespace metadata can be defined as global metadata or applied to a top-level module. + // We check for the metadata at the top-level module first and then fall back to the global scope. + // + static const string prefix = "cs:namespace:"; + + string q; + if(!m->findMetaData(prefix, q)) + { + UnitPtr unit = cont->unit(); + string file = cont->file(); + assert(!file.empty()); + + DefinitionContextPtr dc = unit->findDefinitionContext(file); + assert(dc); + q = dc->findMetaData(prefix); + } + + if(!q.empty()) + { + q = q.substr(prefix.size()); + } + + return q; +} + +string +Slice::CsGenerator::getPackage(const ContainedPtr& cont) +{ + string scope = fixId(cont->scope()); + if(scope.rfind(".") == scope.size() - 1) + { + scope = scope.substr(0, scope.size() - 1); + } + string prefix = getPackagePrefix(cont); + if(!prefix.empty()) + { + if(!scope.empty()) + { + return prefix + "." + scope; + } + else + { + return prefix; + } + } + + return scope; +} + +string +Slice::CsGenerator::getUnqualified(const string& type, const string& scope, bool builtin) +{ + if(type.find(".") != string::npos && type.find(scope) == 0 && type.find(".", scope.size() + 1) == string::npos) + { + return type.substr(scope.size() + 1); + } + else if(builtin) + { + return type.find(".") == string::npos ? type : "global::" + type; + } + else + { + return "global::" + type; + } +} + +string +Slice::CsGenerator::getUnqualified(const ContainedPtr& p, const string& package, const string& prefix, + const string& suffix) +{ + string name = fixId(p->name()); + string contPkg = getPackage(p); + if(contPkg == package || contPkg.empty()) + { + return prefix + name + suffix; + } + else + { + return "global::" + contPkg + "." + prefix + name + suffix; } - return type; } // @@ -294,7 +392,7 @@ Slice::CsGenerator::getStaticId(const TypePtr& type) } string -Slice::CsGenerator::typeToString(const TypePtr& type, const string& scope, bool optional, bool local, +Slice::CsGenerator::typeToString(const TypePtr& type, const string& package, bool optional, bool local, const StringList& metaData) { if(!type) @@ -304,7 +402,7 @@ Slice::CsGenerator::typeToString(const TypePtr& type, const string& scope, bool if(optional) { - return getUnqualified("Ice.Optional", scope) + "<" + typeToString(type, scope, false, local) + ">"; + return getUnqualified("Ice.Optional", package) + "<" + typeToString(type, package, false, local) + ">"; } static const char* builtinTable[] = @@ -319,7 +417,7 @@ Slice::CsGenerator::typeToString(const TypePtr& type, const string& scope, bool "string", "Ice.Object", "Ice.ObjectPrx", - "_System.Object", + "System.Object", "Ice.Value" }; @@ -341,11 +439,11 @@ Slice::CsGenerator::typeToString(const TypePtr& type, const string& scope, bool { if(!local && builtin->kind() == Builtin::KindObject) { - return getUnqualified(builtinTable[Builtin::KindValue], scope); + return getUnqualified(builtinTable[Builtin::KindValue], package, true); } else { - return getUnqualified(builtinTable[builtin->kind()], scope); + return getUnqualified(builtinTable[builtin->kind()], package, true); } } @@ -354,11 +452,11 @@ Slice::CsGenerator::typeToString(const TypePtr& type, const string& scope, bool { if(cl->isInterface() && !local) { - return getUnqualified("Ice.Value", scope); + return getUnqualified("Ice.Value", package); } else { - return getUnqualified(fixId(cl->scoped()), scope); + return getUnqualified(cl, package); } } @@ -368,11 +466,11 @@ Slice::CsGenerator::typeToString(const TypePtr& type, const string& scope, bool ClassDefPtr def = proxy->_class()->definition(); if(def->isInterface() || def->allOperations().size() > 0) { - return getUnqualified(fixId(proxy->_class()->scoped() + "Prx"), scope); + return getUnqualified(proxy->_class(), package, "", "Prx"); } else { - return getUnqualified("Ice.ObjectPrx", scope); + return getUnqualified("Ice.ObjectPrx", package); } } @@ -386,11 +484,12 @@ Slice::CsGenerator::typeToString(const TypePtr& type, const string& scope, bool string type = meta.substr(prefix.size()); if(type == "List" || type == "LinkedList" || type == "Queue" || type == "Stack") { - return "_System.Collections.Generic." + type + "<" + typeToString(seq->type(), scope, optional, local) + ">"; + return "global::System.Collections.Generic." + type + "<" + + typeToString(seq->type(), package, optional, local) + ">"; } else { - return "global::" + type + "<" + typeToString(seq->type(), scope, optional, local) + ">"; + return "global::" + type + "<" + typeToString(seq->type(), package, optional, local) + ">"; } } @@ -401,7 +500,7 @@ Slice::CsGenerator::typeToString(const TypePtr& type, const string& scope, bool return "global::" + type; } - return typeToString(seq->type(), scope, optional, local) + "[]"; + return typeToString(seq->type(), package, optional, local) + "[]"; } DictionaryPtr d = DictionaryPtr::dynamicCast(type); @@ -418,15 +517,15 @@ Slice::CsGenerator::typeToString(const TypePtr& type, const string& scope, bool { typeName = "Dictionary"; } - return "_System.Collections.Generic." + typeName + "<" + - typeToString(d->keyType(), scope, optional, local) + ", " + - typeToString(d->valueType(), scope, optional, local) + ">"; + return "global::System.Collections.Generic." + typeName + "<" + + typeToString(d->keyType(), package, optional, local) + ", " + + typeToString(d->valueType(), package, optional, local) + ">"; } ContainedPtr contained = ContainedPtr::dynamicCast(type); if(contained) { - return getUnqualified(fixId(contained->scoped()), scope); + return getUnqualified(fixId(contained->scoped()), package); } return "???"; @@ -481,11 +580,11 @@ Slice::CsGenerator::taskResultType(const OperationPtr& op, const string& scope, string t = resultType(op, scope, dispatch); if(t.empty()) { - return "_System.Threading.Tasks.Task"; + return "global::System.Threading.Tasks.Task"; } else { - return "_System.Threading.Tasks.Task<" + t + '>'; + return "global::System.Threading.Tasks.Task<" + t + '>'; } } @@ -1301,7 +1400,7 @@ Slice::CsGenerator::writeSequenceMarshalUnmarshalCode(Output& out, } else { - out << nl << "_System.Collections.Generic.IEnumerator<" << typeS + out << nl << "global::System.Collections.Generic.IEnumerator<" << typeS << "> e = " << param << ".GetEnumerator();"; out << nl << "while(e.MoveNext())"; out << sb; @@ -1335,40 +1434,40 @@ Slice::CsGenerator::writeSequenceMarshalUnmarshalCode(Output& out, string patcherName; if(isArray) { - patcherName = "IceInternal.Patcher.arrayReadValue"; - out << "Ice.Value[" << param << "_lenx];"; + patcherName = "global::IceInternal.Patcher.arrayReadValue"; + out << "global::Ice.Value[" << param << "_lenx];"; } else if(isCustom) { - patcherName = "IceInternal.Patcher.customSeqReadValue"; - out << "global::" << genericType << "<Ice.Value>();"; + patcherName = "global::IceInternal.Patcher.customSeqReadValue"; + out << "global::" << genericType << "<global::Ice.Value>();"; } else { - patcherName = "IceInternal.Patcher.listReadValue"; - out << "_System.Collections.Generic." << genericType << "<Ice.Value>(" << param << "_lenx);"; + patcherName = "global::IceInternal.Patcher.listReadValue"; + out << "global::System.Collections.Generic." << genericType << "<Ice.Value>(" << param << "_lenx);"; } out << nl << "for(int ix = 0; ix < " << param << "_lenx; ++ix)"; out << sb; - out << nl << stream << ".readValue(" << patcherName << "<Ice.Value>(" << param << ", ix));"; + out << nl << stream << ".readValue(" << patcherName << "<global::Ice.Value>(" << param << ", ix));"; } else { if(isStack) { - out << nl << "Ice.ObjectPrx[] " << param << "_tmp = new Ice.ObjectPrx[" << param << "_lenx];"; + out << nl << "global::Ice.ObjectPrx[] " << param << "_tmp = new global::Ice.ObjectPrx[" << param << "_lenx];"; } else if(isArray) { - out << "Ice.ObjectPrx[" << param << "_lenx];"; + out << "global::Ice.ObjectPrx[" << param << "_lenx];"; } else if(isCustom) { - out << "global::" << genericType << "<Ice.ObjectPrx>();"; + out << "global::" << genericType << "<global::Ice.ObjectPrx>();"; } else { - out << "_System.Collections.Generic." << genericType << "<Ice.ObjectPrx>("; + out << "global::System.Collections.Generic." << genericType << "<global::Ice.ObjectPrx>("; if(!isLinkedList) { out << param << "_lenx"; @@ -1385,7 +1484,7 @@ Slice::CsGenerator::writeSequenceMarshalUnmarshalCode(Output& out, } else { - out << nl << "Ice.ObjectPrx val = new Ice.ObjectPrxHelperBase();"; + out << nl << "global::Ice.ObjectPrx val = new global::Ice.ObjectPrxHelperBase();"; out << nl << "val = " << stream << ".readProxy();"; out << nl << param << "." << addMethod << "(val);"; } @@ -1394,8 +1493,8 @@ Slice::CsGenerator::writeSequenceMarshalUnmarshalCode(Output& out, if(isStack) { - out << nl << "_System.Array.Reverse(" << param << "_tmp);"; - out << nl << param << " = new _System.Collections.Generic." << genericType << "<" << typeS << ">(" + out << nl << "global::System.Array.Reverse(" << param << "_tmp);"; + out << nl << param << " = new global::System.Collections.Generic." << genericType << "<" << typeS << ">(" << param << "_tmp);"; } } @@ -1486,7 +1585,7 @@ Slice::CsGenerator::writeSequenceMarshalUnmarshalCode(Output& out, // Stacks cannot contain class instances, so there is no need to marshal a // stack bottom-up here. // - out << nl << "_System.Collections.Generic.IEnumerator<" << typeS + out << nl << "global::System.Collections.Generic.IEnumerator<" << typeS << "> e = " << param << ".GetEnumerator();"; out << nl << "while(e.MoveNext())"; out << sb; @@ -1511,18 +1610,18 @@ Slice::CsGenerator::writeSequenceMarshalUnmarshalCode(Output& out, string patcherName; if(isArray) { - patcherName = "IceInternal.Patcher.arrayReadValue"; + patcherName = "global::IceInternal.Patcher.arrayReadValue"; out << toArrayAlloc(typeS + "[]", "szx") << ";"; } else if(isCustom) { - patcherName = "IceInternal.Patcher.customSeqReadValue"; + patcherName = "global::IceInternal.Patcher.customSeqReadValue"; out << "global::" << genericType << "<" << typeS << ">();"; } else { - patcherName = "IceInternal.Patcher.listReadValue"; - out << "_System.Collections.Generic." << genericType << "<" << typeS << ">(szx);"; + patcherName = "global::IceInternal.Patcher.listReadValue"; + out << "global::System.Collections.Generic." << genericType << "<" << typeS << ">(szx);"; } out << nl << "for(int ix = 0; ix < szx; ++ix)"; out << sb; @@ -1558,7 +1657,7 @@ Slice::CsGenerator::writeSequenceMarshalUnmarshalCode(Output& out, } else { - out << nl << "_System.Collections.Generic.IEnumerator<" << typeS + out << nl << "global::System.Collections.Generic.IEnumerator<" << typeS << "> e = " << param << ".GetEnumerator();"; out << nl << "while(e.MoveNext())"; } @@ -1637,7 +1736,7 @@ Slice::CsGenerator::writeSequenceMarshalUnmarshalCode(Output& out, } else { - out << nl << param << " = new _System.Collections.Generic." << genericType << "<" << typeS << ">("; + out << nl << param << " = new global::System.Collections.Generic." << genericType << "<" << typeS << ">("; if(!isLinkedList) { out << "szx"; @@ -1664,8 +1763,8 @@ Slice::CsGenerator::writeSequenceMarshalUnmarshalCode(Output& out, out << eb; if(isStack) { - out << nl << "_System.Array.Reverse(" << param << "_tmp);"; - out << nl << param << " = new _System.Collections.Generic." << genericType << "<" << typeS << ">(" + out << nl << "global::System.Array.Reverse(" << param << "_tmp);"; + out << nl << param << " = new global::System.Collections.Generic." << genericType << "<" << typeS << ">(" << param << "_tmp);"; } out << eb; @@ -1700,7 +1799,7 @@ Slice::CsGenerator::writeSequenceMarshalUnmarshalCode(Output& out, } else { - out << nl << "_System.Collections.Generic.IEnumerator<" << typeS + out << nl << "global::System.Collections.Generic.IEnumerator<" << typeS << "> e = " << param << ".GetEnumerator();"; out << nl << "while(e.MoveNext())"; out << sb; @@ -1736,7 +1835,7 @@ Slice::CsGenerator::writeSequenceMarshalUnmarshalCode(Output& out, } else { - out << nl << param << " = new _System.Collections.Generic." << genericType << "<" << typeS << ">("; + out << nl << param << " = new global::System.Collections.Generic." << genericType << "<" << typeS << ">("; if(!isLinkedList) { out << "szx"; @@ -1758,8 +1857,8 @@ Slice::CsGenerator::writeSequenceMarshalUnmarshalCode(Output& out, out << eb; if(isStack) { - out << nl << "_System.Array.Reverse(" << param << "_tmp);"; - out << nl << param << " = new _System.Collections.Generic." << genericType << "<" << typeS << ">(" + out << nl << "global::System.Array.Reverse(" << param << "_tmp);"; + out << nl << param << " = new global::System.Collections.Generic." << genericType << "<" << typeS << ">(" << param << "_tmp);"; } out << eb; @@ -1803,7 +1902,7 @@ Slice::CsGenerator::writeSequenceMarshalUnmarshalCode(Output& out, } else { - out << nl << "_System.Collections.Generic.IEnumerator<" << typeS + out << nl << "global::System.Collections.Generic.IEnumerator<" << typeS << "> e = " << param << ".GetEnumerator();"; out << nl << "while(e.MoveNext())"; out << sb; @@ -1840,7 +1939,7 @@ Slice::CsGenerator::writeSequenceMarshalUnmarshalCode(Output& out, } else { - out << nl << param << " = new _System.Collections.Generic." << genericType << "<" << typeS << ">();"; + out << nl << param << " = new global::System.Collections.Generic." << genericType << "<" << typeS << ">();"; } out << nl << "for(int ix = 0; ix < szx; ++ix)"; out << sb; @@ -1856,8 +1955,8 @@ Slice::CsGenerator::writeSequenceMarshalUnmarshalCode(Output& out, out << eb; if(isStack) { - out << nl << "_System.Array.Reverse(" << param << "_tmp);"; - out << nl << param << " = new _System.Collections.Generic." << genericType << "<" << typeS << ">(" + out << nl << "global::System.Array.Reverse(" << param << "_tmp);"; + out << nl << param << " = new global::System.Collections.Generic." << genericType << "<" << typeS << ">(" << param << "_tmp);"; } out << eb; @@ -2387,7 +2486,9 @@ Slice::CsGenerator::MetaDataVisitor::visitUnitStart(const UnitPtr& p) if(s.find(csPrefix) == 0) { static const string csAttributePrefix = csPrefix + "attribute:"; - if(s.find(csAttributePrefix) != 0 || s.size() == csAttributePrefix.size()) + static const string csNamespacePrefix = csPrefix + "namespace:"; + if(!(s.find(csNamespacePrefix) == 0 && s.size() > csNamespacePrefix.size()) && + !(s.find(csAttributePrefix) == 0 && s.size() > csAttributePrefix.size())) { dc->warning(InvalidMetaData, file, -1, "ignoring invalid global metadata `" + oldS + "'"); continue; @@ -2630,6 +2731,15 @@ Slice::CsGenerator::MetaDataVisitor::validate(const ContainedPtr& cont) continue; } } + else if(ModulePtr::dynamicCast(cont)) + { + static const string csNamespacePrefix = csPrefix + "namespace:"; + if(s.find(csNamespacePrefix) == 0 && s.size() > csNamespacePrefix.size()) + { + newLocalMetaData.push_back(s); + continue; + } + } static const string csAttributePrefix = csPrefix + "attribute:"; static const string csTie = csPrefix + "tie"; |