diff options
Diffstat (limited to 'cpp/src')
-rw-r--r-- | cpp/src/Ice/ReferenceFactory.cpp | 8 | ||||
-rw-r--r-- | cpp/src/Slice/CPlusPlusUtil.cpp | 2 | ||||
-rw-r--r-- | cpp/src/Slice/JavaUtil.cpp | 99 | ||||
-rw-r--r-- | cpp/src/slice2java/Gen.cpp | 103 |
4 files changed, 181 insertions, 31 deletions
diff --git a/cpp/src/Ice/ReferenceFactory.cpp b/cpp/src/Ice/ReferenceFactory.cpp index 211561d5669..47de304261f 100644 --- a/cpp/src/Ice/ReferenceFactory.cpp +++ b/cpp/src/Ice/ReferenceFactory.cpp @@ -298,12 +298,12 @@ IceInternal::ReferenceFactory::create(const string& str) { end = beg; - while (end < s.length() && s[end] == ':') + while(end < s.length() && s[end] == ':') { beg = end + 1; end = s.find(':', beg); - if (end == string::npos) + if(end == string::npos) { end = s.length(); } @@ -316,13 +316,13 @@ IceInternal::ReferenceFactory::create(const string& str) else if(s[beg] == '@') { beg = str.find_first_not_of(delim, beg + 1); - if (beg == string::npos) + if(beg == string::npos) { beg = end + 1; } end = str.find_first_of(delim, beg); - if (end == string::npos) + if(end == string::npos) { end = str.length(); } diff --git a/cpp/src/Slice/CPlusPlusUtil.cpp b/cpp/src/Slice/CPlusPlusUtil.cpp index f4838f81b35..5d1c95ec4a9 100644 --- a/cpp/src/Slice/CPlusPlusUtil.cpp +++ b/cpp/src/Slice/CPlusPlusUtil.cpp @@ -307,7 +307,7 @@ splitScopedName(const string& scoped) if(pos != scoped.size()) { string::size_type endpos = scoped.find("::", pos); - if (endpos != string::npos) + if(endpos != string::npos) { ids.push_back(scoped.substr(pos, endpos - pos)); } diff --git a/cpp/src/Slice/JavaUtil.cpp b/cpp/src/Slice/JavaUtil.cpp index aa1ca23274b..5a6ac829e64 100644 --- a/cpp/src/Slice/JavaUtil.cpp +++ b/cpp/src/Slice/JavaUtil.cpp @@ -148,8 +148,8 @@ Slice::JavaGenerator::output() const return *_out; } -string -Slice::JavaGenerator::fixKwd(const string& name) const +static string +lookupKwd(const string& name) { // // Keyword list. *Must* be kept in alphabetical order. @@ -159,10 +159,10 @@ Slice::JavaGenerator::fixKwd(const string& name) const "abstract", "assert", "boolean", "break", "byte", "case", "catch", "char", "class", "clone", "const", "continue", "default", "do", "double", "else", "equals", "extends", "false", "final", "finalize", - "finally", "float", "for", "getClass", "goto", "hashCode", "if", - "implements", "import", "instanceof", "int", "interface", "long", - "native", "new", "notify", "notifyAll", "null", "package", "private", - "protected", "public", "return", "short", "static", "super", "switch", + "finally", "float", "for", "getClass", "goto", "hashCode", "if", + "implements", "import", "instanceof", "int", "interface", "long", + "native", "new", "notify", "notifyAll", "null", "package", "private", + "protected", "public", "return", "short", "static", "strictfp", "super", "switch", "synchronized", "this", "throw", "throws", "toString", "transient", "true", "try", "void", "volatile", "wait", "while" }; @@ -171,6 +171,69 @@ Slice::JavaGenerator::fixKwd(const string& name) const name); return found ? "_" + name : name; } +// +// +// Split a scoped name into its components and return the components as a list of (unscoped) identifiers. +// +static StringList +splitScopedName(const string& scoped) +{ + assert(scoped[0] == ':'); + StringList ids; + string::size_type next = 0; + string::size_type pos; + while((pos = scoped.find("::", next)) != string::npos) + { + pos += 2; + if(pos != scoped.size()) + { + string::size_type endpos = scoped.find("::", pos); + if(endpos != string::npos) + { + ids.push_back(scoped.substr(pos, endpos - pos)); + } + } + next = pos; + } + if(next != scoped.size()) + { + ids.push_back(scoped.substr(next)); + } + else + { + ids.push_back(""); + } + + return ids; +} + +// +// If the passed name is a scoped name, return the identical scoped name, +// but with all components that are Java keywords replaced by +// their "_"-prefixed version; otherwise, if the passed name is +// not scoped, but a Java keyword, return the "_"-prefixed name; +// otherwise, return the name unchanged. +// +string +Slice::JavaGenerator::fixKwd(const string& name) const +{ + if(name.empty()) + { + return name; + } + if(name[0] != ':') + { + return lookupKwd(name); + } + StringList ids = splitScopedName(name); + transform(ids.begin(), ids.end(), ids.begin(), ptr_fun(lookupKwd)); + stringstream result; + for(StringList::const_iterator i = ids.begin(); i != ids.end(); ++i) + { + result << "::" + *i; + } + return result.str(); +} string Slice::JavaGenerator::getAbsolute(const string& scoped, @@ -180,8 +243,9 @@ Slice::JavaGenerator::getAbsolute(const string& scoped, { string result; string::size_type start = 0; + string fscope = fixKwd(scope); - if(!scope.empty()) + if(!fscope.empty()) { // // Only remove the scope if the resulting symbol is unscoped. @@ -190,19 +254,14 @@ Slice::JavaGenerator::getAbsolute(const string& scoped, // scope=::A, scoped=::A::B, result=B // scope=::A, scoped=::A::B::C, result=::A::B::C // - string::size_type scopeSize = scope.size(); - if(scoped.compare(0, scopeSize, scope) == 0) - { - start = scoped.find(':', scopeSize); - if(start == string::npos) - { - start = scopeSize; - } - else - { - start = 0; - } - } + string::size_type fscopeSize = fscope.size(); + if(scoped.compare(0, fscopeSize, fscope) == 0) + { + if(scoped.size() > fscopeSize && scoped[fscopeSize] == ':') + { + start = fscopeSize; + } + } } // diff --git a/cpp/src/slice2java/Gen.cpp b/cpp/src/slice2java/Gen.cpp index 6e7772ffd72..975719182e5 100644 --- a/cpp/src/slice2java/Gen.cpp +++ b/cpp/src/slice2java/Gen.cpp @@ -1753,16 +1753,107 @@ Slice::Gen::TypesVisitor::visitEnum(const EnumPtr& p) void Slice::Gen::TypesVisitor::visitConstDef(const ConstDefPtr& p) { -#if 0 string name = fixKwd(p->name()); - string absolute = getAbsolute(p->scoped()); + string scoped = p->scoped(); + string absolute = getAbsolute(scoped); + TypePtr type = p->type(); - if(open(absolute + "PrxHolder")) + if(!open(absolute)) + { + return; + } Output& out = output(); - out << "This is a definition for constant " << p->name() << endl; - out << eb; + out << sp << nl << "public interface " << name; + out << sb; + out << nl << typeToString(type, TypeModeIn, scoped) << " value = "; + + BuiltinPtr bp; + EnumPtr ep; + if(bp = BuiltinPtr::dynamicCast(type)) + { + switch(bp->kind()) + { + case Builtin::KindString: + { + out << "\""; // Opening " + + ios_base::fmtflags originalFlags = out.flags(); // Save stream state + streamsize originalWidth = out.width(); + ostream::char_type originalFill = out.fill(); + + const string val = p->value(); + for(string::const_iterator c = val.begin(); c != val.end(); ++c) + { + if(isascii(*c) && isprint(*c)) + { + switch(*c) + { + case '\\': // Take care of \ and " + case '"': + { + out << "\\"; + break; + } + } + out << *c; // Print normally if printable + } + else + { + switch(*c) + { + case '\r': // CR can't be written as a universal char name in Java + { + out << "\\r"; + break; + } + case '\n': // Ditto for NL + { + out << "\\n"; + break; + } + default: + { + unsigned char uc = *c; // char may be signed, so make it positive + out << "\\u"; // Print as universal character name if non-printable + out.flags(ios_base::hex); + out.width(4); + out.fill('0'); + out << static_cast<unsigned>(uc); + break; + } + } + } + } + + out.fill(originalFill); // Restore stream state + out.width(originalWidth); + out.flags(originalFlags); + + out << "\""; // Closing " + break; + } + case Builtin::KindByte: + { + out << p->value() << " - 128"; // Slice byte runs from 0-255, Java byte runs from -128 - 127 + break; + } + case Builtin::KindLong: + { + out << p->value() << "L"; // Need to append "L" modifier for long constants + } + } + + } + else if(ep = EnumPtr::dynamicCast(type)) + { + out << fixKwd(p->value()); + } + else + { + out << p->value(); + } + out << ";" << eb; close(); -#endif } Slice::Gen::HolderVisitor::HolderVisitor(const string& dir, const string& package) : |