diff options
author | Mark Spruiell <mes@zeroc.com> | 2010-07-23 16:02:04 -0700 |
---|---|---|
committer | Mark Spruiell <mes@zeroc.com> | 2010-07-23 16:02:04 -0700 |
commit | 2414545a397baa35e90fbc04c807b2b96ed7cc93 (patch) | |
tree | 271b74750e65d3eb64ef39299b14658b420eb6d9 /cpp/src/Slice/PythonUtil.cpp | |
parent | Bug-fix for 4793. As well as some refactoring. (diff) | |
download | ice-2414545a397baa35e90fbc04c807b2b96ed7cc93.tar.bz2 ice-2414545a397baa35e90fbc04c807b2b96ed7cc93.tar.xz ice-2414545a397baa35e90fbc04c807b2b96ed7cc93.zip |
bug 4794 - allow default values to refer to constants
Diffstat (limited to 'cpp/src/Slice/PythonUtil.cpp')
-rwxr-xr-x | cpp/src/Slice/PythonUtil.cpp | 243 |
1 files changed, 126 insertions, 117 deletions
diff --git a/cpp/src/Slice/PythonUtil.cpp b/cpp/src/Slice/PythonUtil.cpp index b479d8c4a44..da80807751e 100755 --- a/cpp/src/Slice/PythonUtil.cpp +++ b/cpp/src/Slice/PythonUtil.cpp @@ -119,9 +119,9 @@ private: void writeType(const TypePtr&); // - // Write a default value for a given type. + // Write an initializer value for a given type. // - void writeDefaultValue(const TypePtr&); + void writeInitializer(const TypePtr&); // // Add a value to a hash code. @@ -154,7 +154,7 @@ private: // // Write a constant value. // - void writeConstantValue(const TypePtr&, const string&); + void writeConstantValue(const TypePtr&, const SyntaxTreeBasePtr&, const string&); // // Write constructor parameters with default values. @@ -1509,11 +1509,10 @@ void Slice::Python::CodeVisitor::visitConst(const ConstPtr& p) { Slice::TypePtr type = p->type(); - string value = p->value(); string name = fixIdent(p->name()); _out << sp << nl << "_M_" << getAbsolute(p) << " = "; - writeConstantValue(type, value); + writeConstantValue(type, p->valueType(), p->value()); } string @@ -1613,7 +1612,7 @@ Slice::Python::CodeVisitor::writeType(const TypePtr& p) } void -Slice::Python::CodeVisitor::writeDefaultValue(const TypePtr& p) +Slice::Python::CodeVisitor::writeInitializer(const TypePtr& p) { BuiltinPtr builtin = BuiltinPtr::dynamicCast(p); if(builtin) @@ -1769,131 +1768,140 @@ Slice::Python::CodeVisitor::writeAssign(const MemberInfo& info) } void -Slice::Python::CodeVisitor::writeConstantValue(const TypePtr& type, const string& value) +Slice::Python::CodeVisitor::writeConstantValue(const TypePtr& type, const SyntaxTreeBasePtr& valueType, + const string& value) { - Slice::BuiltinPtr b = Slice::BuiltinPtr::dynamicCast(type); - Slice::EnumPtr en = Slice::EnumPtr::dynamicCast(type); - if(b) + ConstPtr constant = ConstPtr::dynamicCast(valueType); + if(constant) { - switch(b->kind()) - { - case Slice::Builtin::KindBool: - { - _out << (value == "true" ? "True" : "False"); - break; - } - case Slice::Builtin::KindByte: - case Slice::Builtin::KindShort: - case Slice::Builtin::KindInt: - case Slice::Builtin::KindFloat: - case Slice::Builtin::KindDouble: - case Slice::Builtin::KindLong: - { - _out << value; - break; - } - case Slice::Builtin::KindString: + _out << "_M_" << getAbsolute(constant); + } + else + { + Slice::BuiltinPtr b = Slice::BuiltinPtr::dynamicCast(type); + Slice::EnumPtr en = Slice::EnumPtr::dynamicCast(type); + if(b) { - // - // Expand strings into the basic source character set. We can't use isalpha() and the like - // here because they are sensitive to the current locale. - // - static const string basicSourceChars = "abcdefghijklmnopqrstuvwxyz" - "ABCDEFGHIJKLMNOPQRSTUVWXYZ" - "0123456789" - "_{}[]#()<>%:;.?*+-/^&|~!=, '"; - static const set<char> charSet(basicSourceChars.begin(), basicSourceChars.end()); - - _out << "\""; // Opening " - - for(string::const_iterator c = value.begin(); c != value.end(); ++c) + switch(b->kind()) { - switch(*c) - { - case '"': - { - _out << "\\\""; - break; - } - case '\\': - { - _out << "\\\\"; - break; - } - case '\r': - { - _out << "\\r"; - break; - } - case '\n': - { - _out << "\\n"; - break; - } - case '\t': - { - _out << "\\t"; - break; - } - case '\b': - { - _out << "\\b"; - break; - } - case '\f': - { - _out << "\\f"; - break; - } - default: + case Slice::Builtin::KindBool: + { + _out << (value == "true" ? "True" : "False"); + break; + } + case Slice::Builtin::KindByte: + case Slice::Builtin::KindShort: + case Slice::Builtin::KindInt: + case Slice::Builtin::KindFloat: + case Slice::Builtin::KindDouble: + case Slice::Builtin::KindLong: + { + _out << value; + break; + } + case Slice::Builtin::KindString: + { + // + // Expand strings into the basic source character set. We can't use isalpha() and the like + // here because they are sensitive to the current locale. + // + static const string basicSourceChars = "abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "0123456789" + "_{}[]#()<>%:;.?*+-/^&|~!=, '"; + static const set<char> charSet(basicSourceChars.begin(), basicSourceChars.end()); + + _out << "\""; // Opening " + + for(string::const_iterator c = value.begin(); c != value.end(); ++c) { - if(charSet.find(*c) == charSet.end()) + switch(*c) { - unsigned char uc = *c; // Char may be signed, so make it positive. - stringstream s; - s << "\\"; // Print as octal if not in basic source character set. - s.flags(ios_base::oct); - s.width(3); - s.fill('0'); - s << static_cast<unsigned>(uc); - _out << s.str(); + case '"': + { + _out << "\\\""; + break; } - else + case '\\': { - _out << *c; // Print normally if in basic source character set. + _out << "\\\\"; + break; + } + case '\r': + { + _out << "\\r"; + break; + } + case '\n': + { + _out << "\\n"; + break; + } + case '\t': + { + _out << "\\t"; + break; + } + case '\b': + { + _out << "\\b"; + break; + } + case '\f': + { + _out << "\\f"; + break; + } + default: + { + if(charSet.find(*c) == charSet.end()) + { + unsigned char uc = *c; // Char may be signed, so make it positive. + stringstream s; + s << "\\"; // Print as octal if not in basic source character set. + s.flags(ios_base::oct); + s.width(3); + s.fill('0'); + s << static_cast<unsigned>(uc); + _out << s.str(); + } + else + { + _out << *c; // Print normally if in basic source character set. + } + break; + } } - break; - } } - } - _out << "\""; // Closing " - break; - } - case Slice::Builtin::KindObject: - case Slice::Builtin::KindObjectProxy: - case Slice::Builtin::KindLocalObject: - assert(false); + _out << "\""; // Closing " + break; + } + case Slice::Builtin::KindObject: + case Slice::Builtin::KindObjectProxy: + case Slice::Builtin::KindLocalObject: + assert(false); + } } - } - else if(en) - { - string enumName = getSymbol(en); - string::size_type colon = value.rfind(':'); - string enumerator; - if(colon != string::npos) + else if(en) { - enumerator = fixIdent(value.substr(colon + 1)); + string enumName = getSymbol(en); + string::size_type colon = value.rfind(':'); + string enumerator; + if(colon != string::npos) + { + enumerator = fixIdent(value.substr(colon + 1)); + } + else + { + enumerator = fixIdent(value); + } + _out << enumName << '.' << enumerator; } else { - enumerator = fixIdent(value); + assert(false); // Unknown const type. } - _out << enumName << '.' << enumerator; - } - else - { - assert(false); // Unknown const type. } } @@ -1904,13 +1912,14 @@ Slice::Python::CodeVisitor::writeConstructorParams(const MemberInfoList& members { _out << ", " << p->fixedName << "="; - if(p->dataMember->hasDefaultValue()) + const DataMemberPtr member = p->dataMember; + if(member->defaultValueType()) { - writeConstantValue(p->dataMember->type(), p->dataMember->defaultValue()); + writeConstantValue(member->type(), member->defaultValueType(), member->defaultValue()); } else { - writeDefaultValue(p->dataMember->type()); + writeInitializer(member->type()); } } } |