1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 #using <mscorlib.dll> 25 #using <cli_basetypes.dll> 26 27 #include <vcclr.h> 28 29 #include "osl/diagnose.h" 30 #include "com/sun/star/reflection/XConstantTypeDescription.hpp" 31 #include "com/sun/star/reflection/XConstantsTypeDescription.hpp" 32 #include "com/sun/star/reflection/XEnumTypeDescription.hpp" 33 #include "com/sun/star/reflection/XInterfaceTypeDescription2.hpp" 34 #include "com/sun/star/reflection/XCompoundTypeDescription.hpp" 35 #include "com/sun/star/reflection/XServiceTypeDescription2.hpp" 36 #include "com/sun/star/reflection/XSingletonTypeDescription2.hpp" 37 #include "com/sun/star/reflection/XInterfaceMethodTypeDescription.hpp" 38 39 #define OUSTR(x) ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM(x) ) 40 41 42 namespace css = ::com::sun::star; 43 44 namespace climaker 45 { 46 47 //------------------------------------------------------------------------------ 48 extern bool g_verbose; 49 50 ref struct Constants 51 { 52 // literal, not "static const": in C++/CLI a compile-time constant field is 53 // spelled literal, and it is substituted at the use site exactly as the 54 // S"..." initialised statics were. 55 literal ::System::String ^ sUnoVoid = "void"; 56 literal ::System::String ^ sUnoType = "type"; 57 literal ::System::String ^ sUnoAny = "any"; 58 literal ::System::String ^ sUnoBool = "boolean"; 59 literal ::System::String ^ sUnoByte = "byte"; 60 literal ::System::String ^ sUnoChar = "char"; 61 literal ::System::String ^ sUnoShort = "short"; 62 literal ::System::String ^ sUnoUShort = "unsigned short"; 63 literal ::System::String ^ sUnoLong = "long"; 64 literal ::System::String ^ sUnoULong = "unsigned long"; 65 literal ::System::String ^ sUnoHyper = "hyper"; 66 literal ::System::String ^ sUnoUHyper = "unsigned hyper"; 67 literal ::System::String ^ sUnoString = "string"; 68 literal ::System::String ^ sUnoFloat = "float"; 69 literal ::System::String ^ sUnoDouble = "double"; 70 literal ::System::String ^ sUnoXInterface = "com.sun.star.uno.XInterface"; 71 literal ::System::String ^ sBrackets = "[]"; 72 73 literal System::String ^ sObject = "System.Object"; 74 literal System::String ^ sType = "System.Type"; 75 literal System::String ^ sUnoidl = "unoidl."; 76 literal System::String ^ sVoid = "System.Void"; 77 literal System::String ^ sAny = "uno.Any"; 78 literal System::String ^ sBoolean = "System.Boolean"; 79 literal System::String ^ sChar = "System.Char"; 80 literal System::String ^ sByte = "System.Byte"; 81 literal System::String ^ sInt16 = "System.Int16"; 82 literal System::String ^ sUInt16 = "System.UInt16"; 83 literal System::String ^ sInt32 = "System.Int32"; 84 literal System::String ^ sUInt32 = "System.UInt32"; 85 literal System::String ^ sInt64 = "System.Int64"; 86 literal System::String ^ sUInt64 = "System.UInt64"; 87 literal System::String ^ sString = "System.String"; 88 literal System::String ^ sSingle = "System.Single"; 89 literal System::String ^ sDouble = "System.Double"; 90 literal System::String ^ sComma = ","; 91 92 }; 93 94 //------------------------------------------------------------------------------ 95 inline ::System::String ^ ustring_to_String( ::rtl::OUString const & ustr ) 96 { 97 return gcnew ::System::String( ustr.getStr(), 0, ustr.getLength() ); 98 } 99 100 //------------------------------------------------------------------------------ 101 inline ::rtl::OUString String_to_ustring( ::System::String ^ str ) 102 { 103 OSL_ASSERT( sizeof (wchar_t) == sizeof (sal_Unicode) ); 104 pin_ptr< wchar_t const > chars = PtrToStringChars( str ); 105 return ::rtl::OUString( chars, str->Length ); 106 } 107 108 /* If the argument type is a typedef for an interface then the interface 109 type description is returned, otherwise an exception is thrown. 110 */ 111 css::uno::Reference< css::reflection::XInterfaceTypeDescription2 > 112 resolveInterfaceTypedef(const css::uno::Reference<css::reflection::XTypeDescription>& type); 113 114 const ::System::Reflection::MethodAttributes c_ctor_method_attr = 115 (::System::Reflection::MethodAttributes) 116 (::System::Reflection::MethodAttributes::Public | 117 ::System::Reflection::MethodAttributes::HideBySig | 118 ::System::Reflection::MethodAttributes::SpecialName | 119 ::System::Reflection::MethodAttributes::RTSpecialName 120 /* | xxx todo: ??? compiler does not know Instance ??? 121 ::System::Reflection::MethodAttributes::Instance*/); 122 123 //============================================================================== 124 ref class TypeEmitter : public ::System::IDisposable 125 { 126 ::System::Reflection::Emit::ModuleBuilder ^ m_module_builder; 127 cli::array< ::System::Reflection::Assembly ^ > ^ m_extra_assemblies; 128 129 ::System::Reflection::MethodInfo ^ m_method_info_Type_GetTypeFromHandle; 130 131 ::System::Type ^ m_type_Exception; 132 ::System::Type ^ get_type_Exception(); 133 ::System::Type ^ m_type_RuntimeException; 134 ::System::Type ^ get_type_RuntimeException(); 135 136 ::System::Reflection::Emit::CustomAttributeBuilder ^ get_service_exception_attribute( 137 const css::uno::Reference<css::reflection::XServiceConstructorDescription> & ctorDesc); 138 ::System::Reflection::Emit::CustomAttributeBuilder ^ get_iface_method_exception_attribute( 139 const css::uno::Reference< css::reflection::XInterfaceMethodTypeDescription >& xMethod ); 140 ::System::Reflection::Emit::CustomAttributeBuilder ^ get_exception_attribute( 141 const css::uno::Sequence<css::uno::Reference< 142 css::reflection::XCompoundTypeDescription > >& seq_exceptionsTd ); 143 /* Creates ::System::Type object for UNO exceptions. The UNO exceptions are 144 obtained by 145 com::sun::star::reflection::XServiceConstructorDescription::getExceptions 146 In a first step the respective CLI types are created. Then it is examined 147 if a Type represents a super class of another class. If so the Type of the 148 derived class is discarded. For example there are a uno RuntimeException and 149 a DeploymentException which inherits RuntimeException. Then only the cli Type 150 of the RuntimeException is returned. 151 The purpose of this function is to provide exceptions for which catch blocks 152 are generated in the service constructor code. 153 154 It is always an instance of an ArrayList returned, even if the sequence argument 155 does not contain elements. 156 */ 157 ::System::Collections::ArrayList ^ get_service_ctor_method_exceptions_reduced( 158 const css::uno::Sequence< 159 css::uno::Reference<css::reflection::XCompoundTypeDescription> > & seqExceptionsTd); 160 161 162 // NOTE on the m_xType members below: css::reflection::* are NATIVE UNO 163 // interface pointers, not managed handles, and stay pointers. Only the 164 // System::* members become handles. 165 ref class iface_entry 166 { 167 public: 168 css::reflection::XInterfaceTypeDescription2 * m_xType; 169 ::System::Reflection::Emit::TypeBuilder ^ m_type_builder; 170 }; 171 ::System::Collections::Hashtable ^ m_incomplete_ifaces; 172 // Names type_resolve is part-way through answering. AppDomain's 173 // TypeResolve event has no re-entrancy guard of its own. 174 ::System::Collections::Hashtable ^ m_resolving; 175 ::System::Type ^ complete_iface_type( iface_entry ^ entry ); 176 177 ref class struct_entry 178 { 179 public: 180 css::reflection::XCompoundTypeDescription * m_xType; 181 ::System::Reflection::Emit::TypeBuilder ^ m_type_builder; 182 ::System::Type ^ m_base_type; 183 184 cli::array< ::System::String ^ > ^ m_member_names; 185 cli::array< ::System::Type ^ > ^ m_param_types; 186 ::System::Reflection::ConstructorInfo ^ m_default_ctor; 187 ::System::Reflection::ConstructorInfo ^ m_ctor; 188 }; 189 ::System::Collections::Hashtable ^ m_incomplete_structs; 190 ::System::Type ^ complete_struct_type( struct_entry ^ entry ); 191 192 /* returns the type for the name. If it is a struct then it may 193 complete the struct if not already done. This also refers to its 194 base types. 195 196 @param sName 197 the full name of the type. 198 @return the type object for sName. Not necessarily a struct. 199 */ 200 ::System::Type ^ get_complete_struct( ::System::String ^ sName); 201 202 ref class service_entry 203 { 204 public: 205 ::System::Reflection::Emit::TypeBuilder ^ m_type_builder; 206 css::reflection::XServiceTypeDescription2 * m_xType; 207 }; 208 ::System::Collections::Hashtable ^ m_incomplete_services; 209 ::System::Type ^ complete_service_type(service_entry ^ entry); 210 211 ref class singleton_entry 212 { 213 public: 214 ::System::Reflection::Emit::TypeBuilder ^ m_type_builder; 215 css::reflection::XSingletonTypeDescription2 * m_xType; 216 }; 217 218 219 ::System::Collections::Hashtable ^ m_incomplete_singletons; 220 ::System::Type ^ complete_singleton_type(singleton_entry ^ entry); 221 222 223 ::System::Collections::Hashtable ^ m_generated_structs; 224 225 ::System::Type ^ get_type( 226 ::System::String ^ cli_name, bool throw_exc ); 227 ::System::Type ^ get_type( 228 css::uno::Reference< 229 css::reflection::XConstantTypeDescription > const & xType ); 230 ::System::Type ^ get_type( 231 css::uno::Reference< 232 css::reflection::XConstantsTypeDescription > const & xType ); 233 ::System::Type ^ get_type( 234 css::uno::Reference< 235 css::reflection::XEnumTypeDescription > const & xType ); 236 /* returns the type for a struct or exception. In case of a polymorphic struct it may 237 return a ::uno::PolymorphicType (cli_basetypes.dll) only if the struct is already 238 complete. 239 */ 240 ::System::Type ^ get_type( 241 css::uno::Reference< 242 css::reflection::XCompoundTypeDescription > const & xType ); 243 ::System::Type ^ get_type( 244 css::uno::Reference< 245 css::reflection::XInterfaceTypeDescription2 > const & xType ); 246 ::System::Type ^ get_type( 247 css::uno::Reference< 248 css::reflection::XSingletonTypeDescription2 > const & xType ); 249 250 /* 251 May return NULL if the service description is an obsolete. See 252 description of 253 com.sun.star.reflection.XServiceTypeDescription2.isSingleInterfaceBased 254 */ 255 ::System::Type ^ get_type( 256 css::uno::Reference< 257 css::reflection::XServiceTypeDescription2 > const & xType ); 258 public: 259 TypeEmitter( 260 ::System::Reflection::Emit::ModuleBuilder ^ module_builder, 261 cli::array< ::System::Reflection::Assembly ^ > ^ assemblies ); 262 // Dispose() is reserved in a ref class -- the compiler generates it from 263 // the destructor, which is what "delete emitter" then calls. The body 264 // that used to be Dispose() lives in the destructor now. 265 ~TypeEmitter(); 266 267 ::System::Reflection::Assembly ^ type_resolve( 268 ::System::Object ^ sender, ::System::ResolveEventArgs ^ args ); 269 270 ::System::Type ^ get_type( 271 css::uno::Reference< 272 css::reflection::XTypeDescription > const & xType ); 273 }; 274 275 } 276