diff options
author | Mark Spruiell <mes@zeroc.com> | 2005-01-22 00:48:47 +0000 |
---|---|---|
committer | Mark Spruiell <mes@zeroc.com> | 2005-01-22 00:48:47 +0000 |
commit | 3a03614bd3f7042edf01cd9af7bf9e9e849c023a (patch) | |
tree | fd3edcb1b6cccf1f854a260fb162cb2ad03a271c /cpp/src | |
parent | AMD fixes (diff) | |
download | ice-3a03614bd3f7042edf01cd9af7bf9e9e849c023a.tar.bz2 ice-3a03614bd3f7042edf01cd9af7bf9e9e849c023a.tar.xz ice-3a03614bd3f7042edf01cd9af7bf9e9e849c023a.zip |
adding support for stringification
Diffstat (limited to 'cpp/src')
-rw-r--r-- | cpp/src/Slice/PythonUtil.cpp | 600 |
1 files changed, 295 insertions, 305 deletions
diff --git a/cpp/src/Slice/PythonUtil.cpp b/cpp/src/Slice/PythonUtil.cpp index 7db9f2fa3e9..c50ce95ba46 100644 --- a/cpp/src/Slice/PythonUtil.cpp +++ b/cpp/src/Slice/PythonUtil.cpp @@ -322,11 +322,14 @@ Slice::Python::CodeVisitor::visitClassDecl(const ClassDeclPtr& p) // Emit forward declarations. // string scoped = p->scoped(); - if(!p->isLocal() && _classHistory.count(scoped) == 0) + if(_classHistory.count(scoped) == 0) { string type = getAbsolute(p, "_t_"); _out << sp << nl << "_M_" << type << " = IcePy.declareClass('" << scoped << "')"; - _out << nl << "_M_" << type << "Prx = IcePy.declareProxy('" << scoped << "')"; + if(!p->isLocal()) + { + _out << nl << "_M_" << type << "Prx = IcePy.declareProxy('" << scoped << "')"; + } _classHistory.insert(scoped); // Avoid redundant declarations. } } @@ -528,6 +531,16 @@ Slice::Python::CodeVisitor::visitClassDefStart(const ClassDefPtr& p) } } } + + // + // __str__ + // + _out << sp << nl << "def __str__(self):"; + _out.inc(); + _out << nl << "return IcePy.stringify(self, _M_" << getAbsolute(p, "_t_") << ")"; + _out.dec(); + _out << sp << nl << "__repr__ = __str__"; + _out.dec(); // @@ -631,179 +644,176 @@ Slice::Python::CodeVisitor::visitClassDefStart(const ClassDefPtr& p) _out << sp << nl << "_M_" << type << "Prx = IcePy.defineProxy('" << scoped << "', " << name << "Prx)"; } + if(_classHistory.count(scoped) == 0 && p->canBeCyclic()) + { + // + // Emit a forward declaration for the class in case a data member refers to this type. + // + _out << sp << nl << "_M_" << type << " = IcePy.declareClass('" << scoped << "')"; + } + + DataMemberList members = p->dataMembers(); + _out << sp << nl << "_M_" << type << " = IcePy.defineClass('" << scoped << "', " << name << ", " + << (p->isAbstract() ? "True" : "False") << ", "; + if(!base) + { + _out << "None"; + } + else + { + _out << "_M_" << getAbsolute(base, "_t_"); + } + _out << ", ("; // - // Emit the type information for a non-local class. + // Interfaces // - if(!p->isLocal()) + int interfaceCount = 0; + for(ClassList::const_iterator q = bases.begin(); q != bases.end(); ++q) { - if(_classHistory.count(scoped) == 0 && p->canBeCyclic()) - { - // - // Emit a forward declaration for the class in case a data member refers to this type. - // - _out << sp << nl << "_M_" << type << " = IcePy.declareClass('" << scoped << "')"; - } - - DataMemberList members = p->dataMembers(); - _out << sp << nl << "_M_" << type << " = IcePy.defineClass('" << scoped << "', " << name << ", " - << (p->isAbstract() ? "True" : "False") << ", "; - if(!base) - { - _out << "None"; - } - else - { - _out << "_M_" << getAbsolute(base, "_t_"); - } - _out << ", ("; - // - // Interfaces - // - int interfaceCount = 0; - for(ClassList::const_iterator q = bases.begin(); q != bases.end(); ++q) - { - if((*q)->isInterface()) - { - if(interfaceCount > 0) - { - _out << ", "; - } - _out << "_M_" << getAbsolute(*q, "_t_"); - ++interfaceCount; - } - } - if(interfaceCount == 1) - { - _out << ','; - } - // - // Members - // - // Data members are represented as a tuple: - // - // ('MemberName', MemberType) - // - // where MemberType is either a primitive type constant (T_INT, etc.) or the id of a constructed type. - // - _out << "), ("; - if(members.size() > 1) - { - _out.inc(); - _out << nl; - } - for(DataMemberList::iterator r = members.begin(); r != members.end(); ++r) - { - if(r != members.begin()) - { - _out << ',' << nl; - } - _out << "('" << fixIdent((*r)->name()) << "', "; - writeType((*r)->type()); - _out << ')'; - } - if(members.size() == 1) - { - _out << ','; - } - else if(members.size() > 1) - { - _out.dec(); - _out << nl; - } - _out << "))"; - _out << nl << name << ".ice_type = _M_" << type; - - // - // Define each operation. The arguments to the IcePy.Operation constructor are: - // - // 'opName', Mode, (InParams), (OutParams), ReturnType, (Exceptions) - // - // where InParams and OutParams are tuples of type descriptions, and Exceptions - // is a tuple of exception type ids. - // - if(!ops.empty()) - { - _out << sp; - } - for(OperationList::iterator s = ops.begin(); s != ops.end(); ++s) - { - ParamDeclList params = (*s)->parameters(); - ParamDeclList::iterator t; - int count; + if((*q)->isInterface()) + { + if(interfaceCount > 0) + { + _out << ", "; + } + _out << "_M_" << getAbsolute(*q, "_t_"); + ++interfaceCount; + } + } + if(interfaceCount == 1) + { + _out << ','; + } + // + // Members + // + // Data members are represented as a tuple: + // + // ('MemberName', MemberType) + // + // where MemberType is either a primitive type constant (T_INT, etc.) or the id of a constructed type. + // + _out << "), ("; + if(members.size() > 1) + { + _out.inc(); + _out << nl; + } + for(DataMemberList::iterator r = members.begin(); r != members.end(); ++r) + { + if(r != members.begin()) + { + _out << ',' << nl; + } + _out << "('" << fixIdent((*r)->name()) << "', "; + writeType((*r)->type()); + _out << ')'; + } + if(members.size() == 1) + { + _out << ','; + } + else if(members.size() > 1) + { + _out.dec(); + _out << nl; + } + _out << "))"; + _out << nl << name << ".ice_type = _M_" << type; - _out << nl << name << "._op_" << (*s)->name() << " = IcePy.Operation('" << (*s)->name() << "', "; - switch((*s)->mode()) - { - case Operation::Normal: - _out << "Ice.OperationMode.Normal"; - break; - case Operation::Nonmutating: - _out << "Ice.OperationMode.Nonmutating"; - break; - case Operation::Idempotent: - _out << "Ice.OperationMode.Idempotent"; - break; - } - _out << ", " << ((p->hasMetaData("amd") || (*s)->hasMetaData("amd")) ? "True" : "False") << ", ("; - for(t = params.begin(), count = 0; t != params.end(); ++t) - { - if(!(*t)->isOutParam()) - { - if(count > 0) - { - _out << ", "; - } - writeType((*t)->type()); - ++count; - } - } - if(count == 1) - { - _out << ','; - } - _out << "), ("; - for(t = params.begin(), count = 0; t != params.end(); ++t) - { - if((*t)->isOutParam()) - { - if(count > 0) - { - _out << ", "; - } - writeType((*t)->type()); - ++count; - } - } - if(count == 1) - { - _out << ','; - } - _out << "), "; - TypePtr returnType = (*s)->returnType(); - if(returnType) - { - writeType(returnType); - } - else - { - _out << "None"; - } - _out << ", ("; - ExceptionList exceptions = (*s)->throws(); - for(ExceptionList::iterator u = exceptions.begin(); u != exceptions.end(); ++u) - { - if(u != exceptions.begin()) - { - _out << ", "; - } - _out << "_M_" << getAbsolute(*u, "_t_"); - } - if(exceptions.size() == 1) - { - _out << ','; - } - _out << "))"; - } + // + // Define each operation. The arguments to the IcePy.Operation constructor are: + // + // 'opName', Mode, (InParams), (OutParams), ReturnType, (Exceptions) + // + // where InParams and OutParams are tuples of type descriptions, and Exceptions + // is a tuple of exception type ids. + // + if(!p->isLocal()) + { + if(!ops.empty()) + { + _out << sp; + } + for(OperationList::iterator s = ops.begin(); s != ops.end(); ++s) + { + ParamDeclList params = (*s)->parameters(); + ParamDeclList::iterator t; + int count; + + _out << nl << name << "._op_" << (*s)->name() << " = IcePy.Operation('" << (*s)->name() << "', "; + switch((*s)->mode()) + { + case Operation::Normal: + _out << "Ice.OperationMode.Normal"; + break; + case Operation::Nonmutating: + _out << "Ice.OperationMode.Nonmutating"; + break; + case Operation::Idempotent: + _out << "Ice.OperationMode.Idempotent"; + break; + } + _out << ", " << ((p->hasMetaData("amd") || (*s)->hasMetaData("amd")) ? "True" : "False") << ", ("; + for(t = params.begin(), count = 0; t != params.end(); ++t) + { + if(!(*t)->isOutParam()) + { + if(count > 0) + { + _out << ", "; + } + writeType((*t)->type()); + ++count; + } + } + if(count == 1) + { + _out << ','; + } + _out << "), ("; + for(t = params.begin(), count = 0; t != params.end(); ++t) + { + if((*t)->isOutParam()) + { + if(count > 0) + { + _out << ", "; + } + writeType((*t)->type()); + ++count; + } + } + if(count == 1) + { + _out << ','; + } + _out << "), "; + TypePtr returnType = (*s)->returnType(); + if(returnType) + { + writeType(returnType); + } + else + { + _out << "None"; + } + _out << ", ("; + ExceptionList exceptions = (*s)->throws(); + for(ExceptionList::iterator u = exceptions.begin(); u != exceptions.end(); ++u) + { + if(u != exceptions.begin()) + { + _out << ", "; + } + _out << "_M_" << getAbsolute(*u, "_t_"); + } + if(exceptions.size() == 1) + { + _out << ','; + } + _out << "))"; + } } registerName(name); @@ -907,75 +917,59 @@ Slice::Python::CodeVisitor::visitExceptionStart(const ExceptionPtr& p) // _out << sp << nl << "def __str__(self):"; _out.inc(); - if(allMembers.empty()) + _out << nl << "return IcePy.stringifyException(self)"; + _out.dec(); + _out << sp << nl << "__repr__ = __str__"; + + _out.dec(); + + // + // Emit the type information. + // + string type = getAbsolute(p, "_t_"); + _out << sp << nl << "_M_" << type << " = IcePy.defineException('" << scoped << "', " << name << ", "; + if(!base) { - _out << nl << "return '" << abs << "'"; + _out << "None"; } else { - _out << nl << "return '" << abs << ":'"; - _out.inc(); - for(MemberInfoList::iterator q = allMembers.begin(); q != allMembers.end(); ++q) - { - _out << " +\\" - << nl << "'\\n" << q->fixedName << ": ' + str(self." << q->fixedName << ')'; - } - _out.dec(); + _out << "_M_" << getAbsolute(base, "_t_"); + } + _out << ", ("; + if(members.size() > 1) + { + _out.inc(); + _out << nl; } - _out.dec(); - - _out.dec(); - // - // Emit the type information for a non-local exception. + // Data members are represented as a tuple: // - if(!p->isLocal()) + // ('MemberName', MemberType) + // + // where MemberType is either a primitive type constant (T_INT, etc.) or the id of a constructed type. + // + for(dmli = members.begin(); dmli != members.end(); ++dmli) { - string type = getAbsolute(p, "_t_"); - _out << sp << nl << "_M_" << type << " = IcePy.defineException('" << scoped << "', " << name << ", "; - if(!base) - { - _out << "None"; - } - else - { - _out << "_M_" << getAbsolute(base, "_t_"); - } - _out << ", ("; - if(members.size() > 1) - { - _out.inc(); - _out << nl; - } - // - // Data members are represented as a tuple: - // - // ('MemberName', MemberType) - // - // where MemberType is either a primitive type constant (T_INT, etc.) or the id of a constructed type. - // - for(dmli = members.begin(); dmli != members.end(); ++dmli) - { - if(dmli != members.begin()) - { - _out << ',' << nl; - } - _out << "(\"" << fixIdent((*dmli)->name()) << "\", "; - writeType((*dmli)->type()); - _out << ')'; - } - if(members.size() == 1) - { - _out << ','; - } - else if(members.size() > 1) - { - _out.dec(); - _out << nl; - } - _out << "))"; - _out << nl << name << ".ice_type = _M_" << type; + if(dmli != members.begin()) + { + _out << ',' << nl; + } + _out << "(\"" << fixIdent((*dmli)->name()) << "\", "; + writeType((*dmli)->type()); + _out << ')'; + } + if(members.size() == 1) + { + _out << ','; + } + else if(members.size() > 1) + { + _out.dec(); + _out << nl; } + _out << "))"; + _out << nl << name << ".ice_type = _M_" << type; registerName(name); @@ -1001,7 +995,6 @@ Slice::Python::CodeVisitor::visitStructStart(const StructPtr& p) } } - _out << sp << nl << "_M_" << abs << " = Ice.createTempClass()"; _out << nl << "class " << name << "(object):"; _out.inc(); @@ -1043,48 +1036,54 @@ Slice::Python::CodeVisitor::visitStructStart(const StructPtr& p) _out << nl << "return True"; _out.dec(); + // + // __str__ + // + _out << sp << nl << "def __str__(self):"; + _out.inc(); + _out << nl << "return IcePy.stringify(self, _M_" << getAbsolute(p, "_t_") << ")"; + _out.dec(); + _out << sp << nl << "__repr__ = __str__"; + _out.dec(); // - // Emit the type information for a non-local struct. + // Emit the type information. // - if(!p->isLocal()) + _out << sp << nl << "_M_" << getAbsolute(p, "_t_") << " = IcePy.defineStruct('" << scoped << "', " + << name << ", ("; + // + // Data members are represented as a tuple: + // + // ('MemberName', MemberType) + // + // where MemberType is either a primitive type constant (T_INT, etc.) or the id of a constructed type. + // + if(memberList.size() > 1) { - _out << sp << nl << "_M_" << getAbsolute(p, "_t_") << " = IcePy.defineStruct('" << scoped << "', " - << name << ", ("; - // - // Data members are represented as a tuple: - // - // ('MemberName', MemberType) - // - // where MemberType is either a primitive type constant (T_INT, etc.) or the id of a constructed type. - // - if(memberList.size() > 1) - { - _out.inc(); - _out << nl; - } - for(r = memberList.begin(); r != memberList.end(); ++r) - { - if(r != memberList.begin()) - { - _out << ',' << nl; - } - _out << "(\"" << r->fixedName << "\", "; - writeType(r->type); - _out << ')'; - } - if(memberList.size() == 1) - { - _out << ','; - } - else if(memberList.size() > 1) - { - _out.dec(); - _out << nl; - } - _out << "))"; + _out.inc(); + _out << nl; } + for(r = memberList.begin(); r != memberList.end(); ++r) + { + if(r != memberList.begin()) + { + _out << ',' << nl; + } + _out << "(\"" << r->fixedName << "\", "; + writeType(r->type); + _out << ')'; + } + if(memberList.size() == 1) + { + _out << ','; + } + else if(memberList.size() > 1) + { + _out.dec(); + _out << nl; + } + _out << "))"; registerName(name); @@ -1095,32 +1094,26 @@ void Slice::Python::CodeVisitor::visitSequence(const SequencePtr& p) { // - // Emit the type information for a non-local sequence. + // Emit the type information. // - if(!p->isLocal()) - { - string scoped = p->scoped(); - _out << sp << nl << "_M_" << getAbsolute(p, "_t_") << " = IcePy.defineSequence('" << scoped << "', "; - writeType(p->type()); - _out << ")"; - } + string scoped = p->scoped(); + _out << sp << nl << "_M_" << getAbsolute(p, "_t_") << " = IcePy.defineSequence('" << scoped << "', "; + writeType(p->type()); + _out << ")"; } void Slice::Python::CodeVisitor::visitDictionary(const DictionaryPtr& p) { // - // Emit the type information for a non-local dictionary. + // Emit the type information. // - if(!p->isLocal()) - { - string scoped = p->scoped(); - _out << sp << nl << "_M_" << getAbsolute(p, "_t_") << " = IcePy.defineDictionary('" << scoped << "', "; - writeType(p->keyType()); - _out << ", "; - writeType(p->valueType()); - _out << ")"; - } + string scoped = p->scoped(); + _out << sp << nl << "_M_" << getAbsolute(p, "_t_") << " = IcePy.defineDictionary('" << scoped << "', "; + writeType(p->keyType()); + _out << ", "; + writeType(p->valueType()); + _out << ")"; } void @@ -1188,27 +1181,24 @@ Slice::Python::CodeVisitor::visitEnum(const EnumPtr& p) } // - // Emit the type information for a non-local enum. + // Emit the type information. // - if(!p->isLocal()) + _out << sp << nl << "_M_" << getAbsolute(p, "_t_") << " = IcePy.defineEnum('" << scoped << "', " << name + << ", ("; + for(q = enums.begin(); q != enums.end(); ++q) { - _out << sp << nl << "_M_" << getAbsolute(p, "_t_") << " = IcePy.defineEnum('" << scoped << "', " << name - << ", ("; - for(EnumeratorList::iterator r = enums.begin(); r != enums.end(); ++r) - { - if(r != enums.begin()) - { - _out << ", "; - } - string fixedEnum = fixIdent((*r)->name()); - _out << name << '.' << fixedEnum; - } - if(enums.size() == 1) - { - _out << ','; - } - _out << "))"; + if(q != enums.begin()) + { + _out << ", "; + } + string fixedEnum = fixIdent((*q)->name()); + _out << name << '.' << fixedEnum; + } + if(enums.size() == 1) + { + _out << ','; } + _out << "))"; registerName(name); } @@ -1451,7 +1441,7 @@ Slice::Python::CodeVisitor::writeType(const TypePtr& p) } case Builtin::KindLocalObject: { - assert(false); + _out << "IcePy._t_LocalObject"; break; } } |