1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #using <mscorlib.dll>
29 #using <cli_basetypes.dll>
30 
31 #include <vcclr.h>
32 
33 #include "osl/diagnose.h"
34 #include "com/sun/star/reflection/XConstantTypeDescription.hpp"
35 #include "com/sun/star/reflection/XConstantsTypeDescription.hpp"
36 #include "com/sun/star/reflection/XEnumTypeDescription.hpp"
37 #include "com/sun/star/reflection/XInterfaceTypeDescription2.hpp"
38 #include "com/sun/star/reflection/XCompoundTypeDescription.hpp"
39 #include "com/sun/star/reflection/XServiceTypeDescription2.hpp"
40 #include "com/sun/star/reflection/XSingletonTypeDescription2.hpp"
41 #include "com/sun/star/reflection/XInterfaceMethodTypeDescription.hpp"
42 
43 #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) )
44 
45 
46 namespace css = ::com::sun::star;
47 
48 namespace climaker
49 {
50 
51 //------------------------------------------------------------------------------
52 extern bool g_verbose;
53 
54 __gc struct Constants
55 {
56 	static const ::System::String * sUnoVoid = S"void";
57 	static const ::System::String * sUnoType = S"type";
58 	static const ::System::String * sUnoAny =  S"any";
59 	static const ::System::String * sUnoBool = S"boolean";
60 	static const ::System::String * sUnoByte = S"byte";
61 	static const ::System::String * sUnoChar = S"char";
62 	static const ::System::String * sUnoShort = S"short";
63 	static const ::System::String * sUnoUShort = S"unsigned short";
64 	static const ::System::String * sUnoLong = S"long";
65 	static const ::System::String * sUnoULong = S"unsigned long";
66 	static const ::System::String * sUnoHyper = S"hyper";
67 	static const ::System::String * sUnoUHyper = S"unsigned hyper";
68 	static const ::System::String * sUnoString = S"string";
69 	static const ::System::String * sUnoFloat = S"float";
70 	static const ::System::String * sUnoDouble = S"double";
71 	static const ::System::String * sUnoXInterface = S"com.sun.star.uno.XInterface";
72 	static const ::System::String * sBrackets = S"[]";
73 
74     static const System::String* sObject = S"System.Object";
75     static const System::String* sType = S"System.Type";
76     static const System::String* sUnoidl = S"unoidl.";
77     static const System::String* sVoid = S"System.Void";
78     static const System::String* sAny = S"uno.Any";
79     static const System::String* sBoolean = S"System.Boolean";
80     static const System::String* sChar = S"System.Char";
81     static const System::String* sByte = S"System.Byte";
82     static const System::String* sInt16 = S"System.Int16";
83     static const System::String* sUInt16 = S"System.UInt16";
84     static const System::String* sInt32 = S"System.Int32";
85     static const System::String* sUInt32 = S"System.UInt32";
86     static const System::String* sInt64 = S"System.Int64";
87     static const System::String* sUInt64 = S"System.UInt64";
88     static const System::String* sString = S"System.String";
89     static const System::String* sSingle = S"System.Single";
90     static const System::String* sDouble = S"System.Double";
91 	static const System::String* sComma = new System::String(S",");
92 
93 };
94 
95 //------------------------------------------------------------------------------
96 inline ::System::String * ustring_to_String( ::rtl::OUString const & ustr )
97 {
98     return new ::System::String( ustr.getStr(), 0, ustr.getLength() );
99 }
100 
101 //------------------------------------------------------------------------------
102 inline ::rtl::OUString String_to_ustring( ::System::String * str )
103 {
104     OSL_ASSERT( sizeof (wchar_t) == sizeof (sal_Unicode) );
105     wchar_t const __pin * chars = PtrToStringChars( str );
106     return ::rtl::OUString( chars, str->get_Length() );
107 }
108 
109 /* If the argument type is a typedef for an interface then the interface
110    type description is returned, otherwise an exeption is thrown.
111 */
112 css::uno::Reference< css::reflection::XInterfaceTypeDescription2 >
113 resolveInterfaceTypedef(const css::uno::Reference<css::reflection::XTypeDescription>& type);
114 
115 const ::System::Reflection::MethodAttributes c_ctor_method_attr =
116 (::System::Reflection::MethodAttributes)
117     (::System::Reflection::MethodAttributes::Public |
118      ::System::Reflection::MethodAttributes::HideBySig |
119      ::System::Reflection::MethodAttributes::SpecialName |
120      ::System::Reflection::MethodAttributes::RTSpecialName
121      /* | xxx todo: ??? compiler does not know Instance ???
122         ::System::Reflection::MethodAttributes::Instance*/);
123 
124 //==============================================================================
125 __gc class TypeEmitter : public ::System::IDisposable
126 {
127     ::System::Reflection::Emit::ModuleBuilder * m_module_builder;
128     ::System::Reflection::Assembly * m_extra_assemblies __gc [];
129 
130     ::System::Reflection::MethodInfo * m_method_info_Type_GetTypeFromHandle;
131 
132     ::System::Type * m_type_Exception;
133     ::System::Type * get_type_Exception();
134     ::System::Type * m_type_RuntimeException;
135     ::System::Type * get_type_RuntimeException();
136 
137     ::System::Reflection::Emit::CustomAttributeBuilder* get_service_exception_attribute(
138         const css::uno::Reference<css::reflection::XServiceConstructorDescription> & ctorDesc);
139     ::System::Reflection::Emit::CustomAttributeBuilder* get_iface_method_exception_attribute(
140         const css::uno::Reference< css::reflection::XInterfaceMethodTypeDescription >& xMethod );
141     ::System::Reflection::Emit::CustomAttributeBuilder* get_exception_attribute(
142         const css::uno::Sequence<css::uno::Reference<
143         css::reflection::XCompoundTypeDescription > >& seq_exceptionsTd );
144 /* Creates ::System::Type object for UNO exceptions. The UNO exceptions are
145        obtained by
146        com::sun::star::reflection::XServiceConstructorDescription::getExceptions
147        In a first step the respective CLI types are created. Then it is examined
148        if a Type represents a super class of another class. If so the Type of the
149        derived class is discarded. For example there are a uno RuntimeException and
150        a DeploymentException which inherits RuntimeException. Then only the cli Type
151        of the RuntimeException is returned.
152        The purpose of this function is to provide exceptions for which catch blocks
153        are generated in the service constructor code.
154 
155        It is always an instance of an ArrayList returned, even if the sequence argument
156        does not contain elements.
157     */
158     ::System::Collections::ArrayList * get_service_ctor_method_exceptions_reduced(
159         const css::uno::Sequence<
160         css::uno::Reference<css::reflection::XCompoundTypeDescription> > & seqExceptionsTd);
161 
162 
163     __gc class iface_entry
164     {
165     public:
166         css::reflection::XInterfaceTypeDescription2 * m_xType;
167         ::System::Reflection::Emit::TypeBuilder * m_type_builder;
168     };
169     ::System::Collections::Hashtable * m_incomplete_ifaces;
170     ::System::Type * complete_iface_type( iface_entry * entry );
171 
172     __gc class struct_entry
173     {
174     public:
175          css::reflection::XCompoundTypeDescription * m_xType;
176         ::System::Reflection::Emit::TypeBuilder * m_type_builder;
177         ::System::Type * m_base_type;
178 
179         ::System::String * m_member_names __gc [];
180         ::System::Type * m_param_types __gc [];
181         ::System::Reflection::ConstructorInfo * m_default_ctor;
182         ::System::Reflection::ConstructorInfo * m_ctor;
183     };
184     ::System::Collections::Hashtable * m_incomplete_structs;
185     ::System::Type * complete_struct_type( struct_entry * entry );
186 
187     /*  returns the type for the name. If it is a struct then it may
188         complete the struct if not already done. This also refers to its
189         base types.
190 
191         @param sName
192             the full name of the type.
193         @return the type object for sName. Not necessarily a struct.
194     */
195     ::System::Type * get_complete_struct( ::System::String * sName);
196 
197     __gc class service_entry
198     {
199     public:
200         ::System::Reflection::Emit::TypeBuilder * m_type_builder;
201         css::reflection::XServiceTypeDescription2 * m_xType;
202     };
203     ::System::Collections::Hashtable * m_incomplete_services;
204     ::System::Type * complete_service_type(service_entry * entry);
205 
206     __gc class singleton_entry
207     {
208     public:
209         ::System::Reflection::Emit::TypeBuilder * m_type_builder;
210         css::reflection::XSingletonTypeDescription2 * m_xType;
211     };
212 
213 
214     ::System::Collections::Hashtable * m_incomplete_singletons;
215     ::System::Type * complete_singleton_type(singleton_entry * entry);
216 
217 
218     ::System::Collections::Hashtable * m_generated_structs;
219 
220     ::System::Type * get_type(
221         ::System::String * cli_name, bool throw_exc );
222     ::System::Type * get_type(
223         css::uno::Reference<
224         css::reflection::XConstantTypeDescription > const & xType );
225     ::System::Type * get_type(
226         css::uno::Reference<
227         css::reflection::XConstantsTypeDescription > const & xType );
228     ::System::Type * get_type(
229         css::uno::Reference<
230         css::reflection::XEnumTypeDescription > const & xType );
231     /* returns the type for a struct or exception. In case of a polymorphic struct it may
232         return a ::uno::PolymorphicType (cli_basetypes.dll) only if the struct is already
233         complete.
234     */
235     ::System::Type * get_type(
236         css::uno::Reference<
237         css::reflection::XCompoundTypeDescription > const & xType );
238     ::System::Type * get_type(
239         css::uno::Reference<
240         css::reflection::XInterfaceTypeDescription2 > const & xType );
241     ::System::Type * get_type(
242         css::uno::Reference<
243         css::reflection::XSingletonTypeDescription2 > const & xType );
244 
245     /*
246       May return NULL if the service description is an obsolete. See
247       description of
248       com.sun.star.reflection.XServiceTypeDescription2.isSingleInterfaceBased
249      */
250     ::System::Type * get_type(
251         css::uno::Reference<
252         css::reflection::XServiceTypeDescription2 > const & xType );
253 public:
254     TypeEmitter(
255         ::System::Reflection::Emit::ModuleBuilder * module_builder,
256         ::System::Reflection::Assembly * assemblies __gc [] );
257     // must be called to finish up uncompleted types
258     void Dispose();
259 
260     ::System::Reflection::Assembly * type_resolve(
261         ::System::Object * sender, ::System::ResolveEventArgs * args );
262 
263     ::System::Type * get_type(
264         css::uno::Reference<
265         css::reflection::XTypeDescription > const & xType );
266 };
267 
268 }
269