update dev300-m58
[ooovba.git] / codemaker / source / idlmaker / idltype.cxx
blob9fbda9eedef7bcc825225779b3dd0d6e4e699dad
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: idltype.cxx,v $
10 * $Revision: 1.8 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_codemaker.hxx"
34 #include <stdio.h>
35 #include <rtl/alloc.h>
36 #include <rtl/ustring.hxx>
37 #include <rtl/strbuf.hxx>
39 #include "idltype.hxx"
40 #include "idloptions.hxx"
42 using namespace rtl;
44 //*************************************************************************
45 // IdlType
46 //*************************************************************************
47 IdlType::IdlType(TypeReader& typeReader,
48 const OString& typeName,
49 const TypeManager& typeMgr,
50 const TypeDependency& typeDependencies)
51 : m_inheritedMemberCount(0)
52 , m_indentLength(0)
53 , m_typeName(typeName)
54 , m_reader(typeReader)
55 , m_typeMgr((TypeManager&)typeMgr)
56 , m_dependencies(typeDependencies)
58 sal_Int32 i = typeName.lastIndexOf('/');
59 m_name = typeName.copy( i != -1 ? i+1 : 0 );
62 IdlType::~IdlType()
67 sal_Bool IdlType::dump(IdlOptions* pOptions)
68 throw( CannotDumpException )
70 sal_Bool ret = sal_False;
72 OString outPath;
73 if (pOptions->isValid("-O"))
74 outPath = pOptions->getOption("-O");
76 OString tmpFileName;
77 OString hFileName = createFileNameFromType(outPath, m_typeName, ".idl");
79 sal_Bool bFileExists = sal_False;
80 sal_Bool bFileCheck = sal_False;
82 if ( pOptions->isValid("-G") || pOptions->isValid("-Gc") )
84 bFileExists = fileExists( hFileName );
85 ret = sal_True;
88 if ( bFileExists && pOptions->isValid("-Gc") )
90 tmpFileName = createFileNameFromType(outPath, m_typeName, ".tml");
91 bFileCheck = sal_True;
94 if ( !bFileExists || bFileCheck )
96 FileStream hFile;
98 if ( bFileCheck )
99 hFile.open(tmpFileName);
100 else
101 hFile.open(hFileName);
103 if(!hFile.isValid())
105 OString message("cannot open ");
106 message += hFileName + " for writing";
107 throw CannotDumpException(message);
110 ret = dumpHFile(hFile);
112 hFile.close();
113 if (ret && bFileCheck)
115 ret = checkFileContent(hFileName, tmpFileName);
119 return ret;
121 sal_Bool IdlType::dumpDependedTypes(IdlOptions* pOptions)
122 throw( CannotDumpException )
124 sal_Bool ret = sal_True;
126 TypeUsingSet usingSet(m_dependencies.getDependencies(m_typeName));
128 TypeUsingSet::const_iterator iter = usingSet.begin();
129 OString typeName;
130 sal_uInt32 index = 0;
131 while (iter != usingSet.end())
133 typeName = (*iter).m_type;
134 if ((index = typeName.lastIndexOf(']')) > 0)
135 typeName = typeName.copy(index + 1);
137 if (getBaseType(typeName).getLength() == 0)
139 if (!produceType(typeName,
140 m_typeMgr,
141 m_dependencies,
142 pOptions))
144 fprintf(stderr, "%s ERROR: %s\n",
145 pOptions->getProgramName().getStr(),
146 OString("cannot dump Type '" + typeName + "'").getStr());
147 exit(99);
150 ++iter;
153 return ret;
156 OString IdlType::dumpHeaderDefine(FileStream& o, sal_Char* prefix )
158 if (m_typeName.equals("/"))
160 m_typeName = "global";
163 sal_uInt32 length = 3 + m_typeName.getLength() + strlen(prefix);
165 OStringBuffer tmpBuf(length);
167 tmpBuf.append('_');
168 tmpBuf.append(m_typeName);
169 tmpBuf.append('_');
170 tmpBuf.append(prefix);
171 tmpBuf.append('_');
173 OString tmp(tmpBuf.makeStringAndClear().replace('/', '_').toAsciiUpperCase());
175 o << "#ifndef " << tmp << "\n#define " << tmp << "\n";
177 return tmp;
180 void IdlType::dumpDefaultHIncludes(FileStream& o)
184 void IdlType::dumpInclude(FileStream& o, const OString& genTypeName, const OString& typeName, sal_Char* prefix )
186 sal_uInt32 length = 3+ m_typeName.getLength() + strlen(prefix);
188 OStringBuffer tmpBuf(length);
190 tmpBuf.append('_');
191 tmpBuf.append(typeName);
192 tmpBuf.append('_');
193 tmpBuf.append(prefix);
194 tmpBuf.append('_');
196 OString tmp(tmpBuf.makeStringAndClear().replace('/', '_').toAsciiUpperCase());
198 length = 1 + typeName.getLength() + strlen(prefix);
200 tmpBuf.ensureCapacity(length);
201 tmpBuf.append(typeName);
202 tmpBuf.append('.');
203 tmpBuf.append(prefix);
205 o << "#ifndef " << tmp << "\n#include <";
206 tmp = tmpBuf.makeStringAndClear();
208 sal_Int32 nIndex = 0;
211 genTypeName.getToken(0, '/', nIndex);
212 o << "../";
213 } while( nIndex != -1 );
215 // sal_Int32 nSlashes = genTypeName.getTokenCount( '/');
216 // for( sal_Int32 i = 1; i < nSlashes; i++ )
217 // o << "../";
218 o << tmp;
219 o << ">\n#endif\n";
222 void IdlType::dumpDepIncludes(FileStream& o, const OString& typeName, sal_Char* prefix)
224 TypeUsingSet usingSet(m_dependencies.getDependencies(typeName));
226 TypeUsingSet::const_iterator iter = usingSet.begin();
228 OString sPrefix(OString(prefix).toAsciiUpperCase());
229 sal_uInt32 index = 0;
230 sal_uInt32 seqNum = 0;
231 OString relType;
232 while (iter != usingSet.end())
234 index = (*iter).m_type.lastIndexOf(']');
235 seqNum = (index > 0 ? ((index+1) / 2) : 0);
237 relType = (*iter).m_type;
238 if (index > 0)
239 relType = relType.copy(index+1);
242 OString defPrefix("IDL");
244 if (getBaseType(relType).getLength() == 0 &&
245 m_typeName != relType)
247 if (m_typeMgr.getTypeClass(relType) == RT_TYPE_INTERFACE)
249 if (!((*iter).m_use & TYPEUSE_SUPER))
251 o << "\n";
252 dumpNameSpace(o, sal_True, sal_False, relType);
253 o << "\ninterface " << scopedName(m_typeName, relType, sal_True) << ";\n";
254 dumpNameSpace(o, sal_False, sal_False, relType);
255 o << "\n\n";
258 dumpInclude(o, typeName, relType, prefix);
260 else if (relType == "type")
262 o << "module CORBA {\n"
263 << "\tinterface TypeCode;\n"
264 << "};\n\n";
267 if( seqNum != 0 )
269 // write typedef for sequences to support Rational Rose 2000 import
270 OString aST = relType;
271 OString aScope;
272 dumpNameSpace( o, sal_True, sal_False, relType );
273 for( sal_uInt32 i = 0; i < seqNum; i++ )
275 o << "typedef sequence< " << scopedName("", aST) << " > ";
277 if( i == 0 )
279 aST = aST.replace( '/', '_' );
280 aST = aST.replace( ' ', '_' );
282 aST = aST + "_Sequence" ;
283 o << aST << ";\n";
285 dumpNameSpace( o, sal_False, sal_False, relType );
287 ++iter;
291 void IdlType::dumpNameSpace(FileStream& o, sal_Bool bOpen, sal_Bool bFull, const OString& type)
293 OString typeName(type);
294 sal_Bool bOneLine = sal_True;
295 if (typeName.getLength() == 0)
297 typeName = m_typeName;
298 bOneLine = sal_False;
301 if (typeName == "/")
302 return;
304 if (typeName.indexOf( '/' ) == -1 && !bFull)
305 return;
307 if (!bFull)
308 typeName = typeName.copy( 0, typeName.lastIndexOf( '/' ) );
310 if (bOpen)
312 sal_Int32 nIndex = 0;
315 o << "module " << typeName.getToken(0, '/', nIndex);
316 if (bOneLine)
317 o << " { ";
318 else
319 o << "\n{\n";
320 } while( nIndex != -1 );
321 } else
323 sal_Int32 nPos = 0;
326 nPos = typeName.lastIndexOf( '/' );
327 o << "};";
328 if( bOneLine )
329 o << " ";
330 else
331 o << " /* " << typeName.copy( nPos+1 ) << " */\n";
332 if( nPos != -1 )
333 typeName = typeName.copy( 0, nPos );
334 } while( nPos != -1 );
339 sal_uInt32 IdlType::getMemberCount()
341 sal_uInt32 count = m_reader.getMethodCount();
343 sal_uInt32 fieldCount = m_reader.getFieldCount();
344 RTFieldAccess access = RT_ACCESS_INVALID;
345 for (sal_uInt16 i=0; i < fieldCount; i++)
347 access = m_reader.getFieldAccess(i);
349 if (access != RT_ACCESS_CONST && access != RT_ACCESS_INVALID)
350 count++;
352 return count;
355 sal_uInt32 IdlType::checkInheritedMemberCount(const TypeReader* pReader)
357 sal_Bool bSelfCheck = sal_True;
358 if (!pReader)
360 bSelfCheck = sal_False;
361 pReader = &m_reader;
364 sal_uInt32 count = 0;
365 OString superType(pReader->getSuperTypeName());
366 if (superType.getLength() > 0)
368 TypeReader aSuperReader(m_typeMgr.getTypeReader(superType));
369 if ( aSuperReader.isValid() )
371 count = checkInheritedMemberCount(&aSuperReader);
375 if (bSelfCheck)
377 count += pReader->getMethodCount();
378 sal_uInt32 fieldCount = pReader->getFieldCount();
379 RTFieldAccess access = RT_ACCESS_INVALID;
380 for (sal_uInt16 i=0; i < fieldCount; i++)
382 access = pReader->getFieldAccess(i);
384 if (access != RT_ACCESS_CONST && access != RT_ACCESS_INVALID)
386 count++;
391 return count;
394 sal_uInt32 IdlType::getInheritedMemberCount()
396 if (m_inheritedMemberCount == 0)
398 m_inheritedMemberCount = checkInheritedMemberCount(0);
401 return m_inheritedMemberCount;
405 void IdlType::dumpType(FileStream& o, const OString& type )
406 throw( CannotDumpException )
408 OString sType(checkRealBaseType(type, sal_True));
409 sal_uInt32 index = sType.lastIndexOf(']');
410 sal_uInt32 seqNum = (index > 0 ? ((index+1) / 2) : 0);
412 OString relType = (index > 0 ? (sType).copy(index+1) : type);
414 RTTypeClass typeClass = m_typeMgr.getTypeClass(relType);
416 sal_uInt32 i;
418 for (i=0; i < seqNum; i++)
420 //o << "sequence< ";
423 switch (typeClass)
425 case RT_TYPE_INVALID:
427 OString tmp(getBaseType(relType));
428 if (tmp.getLength() > 0)
430 tmp = tmp.replace( ' ', '_' );
431 o << tmp;
432 } else
433 throw CannotDumpException("Unknown type '" + relType + "', incomplete type library.");
435 break;
436 case RT_TYPE_INTERFACE:
437 case RT_TYPE_STRUCT:
438 case RT_TYPE_ENUM:
439 case RT_TYPE_TYPEDEF:
440 case RT_TYPE_EXCEPTION:
441 if( seqNum )
443 OString aST = relType.replace( '/', '_' );
444 aST = aST.replace( ' ', '_' );
445 o << aST;
447 else
448 o << scopedName(m_typeName, relType);
449 break;
452 for (i=0; i < seqNum; i++)
454 //o << " >";
455 // use typedef for sequences to support Rational Rose 2000 import
456 o << "_Sequence";
460 OString IdlType::getBaseType(const OString& type)
462 if (type.equals("long"))
463 return type;
464 if (type.equals("short"))
465 return type;
466 if (type.equals("hyper"))
467 return "long long";
468 if (type.equals("string"))
469 return "string";
470 if (type.equals("boolean"))
471 return type;
472 if (type.equals("char"))
473 return "char";
474 if (type.equals("byte"))
475 return "byte";
476 if (type.equals("any"))
477 return type;
478 if (type.equals("type"))
479 return "CORBA::TypeCode";
480 if (type.equals("float"))
481 return type;
482 if (type.equals("double"))
483 return type;
484 if (type.equals("octet"))
485 return type;
486 if (type.equals("void"))
487 return type;
488 if (type.equals("unsigned long"))
489 return type;
490 if (type.equals("unsigned short"))
491 return type;
492 if (type.equals("unsigned hyper"))
493 return "unsigned long long";
495 return OString();
498 void IdlType::dumpIdlGetType(FileStream& o, const OString& type, sal_Bool bDecl, IdlTypeDecl eDeclFlag)
500 OString sType( checkRealBaseType(type, sal_True) );
501 sal_uInt32 index = sType.lastIndexOf(']');
502 OString relType = (index > 0 ? (sType).copy(index+1) : type);
504 if (eDeclFlag == CPPUTYPEDECL_ONLYINTERFACES)
506 if (m_typeMgr.getTypeClass(relType) == RT_TYPE_INTERFACE)
508 o << indent() << "getIdlType( (";
509 dumpType(o, type);
510 o << "*)0 )";
512 if (bDecl)
513 o << ";\n";
515 } else
517 if (isBaseType(type))
519 return;
520 } else
522 if (eDeclFlag == CPPUTYPEDECL_NOINTERFACES &&
523 m_typeMgr.getTypeClass(relType) == RT_TYPE_INTERFACE)
524 return;
526 // if (m_typeMgr.getTypeClass(type) == RT_TYPE_TYPEDEF)
527 // {
528 // o << indent() << "get_" << type.replace('/', '_') << "_Type()";
529 // } else
530 // {
531 o << indent() << "getIdlType( (";
532 dumpType(o, type);
533 o << "*)0 )";
534 // }
536 if (bDecl)
537 o << ";\n";
541 BASETYPE IdlType::isBaseType(const OString& type)
543 if (type.equals("long"))
544 return BT_LONG;
545 if (type.equals("short"))
546 return BT_SHORT;
547 if (type.equals("hyper"))
548 return BT_HYPER;
549 if (type.equals("string"))
550 return BT_STRING;
551 if (type.equals("boolean"))
552 return BT_BOOLEAN;
553 if (type.equals("char"))
554 return BT_CHAR;
555 if (type.equals("byte"))
556 return BT_BYTE;
557 if (type.equals("any"))
558 return BT_ANY;
559 if (type.equals("float"))
560 return BT_FLOAT;
561 if (type.equals("double"))
562 return BT_DOUBLE;
563 if (type.equals("void"))
564 return BT_VOID;
565 if (type.equals("unsigned long"))
566 return BT_UNSIGNED_LONG;
567 if (type.equals("unsigned short"))
568 return BT_UNSIGNED_SHORT;
569 if (type.equals("unsigned hyper"))
570 return BT_UNSIGNED_HYPER;
572 return BT_INVALID;
575 OString IdlType::checkSpecialIdlType(const OString& type)
577 OString baseType(type);
579 RegistryTypeReaderLoader & rReaderLoader = getRegistryTypeReaderLoader();
581 RegistryKey key;
582 sal_uInt8* pBuffer=NULL;
583 RTTypeClass typeClass;
584 sal_Bool isTypeDef = (m_typeMgr.getTypeClass(baseType) == RT_TYPE_TYPEDEF);
585 TypeReader reader;
587 while (isTypeDef)
589 reader = m_typeMgr.getTypeReader(baseType);
591 if (reader.isValid())
593 typeClass = reader.getTypeClass();
595 if (typeClass == RT_TYPE_TYPEDEF)
596 baseType = reader.getSuperTypeName();
597 else
598 isTypeDef = sal_False;
599 } else
601 break;
605 return baseType;
608 OString IdlType::checkRealBaseType(const OString& type, sal_Bool bResolveTypeOnly)
610 sal_uInt32 index = type.lastIndexOf(']');
611 OString baseType = (index > 0 ? ((OString)type).copy(index+1) : type);
612 OString seqPrefix = (index > 0 ? ((OString)type).copy(0, index+1) : OString());
614 RegistryTypeReaderLoader & rReaderLoader = getRegistryTypeReaderLoader();
616 RegistryKey key;
617 sal_uInt8* pBuffer=NULL;
618 RTTypeClass typeClass;
619 sal_Bool mustBeChecked = (m_typeMgr.getTypeClass(baseType) == RT_TYPE_TYPEDEF);
620 TypeReader reader;
622 while (mustBeChecked)
624 reader = m_typeMgr.getTypeReader(baseType);
626 if (reader.isValid())
628 typeClass = reader.getTypeClass();
630 if (typeClass == RT_TYPE_TYPEDEF)
632 baseType = reader.getSuperTypeName();
633 index = baseType.lastIndexOf(']');
634 if (index > 0)
636 seqPrefix += baseType.copy(0, index+1);
637 baseType = baseType.copy(index+1);
639 } else
640 mustBeChecked = sal_False;
641 } else
643 break;
647 if ( bResolveTypeOnly )
648 baseType = seqPrefix + baseType;
650 return baseType;
653 void IdlType::dumpConstantValue(FileStream& o, sal_uInt16 index)
655 RTConstValue constValue = m_reader.getFieldConstValue(index);
657 switch (constValue.m_type)
659 case RT_TYPE_BOOL:
660 if (constValue.m_value.aBool)
661 o << "true";
662 else
663 o << "false";
664 break;
665 case RT_TYPE_BYTE:
667 char tmp[16];
668 snprintf(tmp, sizeof(tmp), "0x%x", (sal_Int8)constValue.m_value.aByte);
669 o << tmp;
671 break;
672 case RT_TYPE_INT16:
673 o << constValue.m_value.aShort;
674 break;
675 case RT_TYPE_UINT16:
676 o << constValue.m_value.aUShort;
677 break;
678 case RT_TYPE_INT32:
679 o << constValue.m_value.aLong;
680 break;
681 case RT_TYPE_UINT32:
682 o << constValue.m_value.aULong;
683 break;
684 case RT_TYPE_INT64:
686 ::rtl::OString tmp( OString::valueOf(constValue.m_value.aHyper) );
687 o << tmp.getStr();
689 break;
690 case RT_TYPE_UINT64:
692 ::rtl::OString tmp( OString::valueOf((sal_Int64)constValue.m_value.aUHyper) );
693 o << tmp.getStr();
695 break;
696 case RT_TYPE_FLOAT:
698 ::rtl::OString tmp( OString::valueOf(constValue.m_value.aFloat) );
699 o << tmp.getStr();
701 break;
702 case RT_TYPE_DOUBLE:
704 ::rtl::OString tmp( OString::valueOf(constValue.m_value.aDouble) );
705 o << tmp.getStr();
707 break;
708 case RT_TYPE_STRING:
710 ::rtl::OUString aUStr(constValue.m_value.aString);
711 ::rtl::OString aStr = ::rtl::OUStringToOString(aUStr, RTL_TEXTENCODING_ASCII_US);
712 o << "\"" << aStr.getStr() << "\")";
714 break;
718 void IdlType::inc(sal_uInt32 num)
720 m_indentLength += num;
723 void IdlType::dec(sal_uInt32 num)
725 if (m_indentLength - num < 0)
726 m_indentLength = 0;
727 else
728 m_indentLength -= num;
731 OString IdlType::indent()
733 OStringBuffer tmp(m_indentLength);
735 for (sal_uInt32 i=0; i < m_indentLength; i++)
737 tmp.append(' ');
739 return tmp.makeStringAndClear();
742 OString IdlType::indent(sal_uInt32 num)
744 OStringBuffer tmp(m_indentLength + num);
746 for (sal_uInt32 i=0; i < m_indentLength + num; i++)
748 tmp.append(' ');
750 return tmp.makeStringAndClear();
753 //*************************************************************************
754 // InterfaceType
755 //*************************************************************************
756 InterfaceType::InterfaceType(TypeReader& typeReader,
757 const OString& typeName,
758 const TypeManager& typeMgr,
759 const TypeDependency& typeDependencies)
760 : IdlType(typeReader, typeName, typeMgr, typeDependencies)
762 m_inheritedMemberCount = 0;
763 m_hasAttributes = sal_False;
764 m_hasMethods = sal_False;
767 InterfaceType::~InterfaceType()
772 sal_Bool InterfaceType::dumpHFile(FileStream& o)
773 throw( CannotDumpException )
775 OString headerDefine(dumpHeaderDefine(o, "IDL"));
776 o << "\n";
778 dumpDefaultHIncludes(o);
779 o << "\n";
780 dumpDepIncludes(o, m_typeName, "idl");
781 o << "\n";
782 dumpNameSpace(o);
784 // write documentation
785 OString aDoc = m_reader.getDoku();
786 if( aDoc.getLength() )
787 o << "/**\n" << aDoc << "\n*/";
788 o << "\ninterface " << m_name;
790 OString superType(m_reader.getSuperTypeName());
791 if (superType.getLength() > 0)
792 o << " : " << scopedName(m_typeName, superType);
794 o << "\n{\n";
795 inc();
797 dumpAttributes(o);
798 dumpMethods(o);
800 dec();
801 o << "};\n\n";
803 dumpNameSpace(o, sal_False);
805 // o << "\nnamespace com { namespace sun { namespace star { namespace uno {\n"
806 // << "class Type;\n} } } }\n\n";
808 o << "#endif /* "<< headerDefine << "*/" << "\n";
809 return sal_True;
812 void InterfaceType::dumpAttributes(FileStream& o)
814 sal_uInt32 fieldCount = m_reader.getFieldCount();
815 sal_Bool first=sal_True;
817 RTFieldAccess access = RT_ACCESS_INVALID;
818 OString fieldName;
819 OString fieldType;
820 for (sal_uInt16 i=0; i < fieldCount; i++)
822 access = m_reader.getFieldAccess(i);
824 if (access == RT_ACCESS_CONST || access == RT_ACCESS_INVALID)
825 continue;
827 fieldName = m_reader.getFieldName(i);
828 fieldType = m_reader.getFieldType(i);
830 if (first)
832 first = sal_False;
833 o << "\n";
836 // write documentation
837 OString aDoc = m_reader.getFieldDoku(i);
838 if( aDoc.getLength() )
839 o << "/**\n" << aDoc << "\n*/\n";
841 if (access == RT_ACCESS_READONLY)
842 o << indent() << "readonly attribute ";
843 else
844 o << indent() << "attribute ";
845 dumpType(o, fieldType);
846 o << " " << fieldName << ";\n";
850 void InterfaceType::dumpMethods(FileStream& o)
852 sal_uInt32 methodCount = m_reader.getMethodCount();
854 OString methodName, returnType, paramType, paramName;
855 sal_uInt32 paramCount = 0;
856 sal_uInt32 excCount = 0;
857 RTMethodMode methodMode = RT_MODE_INVALID;
858 RTParamMode paramMode = RT_PARAM_INVALID;
860 sal_Bool bRef = sal_False;
861 sal_Bool bConst = sal_False;
862 sal_Bool bWithRunTimeExcp = sal_True;
864 for (sal_Int16 i=0; i < methodCount; i++)
866 methodName = m_reader.getMethodName(i);
867 returnType = m_reader.getMethodReturnType(i);
868 paramCount = m_reader.getMethodParamCount(i);
869 excCount = m_reader.getMethodExcCount(i);
870 methodMode = m_reader.getMethodMode(i);
872 if ( methodName.equals("acquire") || methodName.equals("release") )
874 bWithRunTimeExcp = sal_False;
877 // write documentation
878 OString aDoc = m_reader.getMethodDoku(i);
879 if( aDoc.getLength() )
880 o << "/**\n" << aDoc << "\n*/\n";
882 o << indent();
883 dumpType(o, returnType);
884 o << " " << methodName << "( ";
885 sal_uInt16 j;
886 for (j=0; j < paramCount; j++)
888 paramName = m_reader.getMethodParamName(i, j);
889 paramType = m_reader.getMethodParamType(i, j);
890 paramMode = m_reader.getMethodParamMode(i, j);
892 switch (paramMode)
894 case RT_PARAM_IN:
895 o << "in ";
896 break;
897 case RT_PARAM_OUT:
898 o << "out ";
899 break;
900 case RT_PARAM_INOUT:
901 o << "inout ";
902 break;
903 break;
906 dumpType(o, paramType);
907 if( paramName == "Object" )
908 o << " _Object";
909 else
910 o << " " << paramName;
912 if (j+1 < paramCount) o << ", ";
914 o << " )";
916 if( excCount )
918 o << " raises(";
919 OString excpName;
920 sal_Bool bWriteComma = sal_False;
921 sal_Bool bRTExceptionWritten = sal_False;
922 for (j=0; j < excCount; j++)
924 excpName = m_reader.getMethodExcType(i, j);
925 if( bWriteComma )
926 o << ", ";
927 o << scopedName(m_typeName, excpName);
928 bWriteComma = sal_True;
930 if(excpName == "com/sun/star/uno/RuntimeException")
931 bRTExceptionWritten = sal_True;
934 if ( bWithRunTimeExcp && !bRTExceptionWritten )
936 if( bWriteComma )
937 o << ", ";
938 o << "::com::sun::star::uno::RuntimeException";
941 o << ");\n";
943 else if ( bWithRunTimeExcp )
945 o << "raises( ::com::sun::star::uno::RuntimeException );\n";
947 else
949 o << ";\n";
955 sal_uInt32 InterfaceType::getMemberCount()
957 sal_uInt32 count = m_reader.getMethodCount();
959 if (count)
960 m_hasMethods = sal_True;
962 sal_uInt32 fieldCount = m_reader.getFieldCount();
963 RTFieldAccess access = RT_ACCESS_INVALID;
964 for (sal_uInt16 i=0; i < fieldCount; i++)
966 access = m_reader.getFieldAccess(i);
968 if (access != RT_ACCESS_CONST && access != RT_ACCESS_INVALID)
970 m_hasAttributes = sal_True;
971 count++;
974 return count;
977 sal_uInt32 InterfaceType::checkInheritedMemberCount(const TypeReader* pReader)
979 sal_uInt32 cout = 0;
980 sal_Bool bSelfCheck = sal_True;
981 if (!pReader)
983 bSelfCheck = sal_False;
984 pReader = &m_reader;
987 sal_uInt32 count = 0;
988 OString superType(pReader->getSuperTypeName());
989 if (superType.getLength() > 0)
991 TypeReader aSuperReader(m_typeMgr.getTypeReader(superType));
992 if (aSuperReader.isValid())
994 count = checkInheritedMemberCount(&aSuperReader);
998 if (bSelfCheck)
1000 count += pReader->getMethodCount();
1001 sal_uInt32 fieldCount = pReader->getFieldCount();
1002 RTFieldAccess access = RT_ACCESS_INVALID;
1003 for (sal_uInt16 i=0; i < fieldCount; i++)
1005 access = pReader->getFieldAccess(i);
1007 if (access != RT_ACCESS_CONST && access != RT_ACCESS_INVALID)
1009 count++;
1014 return count;
1017 sal_uInt32 InterfaceType::getInheritedMemberCount()
1019 if (m_inheritedMemberCount == 0)
1021 m_inheritedMemberCount = checkInheritedMemberCount(0);
1024 return m_inheritedMemberCount;
1029 //*************************************************************************
1030 // ModuleType
1031 //*************************************************************************
1032 ModuleType::ModuleType(TypeReader& typeReader,
1033 const OString& typeName,
1034 const TypeManager& typeMgr,
1035 const TypeDependency& typeDependencies)
1036 : IdlType(typeReader, typeName, typeMgr, typeDependencies)
1040 ModuleType::~ModuleType()
1045 sal_Bool ModuleType::dump(IdlOptions* pOptions)
1046 throw( CannotDumpException )
1048 sal_Bool ret = sal_False;
1050 OString outPath;
1051 if (pOptions->isValid("-O"))
1052 outPath = pOptions->getOption("-O");
1054 OString tmpName(m_typeName);
1056 if (tmpName.equals("/"))
1057 tmpName = "global";
1058 else
1059 // tmpName += "/" + m_typeName.getToken(m_typeName.getTokenCount('/') - 1, '/');
1060 tmpName += "/" + m_name;
1062 OString tmpFileName;
1063 OString hFileName = createFileNameFromType(outPath, tmpName, ".idl");
1065 sal_Bool bFileExists = sal_False;
1066 sal_Bool bFileCheck = sal_False;
1068 if ( pOptions->isValid("-G") || pOptions->isValid("-Gc") )
1070 bFileExists = fileExists( hFileName );
1071 ret = sal_True;
1074 if ( bFileExists && pOptions->isValid("-Gc") )
1076 tmpFileName = createFileNameFromType(outPath, m_typeName, ".tml");
1077 bFileCheck = sal_True;
1080 if ( !bFileExists || bFileCheck )
1082 FileStream hFile;
1084 if ( bFileCheck )
1085 hFile.open(tmpFileName);
1086 else
1087 hFile.open(hFileName);
1089 if(!hFile.isValid())
1091 OString message("cannot open ");
1092 message += hFileName + " for writing";
1093 throw CannotDumpException(message);
1096 ret = dumpHFile(hFile);
1098 hFile.close();
1099 if (ret && bFileCheck)
1101 ret = checkFileContent(hFileName, tmpFileName);
1105 return ret;
1108 sal_Bool ModuleType::dumpHFile(FileStream& o)
1109 throw( CannotDumpException )
1111 OString headerDefine(dumpHeaderDefine(o, "IDL"));
1112 o << "\n";
1114 dumpDefaultHIncludes(o);
1115 o << "\n";
1116 dumpDepIncludes(o, m_typeName, "idl");
1117 o << "\n";
1119 dumpNameSpace(o, sal_True, sal_True);
1120 o << "\n";
1122 sal_uInt32 fieldCount = m_reader.getFieldCount();
1123 RTFieldAccess access = RT_ACCESS_INVALID;
1124 OString fieldName;
1125 OString fieldType;
1126 for (sal_uInt16 i=0; i < fieldCount; i++)
1128 access = m_reader.getFieldAccess(i);
1130 if (access == RT_ACCESS_CONST)
1132 fieldName = m_reader.getFieldName(i);
1133 fieldType = m_reader.getFieldType(i);
1135 o << "const ";
1136 dumpType(o, fieldType);
1137 o << " " << fieldName << " = ";
1138 dumpConstantValue(o, i);
1139 o << ";\n";
1143 o << "\n";
1144 dumpNameSpace(o, sal_False, sal_True);
1145 o << "\n#endif /* "<< headerDefine << "*/" << "\n";
1147 return sal_True;
1150 sal_Bool ModuleType::hasConstants()
1152 sal_uInt32 fieldCount = m_reader.getFieldCount();
1153 RTFieldAccess access = RT_ACCESS_INVALID;
1155 for (sal_uInt16 i=0; i < fieldCount; i++)
1157 access = m_reader.getFieldAccess(i);
1159 if (access == RT_ACCESS_CONST)
1160 return sal_True;
1163 return sal_False;
1166 //*************************************************************************
1167 // ConstantsType
1168 //*************************************************************************
1169 ConstantsType::ConstantsType(TypeReader& typeReader,
1170 const OString& typeName,
1171 const TypeManager& typeMgr,
1172 const TypeDependency& typeDependencies)
1173 : ModuleType(typeReader, typeName, typeMgr, typeDependencies)
1177 ConstantsType::~ConstantsType()
1182 sal_Bool ConstantsType::dump(IdlOptions* pOptions)
1183 throw( CannotDumpException )
1185 sal_Bool ret = sal_False;
1187 OString outPath;
1188 if (pOptions->isValid("-O"))
1189 outPath = pOptions->getOption("-O");
1191 OString tmpFileName;
1192 OString hFileName = createFileNameFromType(outPath, m_typeName, ".idl");
1194 sal_Bool bFileExists = sal_False;
1195 sal_Bool bFileCheck = sal_False;
1197 if ( pOptions->isValid("-G") || pOptions->isValid("-Gc") )
1199 bFileExists = fileExists( hFileName );
1200 ret = sal_True;
1203 if ( bFileExists && pOptions->isValid("-Gc") )
1205 tmpFileName = createFileNameFromType(outPath, m_typeName, ".tml");
1206 bFileCheck = sal_True;
1209 if ( !bFileExists || bFileCheck )
1211 FileStream hFile;
1213 if ( bFileCheck )
1214 hFile.open(tmpFileName);
1215 else
1216 hFile.open(hFileName);
1218 if(!hFile.isValid())
1220 OString message("cannot open ");
1221 message += hFileName + " for writing";
1222 throw CannotDumpException(message);
1225 ret = dumpHFile(hFile);
1227 hFile.close();
1228 if (ret && bFileCheck)
1230 ret = checkFileContent(hFileName, tmpFileName);
1234 return ret;
1237 //*************************************************************************
1238 // StructureType
1239 //*************************************************************************
1240 StructureType::StructureType(TypeReader& typeReader,
1241 const OString& typeName,
1242 const TypeManager& typeMgr,
1243 const TypeDependency& typeDependencies)
1244 : IdlType(typeReader, typeName, typeMgr, typeDependencies)
1248 StructureType::~StructureType()
1253 sal_Bool StructureType::dumpHFile(FileStream& o)
1254 throw( CannotDumpException )
1256 OString headerDefine(dumpHeaderDefine(o, "IDL"));
1257 o << "\n";
1259 dumpDefaultHIncludes(o);
1260 o << "\n";
1261 dumpDepIncludes(o, m_typeName, "idl");
1262 o << "\n";
1264 dumpNameSpace(o);
1266 // write documentation
1267 OString aDoc = m_reader.getDoku();
1268 if( aDoc.getLength() )
1269 o << "/**\n" << aDoc << "\n*/";
1271 o << "\nstruct " << m_name;
1272 o << "\n{\n";
1273 inc();
1275 OString superType(m_reader.getSuperTypeName());
1276 if (superType.getLength() > 0)
1277 dumpSuperMember(o, superType);
1279 sal_uInt32 fieldCount = m_reader.getFieldCount();
1280 RTFieldAccess access = RT_ACCESS_INVALID;
1281 OString fieldName;
1282 OString fieldType;
1283 sal_uInt16 i=0;
1285 for (i=0; i < fieldCount; i++)
1287 access = m_reader.getFieldAccess(i);
1289 if (access == RT_ACCESS_CONST || access == RT_ACCESS_INVALID)
1290 continue;
1292 fieldName = m_reader.getFieldName(i);
1293 fieldType = m_reader.getFieldType(i);
1295 // write documentation
1296 OString aDoc = m_reader.getFieldDoku(i);
1297 if( aDoc.getLength() )
1298 o << "/**\n" << aDoc << "\n*/";
1300 o << indent();
1301 dumpType(o, fieldType);
1302 o << " " << fieldName << ";\n";
1305 dec();
1306 o << "};\n\n";
1308 dumpNameSpace(o, sal_False);
1310 o << "#endif /* "<< headerDefine << "*/" << "\n";
1312 return sal_True;
1315 void StructureType::dumpSuperMember(FileStream& o, const OString& superType)
1317 if (superType.getLength() > 0)
1319 TypeReader aSuperReader(m_typeMgr.getTypeReader(superType));
1321 if (aSuperReader.isValid())
1323 dumpSuperMember(o, aSuperReader.getSuperTypeName());
1325 sal_uInt32 fieldCount = aSuperReader.getFieldCount();
1326 RTFieldAccess access = RT_ACCESS_INVALID;
1327 OString fieldName;
1328 OString fieldType;
1329 for (sal_uInt16 i=0; i < fieldCount; i++)
1331 access = aSuperReader.getFieldAccess(i);
1333 if (access == RT_ACCESS_CONST || access == RT_ACCESS_INVALID)
1334 continue;
1336 fieldName = aSuperReader.getFieldName(i);
1337 fieldType = aSuperReader.getFieldType(i);
1339 // write documentation
1340 OString aDoc = aSuperReader.getFieldDoku(i);
1341 if( aDoc.getLength() )
1342 o << "/**\n" << aDoc << "\n*/";
1344 o << indent();
1345 dumpType(o, fieldType);
1346 o << " ";
1347 o << fieldName << ";\n";
1353 //*************************************************************************
1354 // ExceptionType
1355 //*************************************************************************
1356 ExceptionType::ExceptionType(TypeReader& typeReader,
1357 const OString& typeName,
1358 const TypeManager& typeMgr,
1359 const TypeDependency& typeDependencies)
1360 : IdlType(typeReader, typeName, typeMgr, typeDependencies)
1364 ExceptionType::~ExceptionType()
1369 sal_Bool ExceptionType::dumpHFile(FileStream& o)
1370 throw( CannotDumpException )
1372 OString headerDefine(dumpHeaderDefine(o, "IDL"));
1373 o << "\n";
1375 dumpDefaultHIncludes(o);
1376 o << "\n";
1377 dumpDepIncludes(o, m_typeName, "idl");
1378 o << "\n";
1380 dumpNameSpace(o);
1382 // write documentation
1383 OString aDoc = m_reader.getDoku();
1384 if( aDoc.getLength() )
1385 o << "/**\n" << aDoc << "\n*/";
1387 o << "\nexception " << m_name;
1388 o << "\n{\n";
1389 inc();
1391 // Write extra member for derived exceptions
1392 o << indent() << "/*extra member to hold a derived exception */\n";
1393 o << indent() << "any _derivedException;\n";
1394 OString superType(m_reader.getSuperTypeName());
1395 if (superType.getLength() > 0)
1396 dumpSuperMember(o, superType);
1398 sal_uInt32 fieldCount = m_reader.getFieldCount();
1399 RTFieldAccess access = RT_ACCESS_INVALID;
1400 OString fieldName;
1401 OString fieldType;
1402 sal_uInt16 i = 0;
1404 for (i=0; i < fieldCount; i++)
1406 access = m_reader.getFieldAccess(i);
1408 if (access == RT_ACCESS_CONST || access == RT_ACCESS_INVALID)
1409 continue;
1411 fieldName = m_reader.getFieldName(i);
1412 fieldType = m_reader.getFieldType(i);
1414 // write documentation
1415 OString aDoc = m_reader.getFieldDoku(i);
1416 if( aDoc.getLength() )
1417 o << "/**\n" << aDoc << "\n*/";
1419 o << indent();
1420 dumpType(o, fieldType);
1421 o << " " << fieldName << ";\n";
1425 dec();
1426 o << "};\n\n";
1428 dumpNameSpace(o, sal_False);
1430 o << "#endif /* "<< headerDefine << "*/" << "\n";
1432 return sal_True;
1435 void ExceptionType::dumpSuperMember(FileStream& o, const OString& superType)
1437 if (superType.getLength() > 0)
1439 TypeReader aSuperReader(m_typeMgr.getTypeReader(superType));
1441 if (aSuperReader.isValid())
1443 dumpSuperMember(o, aSuperReader.getSuperTypeName());
1445 sal_uInt32 fieldCount = aSuperReader.getFieldCount();
1446 RTFieldAccess access = RT_ACCESS_INVALID;
1447 OString fieldName;
1448 OString fieldType;
1449 for (sal_uInt16 i=0; i < fieldCount; i++)
1451 access = aSuperReader.getFieldAccess(i);
1453 if (access == RT_ACCESS_CONST || access == RT_ACCESS_INVALID)
1454 continue;
1456 fieldName = aSuperReader.getFieldName(i);
1457 fieldType = aSuperReader.getFieldType(i);
1459 // write documentation
1460 OString aDoc = aSuperReader.getFieldDoku(i);
1461 if( aDoc.getLength() )
1462 o << "/**\n" << aDoc << "\n*/";
1464 o << indent();
1465 dumpType(o, fieldType);
1466 o << " ";
1467 o << fieldName << ";\n";
1473 //*************************************************************************
1474 // EnumType
1475 //*************************************************************************
1476 EnumType::EnumType(TypeReader& typeReader,
1477 const OString& typeName,
1478 const TypeManager& typeMgr,
1479 const TypeDependency& typeDependencies)
1480 : IdlType(typeReader, typeName, typeMgr, typeDependencies)
1484 EnumType::~EnumType()
1489 sal_Bool EnumType::dumpHFile(FileStream& o)
1490 throw( CannotDumpException )
1492 OString headerDefine(dumpHeaderDefine(o, "IDL"));
1493 o << "\n";
1495 dumpDefaultHIncludes(o);
1496 o << "\n";
1498 dumpNameSpace(o);
1500 // write documentation
1501 OString aDoc = m_reader.getDoku();
1502 if( aDoc.getLength() )
1503 o << "/**\n" << aDoc << "\n*/";
1505 o << "\nenum " << m_name << "\n{\n";
1506 inc();
1508 sal_uInt32 fieldCount = m_reader.getFieldCount();
1509 RTFieldAccess access = RT_ACCESS_INVALID;
1510 RTConstValue constValue;
1511 OString fieldName;
1512 sal_uInt32 value=0;
1513 for (sal_uInt16 i=0; i < fieldCount; i++)
1515 access = m_reader.getFieldAccess(i);
1517 if (access != RT_ACCESS_CONST)
1518 continue;
1520 fieldName = m_reader.getFieldName(i);
1521 constValue = m_reader.getFieldConstValue(i);
1523 if (constValue.m_type == RT_TYPE_INT32)
1524 value = constValue.m_value.aLong;
1525 else
1526 value++;
1528 /* doesn't work with rational rose 2000
1529 // write documentation
1530 OString aDoc = m_reader.getFieldDoku(i);
1531 if( aDoc.getLength() )
1533 // o << "/**\n" << aDoc << "\n*/\n";
1534 o << indent() << fieldName;
1535 if( i +1 < fieldCount )
1536 o << ",\n";
1539 dec();
1540 o << "\n};\n\n";
1542 dumpNameSpace(o, sal_False);
1544 o << "#endif /* "<< headerDefine << "*/" << "\n";
1546 return sal_True;
1550 //*************************************************************************
1551 // TypeDefType
1552 //*************************************************************************
1553 TypeDefType::TypeDefType(TypeReader& typeReader,
1554 const OString& typeName,
1555 const TypeManager& typeMgr,
1556 const TypeDependency& typeDependencies)
1557 : IdlType(typeReader, typeName, typeMgr, typeDependencies)
1561 TypeDefType::~TypeDefType()
1566 sal_Bool TypeDefType::dumpHFile(FileStream& o)
1567 throw( CannotDumpException )
1569 OString headerDefine(dumpHeaderDefine(o, "IDL"));
1570 o << "\n";
1572 dumpDefaultHIncludes(o);
1573 o << "\n";
1574 dumpDepIncludes(o, m_typeName, "idl");
1575 o << "\n";
1577 dumpNameSpace(o);
1579 o << "\ntypedef ";
1580 dumpType(o, m_reader.getSuperTypeName());
1581 o << " " << m_name << ";\n\n";
1583 dumpNameSpace(o, sal_False);
1585 o << "#endif /* "<< headerDefine << "*/" << "\n";
1587 return sal_True;
1591 //*************************************************************************
1592 // produceType
1593 //*************************************************************************
1594 sal_Bool produceType(const OString& typeName,
1595 TypeManager& typeMgr,
1596 TypeDependency& typeDependencies,
1597 IdlOptions* pOptions)
1598 throw( CannotDumpException )
1600 if (typeDependencies.isGenerated(typeName))
1601 return sal_True;
1603 TypeReader reader(typeMgr.getTypeReader(typeName));
1605 if (!reader.isValid())
1607 if (typeName.equals("/"))
1608 return sal_True;
1609 else
1610 return sal_False;
1613 if( !checkTypeDependencies(typeMgr, typeDependencies, typeName))
1614 return sal_False;
1616 RTTypeClass typeClass = reader.getTypeClass();
1617 sal_Bool ret = sal_False;
1618 switch (typeClass)
1620 case RT_TYPE_INTERFACE:
1622 InterfaceType iType(reader, typeName, typeMgr, typeDependencies);
1623 ret = iType.dump(pOptions);
1624 if (ret) typeDependencies.setGenerated(typeName);
1625 ret = iType.dumpDependedTypes(pOptions);
1627 break;
1628 case RT_TYPE_MODULE:
1630 ModuleType mType(reader, typeName, typeMgr, typeDependencies);
1631 if (mType.hasConstants())
1633 ret = mType.dump(pOptions);
1634 if (ret) typeDependencies.setGenerated(typeName);
1635 // ret = mType.dumpDependedTypes(pOptions);
1636 } else
1638 typeDependencies.setGenerated(typeName);
1639 ret = sal_True;
1642 break;
1643 case RT_TYPE_STRUCT:
1645 StructureType sType(reader, typeName, typeMgr, typeDependencies);
1646 ret = sType.dump(pOptions);
1647 if (ret) typeDependencies.setGenerated(typeName);
1648 ret = sType.dumpDependedTypes(pOptions);
1650 break;
1651 case RT_TYPE_ENUM:
1653 EnumType enType(reader, typeName, typeMgr, typeDependencies);
1654 ret = enType.dump(pOptions);
1655 if (ret) typeDependencies.setGenerated(typeName);
1656 ret = enType.dumpDependedTypes(pOptions);
1658 break;
1659 case RT_TYPE_EXCEPTION:
1661 ExceptionType eType(reader, typeName, typeMgr, typeDependencies);
1662 ret = eType.dump(pOptions);
1663 if (ret) typeDependencies.setGenerated(typeName);
1664 ret = eType.dumpDependedTypes(pOptions);
1666 break;
1667 case RT_TYPE_TYPEDEF:
1669 TypeDefType tdType(reader, typeName, typeMgr, typeDependencies);
1670 ret = tdType.dump(pOptions);
1671 if (ret) typeDependencies.setGenerated(typeName);
1672 ret = tdType.dumpDependedTypes(pOptions);
1674 break;
1675 case RT_TYPE_CONSTANTS:
1677 ConstantsType cType(reader, typeName, typeMgr, typeDependencies);
1678 if (cType.hasConstants())
1680 ret = cType.dump(pOptions);
1681 if (ret) typeDependencies.setGenerated(typeName);
1682 // ret = cType.dumpDependedTypes(pOptions);
1683 } else
1685 typeDependencies.setGenerated(typeName);
1686 ret = sal_True;
1689 break;
1690 case RT_TYPE_SERVICE:
1691 case RT_TYPE_OBJECT:
1692 ret = sal_True;
1693 break;
1696 return ret;
1699 //*************************************************************************
1700 // scopedName
1701 //*************************************************************************
1702 OString scopedName(const OString& scope, const OString& type,
1703 sal_Bool bNoNameSpace)
1705 sal_Int32 nPos = type.lastIndexOf( '/' );
1706 if (nPos == -1)
1707 return type;
1709 if (bNoNameSpace)
1710 return type.copy(nPos+1);
1712 OStringBuffer tmpBuf(type.getLength()*2);
1713 nPos = 0;
1716 tmpBuf.append("::");
1717 tmpBuf.append(type.getToken(0, '/', nPos));
1718 } while( nPos != -1 );
1720 return tmpBuf.makeStringAndClear();
1723 //*************************************************************************
1724 // shortScopedName
1725 //*************************************************************************
1726 OString scope(const OString& scope, const OString& type )
1728 sal_Int32 nPos = type.lastIndexOf( '/' );
1729 if( nPos == -1 )
1730 return OString();
1732 // scoped name only if the namespace is not equal
1733 if (scope.lastIndexOf('/') > 0)
1735 OString tmpScp(scope.copy(0, scope.lastIndexOf('/')));
1736 OString tmpScp2(type.copy(0, nPos));
1738 if (tmpScp == tmpScp2)
1739 return OString();
1742 OString aScope( type.copy( 0, nPos ) );
1743 OStringBuffer tmpBuf(aScope.getLength()*2);
1745 nPos = 0;
1748 tmpBuf.append("::");
1749 tmpBuf.append(aScope.getToken(0, '/', nPos));
1750 } while( nPos != -1 );
1752 return tmpBuf.makeStringAndClear();