2 using System
.Collections
;
3 using System
.Reflection
;
4 using System
.Reflection
.Emit
;
5 using System
.Runtime
.InteropServices
;
41 public static readonly string UnoVoid
= "void";
42 public static readonly string UnoType
= "type";
43 public static readonly string UnoAny
= "any";
44 public static readonly string UnoBool
= "boolean";
45 public static readonly string UnoByte
= "byte";
46 public static readonly string UnoChar
= "char";
47 public static readonly string UnoShort
= "short";
48 public static readonly string UnoUShort
= "unsigned short";
49 public static readonly string UnoLong
= "long";
50 public static readonly string UnoULong
= "unsigned long";
51 public static readonly string UnoHyper
= "hyper";
52 public static readonly string UnoUHyper
= "unsigned hyper";
53 public static readonly string UnoString
= "string";
54 public static readonly string UnoFloat
= "float";
55 public static readonly string UnoDouble
= "double";
56 public static readonly string UnoXInterface
= "com.sun.star.uno.XInterface";
57 public static readonly string Brackets
= "[]";
59 public static readonly string Object
= "System.Object";
60 public static readonly string Type
= "System.Type";
61 public static readonly string Unoidl
= "unoidl.";
62 public static readonly string Void
= "System.Void";
63 public static readonly string Any
= "uno.Any";
64 public static readonly string Boolean
= "System.Boolean";
65 public static readonly string Char
= "System.Char";
66 public static readonly string Byte
= "System.Byte";
67 public static readonly string Int16
= "System.Int16";
68 public static readonly string UInt16
= "System.UInt16";
69 public static readonly string Int32
= "System.Int32";
70 public static readonly string UInt32
= "System.UInt32";
71 public static readonly string Int64
= "System.Int64";
72 public static readonly string UInt64
= "System.UInt64";
73 public static readonly string String
= "System.String";
74 public static readonly string Single
= "System.Single";
75 public static readonly string Double
= "System.Double";
76 public static readonly string Comma
= ",";
79 class TypeEmitter
: IDisposable
81 ResolveEventHandler mTypeResolver
;
82 ModuleBuilder mModuleBuilder
;
83 ArrayList mExtraAssemblies
;
84 Hashtable mIncompleteIFaces
;
85 Hashtable mIncompleteServices
;
86 Hashtable mIncompleteSingletons
;
87 Hashtable mGeneratedStructs
;
90 Type mTypeException
= null;
91 Type mTypeRuntimeException
= null;
93 readonly static MethodAttributes cCtorMethodAttr
=
94 MethodAttributes
.Public
|
95 MethodAttributes
.HideBySig
|
96 MethodAttributes
.SpecialName
|
97 MethodAttributes
.RTSpecialName
;
98 /* | xxx todo: ??? compiler does not know Instance ???
99 ::System::Reflection::MethodAttributes::Instance*/
101 MethodInfo mMethodInfoTypeGetTypeFromHandle
;
105 public UnoXInterfaceTypeDescription mType
;
106 public TypeBuilder mTypeBuilder
;
111 public UnoXServiceTypeDescription mType
;
112 public TypeBuilder mTypeBuilder
;
117 public UnoXSingletonTypeDescription mType
;
118 public TypeBuilder mTypeBuilder
;
123 public string[] mMemberNames
;
124 public Type
[] mParamTypes
;
125 public ConstructorInfo mDefaultConstructor
;
126 public ConstructorInfo mConstructor
;
129 public TypeEmitter (Config config
, ModuleBuilder builder
)
133 // load extra assemblies
134 mExtraAssemblies
= new ArrayList ();
135 foreach (string assemblyPath
in mConfig
.mExtraAssemblies
)
136 mExtraAssemblies
.Add (Assembly
.LoadFrom (assemblyPath
));
138 mTypeResolver
= new ResolveEventHandler (TypeResolveHandler
);
139 mModuleBuilder
= builder
;
141 mIncompleteIFaces
= new Hashtable ();
142 mIncompleteServices
= new Hashtable ();
143 mIncompleteSingletons
= new Hashtable ();
144 mGeneratedStructs
= new Hashtable ();
146 Type
[] paramTypes
= { typeof (RuntimeTypeHandle) }
;
147 mMethodInfoTypeGetTypeFromHandle
= typeof (Type
).GetMethod ("GetTypeFromHandle", paramTypes
);
150 public ResolveEventHandler ResolveEventHandler
154 return mTypeResolver
;
158 Assembly
TypeResolveHandler (object o
, ResolveEventArgs args
)
160 Type ret
= mModuleBuilder
.GetType (args
.Name
, false);
161 //Console.WriteLine ("mModuleBuilder.GetType yields {0}", ret);
164 if (ret
is TypeBuilder
) {
165 TypeBuilder tb
= ret
as TypeBuilder
;
166 //Console.WriteLine ("{0} is type builder", tb);
167 if (tb
.IsCreated ()) {
168 ret
= tb
.CreateType ();
169 //Console.WriteLine ("resolving to created {0} {1}", ret, tb);
174 IFaceEntry entry
= mIncompleteIFaces
[args
.Name
] as IFaceEntry
;
176 ret
= entry
.mTypeBuilder
;
179 if (ret
== null && mExtraAssemblies
!= null) {
180 //Console.WriteLine ("assemblies {0}", mExtraAssemblies);
181 foreach (Assembly assembly
in mExtraAssemblies
) {
182 ret
= assembly
.GetType (args
.Name
, false);
184 if (mConfig
.mVerbose
) {
185 Console
.WriteLine ("> resolving type {0} from {1}.",
186 args
.Name
, ret
.Assembly
.FullName
);
191 //Console.WriteLine ("done {0}", ret);
200 Type
GetType (string name
, bool throwExc
)
202 Type ret
= mModuleBuilder
.GetType (name
, false);
203 //Console.WriteLine ("mModuleBuilder.GetType yields {0}", ret);
206 if (ret
is TypeBuilder
) {
207 TypeBuilder tb
= ret
as TypeBuilder
;
208 //Console.WriteLine ("{0} is type builder", tb);
209 if (tb
.IsCreated ()) {
210 ret
= tb
.CreateType ();
211 //Console.WriteLine ("resolving to created {0} {1}", ret, tb);
216 //Console.WriteLine ("looking name {0}", name);
217 IFaceEntry entry
= mIncompleteIFaces
[name
] as IFaceEntry
;
219 ret
= entry
.mTypeBuilder
;
222 //try the cli_basetypes assembly
224 ret
= Type
.GetType (name
+ ",cli_basetypes");
229 // may call on type_resolve()
230 return Type
.GetType (name
, throwExc
);
231 } catch (Exception e
) {
232 //If the type is not found one may have forgotten to specify assemblies with
235 throw new Exception ("\nThe type " + name
+ " \n could not be found. Did you forget to " +
236 "specify an additional assembly with the --reference option?\n", e
);
243 public Type
GetType (UnoXEnumTypeDescription xtd
)
245 //Console.WriteLine ("emit enum {0} {1}", xtd.Name, xtd.Length);
246 string name
= "unoidl." + xtd
.Name
;
248 Type ret
= GetType (name
, false /* no exc */);
250 // Emit::EnumBuilder * enum_builder =
251 // m_module_builder->DefineEnum(
253 // (TypeAttributes) (TypeAttributes::Public |
254 // // TypeAttributes::Sealed |
255 // TypeAttributes::AnsiClass),
256 // __typeof (::System::Int32) );
257 // workaround enum builder bug
258 TypeBuilder enumBuilder
=
259 mModuleBuilder
.DefineType (name
,
260 TypeAttributes
.Public
|
261 TypeAttributes
.Sealed
,
262 typeof (System
.Enum
));
263 enumBuilder
.DefineField ("value__", typeof (System
.Int32
),
264 FieldAttributes
.Public
|
265 FieldAttributes
.SpecialName
|
266 FieldAttributes
.RTSpecialName
);
268 int length
= xtd
.Length
;
269 for (int pos
= 0; pos
< length
; pos
++) {
270 // enum_builder->DefineLiteral(
271 // ustring_to_String( enum_names[ enum_pos ] ),
272 // __box ((::System::Int32) enum_values[ enum_pos ]) );
273 FieldBuilder fieldBuilder
=
274 enumBuilder
.DefineField (xtd
.ValueName (pos
),
276 FieldAttributes
.Public
|
277 FieldAttributes
.Static
|
278 FieldAttributes
.Literal
);
279 fieldBuilder
.SetConstant (xtd
.Value (pos
));
282 if (mConfig
.mVerbose
)
283 Console
.WriteLine ("> emitting enum type {0}", name
);
285 ret
= enumBuilder
.CreateType ();
291 public Type
GetType (UnoXInterfaceTypeDescription xtd
)
293 //Console.WriteLine ("get iface {0}", xtd.Name);
295 if (String
.Compare (xtd
.Name
, "com.sun.star.uno.XInterface") == 0) {
296 return typeof (object);
299 string name
= "unoidl." + xtd
.Name
;
301 Type ret
= GetType (name
, false /* no exc */);
304 //Console.WriteLine ("creating name {0}", name);
305 TypeBuilder typeBuilder
;
307 TypeAttributes attr
= TypeAttributes
.Public
|
308 TypeAttributes
.Interface
|
309 TypeAttributes
.Abstract
|
310 TypeAttributes
.AnsiClass
;
312 int length
= xtd
.BaseTypes
;
314 ArrayList ifaces
= new ArrayList ();
317 for (i
= 0; i
< length
; i
++) {
318 UnoXInterfaceTypeDescription baseType
= xtd
.BaseType (i
);
319 if (String
.Compare (baseType
.Name
, "com.sun.star.uno.XInterface") != 0)
320 ifaces
.Add (baseType
);
322 Type
[] baseInterfaces
= new Type
[ifaces
.Count
];
325 foreach (UnoXInterfaceTypeDescription iface
in ifaces
) {
326 baseInterfaces
[i
] = GetType (iface
);
330 typeBuilder
= mModuleBuilder
.DefineType (name
, attr
, null, baseInterfaces
);
332 System
.Console
.WriteLine ("warning: IDL interface {0} is not derived from " +
333 "com.sun.star.uno.XInterface!", name
);
335 typeBuilder
= mModuleBuilder
.DefineType (name
, attr
);
338 // insert to be completed
339 IFaceEntry entry
= new IFaceEntry ();
341 entry
.mTypeBuilder
= typeBuilder
;
342 mIncompleteIFaces
[name
] = entry
;
344 // type is incomplete
351 string PolymorphicStructNameToStructName (string name
)
353 if (!name
.EndsWith (">"))
356 int index
= name
.IndexOf ('<');
361 return name
.Substring (0, index
);
364 Type
TypeException ()
366 if (mTypeException
== null) {
367 mTypeException
= GetType ("unoidl.com.sun.star.uno.Exception", false);
369 if (mTypeException
== null) {
370 // define hardcoded type unoidl.com.sun.star.uno.Exception
371 TypeBuilder typeBuilder
= mModuleBuilder
.DefineType ("unoidl.com.sun.star.uno.Exception",
372 TypeAttributes
.Public
|
373 TypeAttributes
.BeforeFieldInit
|
374 TypeAttributes
.AnsiClass
,
375 typeof (System
.Exception
));
376 FieldBuilder fieldContext
= typeBuilder
.DefineField ("Context", typeof (object),
377 FieldAttributes
.Public
);
379 typeBuilder
.DefineDefaultConstructor (cCtorMethodAttr
);
381 Type
[] paramTypes
= new Type
[2];
382 paramTypes
[0] = typeof (string);
383 paramTypes
[1] = typeof (object);
384 ConstructorBuilder ctorBuilder
= typeBuilder
.DefineConstructor (cCtorMethodAttr
,
385 CallingConventions
.Standard
,
387 ctorBuilder
.DefineParameter (1, ParameterAttributes
.In
, "Message");
388 ctorBuilder
.DefineParameter (2, ParameterAttributes
.In
, "Context");
389 ILGenerator code
= ctorBuilder
.GetILGenerator ();
390 code
.Emit (OpCodes
.Ldarg_0
);
391 code
.Emit (OpCodes
.Ldarg_1
);
392 paramTypes
= new Type
[1];
393 paramTypes
[0] = typeof (string);
394 code
.Emit (OpCodes
.Call
, typeof (System
.Exception
).GetConstructor (paramTypes
));
395 code
.Emit( OpCodes
.Ldarg_0
);
396 code
.Emit( OpCodes
.Ldarg_2
);
397 code
.Emit( OpCodes
.Stfld
, fieldContext
);
398 code
.Emit( OpCodes
.Ret
);
400 if (mConfig
.mVerbose
)
401 Console
.WriteLine ("> emitting exception type unoidl.com.sun.star.uno.Exception");
403 mTypeException
= typeBuilder
.CreateType ();
407 return mTypeException
;
410 Type
TypeRuntimeException ()
412 if (mTypeRuntimeException
== null) {
413 mTypeRuntimeException
= GetType ("unoidl.com.sun.star.uno.RuntimeException", false);
414 if (mTypeRuntimeException
== null) {
415 // define hardcoded type unoidl.com.sun.star.uno.RuntimeException
416 Type typeException
= TypeException ();
417 TypeBuilder typeBuilder
= mModuleBuilder
.DefineType ("unoidl.com.sun.star.uno.RuntimeException",
418 TypeAttributes
.Public
|
419 TypeAttributes
.BeforeFieldInit
|
420 TypeAttributes
.AnsiClass
,
423 typeBuilder
.DefineDefaultConstructor (cCtorMethodAttr
);
425 Type
[] paramTypes
= new Type
[2];
426 paramTypes
[0] = typeof (string);
427 paramTypes
[1] = typeof (object);
428 ConstructorBuilder ctorBuilder
= typeBuilder
.DefineConstructor (cCtorMethodAttr
,
429 CallingConventions
.Standard
,
431 ctorBuilder
.DefineParameter (1, ParameterAttributes
.In
, "Message");
432 ctorBuilder
.DefineParameter (2, ParameterAttributes
.In
, "Context");
433 ILGenerator code
= ctorBuilder
.GetILGenerator ();
434 code
.Emit (OpCodes
.Ldarg_0
);
435 code
.Emit (OpCodes
.Ldarg_1
);
436 code
.Emit (OpCodes
.Ldarg_2
);
437 code
.Emit (OpCodes
.Call
,
438 typeException
.GetConstructor (paramTypes
));
439 code
.Emit (OpCodes
.Ret
);
441 if (mConfig
.mVerbose
)
442 Console
.WriteLine ("> emitting exception type unoidl.com.sun.star.uno.RuntimeException");
444 mTypeRuntimeException
= typeBuilder
.CreateType ();
448 return mTypeRuntimeException
;
451 public Type
GetType (UnoXServiceTypeDescription xtd
)
453 // Console.WriteLine ("get service {0}", xtd.Name);
455 if (!xtd
.IsSingleInterfaceBased
)
458 string name
= "unoidl." + xtd
.Name
;
460 Type ret
= GetType (name
, false /* no exc */);
465 TypeAttributes attr
=
466 TypeAttributes
.Public
|
467 TypeAttributes
.Sealed
|
468 TypeAttributes
.BeforeFieldInit
|
469 TypeAttributes
.AnsiClass
;
471 // insert to be completed
472 ServiceEntry entry
= new ServiceEntry ();
474 entry
.mTypeBuilder
= mModuleBuilder
.DefineType (name
, attr
);
475 mIncompleteServices
.Add (name
, entry
);
477 return entry
.mTypeBuilder
;
480 static void EmitLdarg (ILGenerator code
, int index
)
484 code
.Emit( OpCodes
.Ldarg_0
);
487 code
.Emit( OpCodes
.Ldarg_1
);
490 code
.Emit( OpCodes
.Ldarg_2
);
493 code
.Emit( OpCodes
.Ldarg_3
);
497 code
.Emit (OpCodes
.Ldarg_S
, (byte) index
);
498 else if (index
< 0x8000)
499 code
.Emit (OpCodes
.Ldarg_S
, (System
.Int16
) index
);
501 code
.Emit (OpCodes
.Ldarg
, index
);
506 /** For example, there is a uno type
507 com.sun.star.Foo<char, long>.
508 The values in the type list
509 are uno types and are replaced by cli types, such as System.Char,
512 Strings can be as complicated as this
513 test.MyStruct<char,test.MyStruct<long, []string>>
515 string MapUnoPolymorphicName (string unoName
)
517 int index
= unoName
.IndexOf('<');
521 System
.Text
.StringBuilder builder
= new System
.Text
.StringBuilder (unoName
.Substring (0, index
+ 1));
523 //Find the first occurrence of ','
524 //If the parameter is a polymorphic struct then we neede to ignore everything
525 //between the brackets because it can also contain commas
526 //get the type list within < and >
527 int endIndex
= unoName
.Length
- 1;
531 while (cur
<= endIndex
) {
532 char c
= unoName
[cur
];
534 if (c
== ',' || c
== '>') {
535 //insert a comma if needed
536 if (countParams
!= 0)
537 builder
.Append (",");
539 string param
= unoName
.Substring (index
, cur
- index
);
542 //the the index to the beginning of the next param
544 builder
.Append (MapUnoTypeName (param
));
545 } else if (c
== '<') {
547 //continue until the matching '>'
550 char curChar
= unoName
[cur
];
553 else if (curChar
== '>') {
564 builder
.Append ('>');
565 return builder
.ToString();
568 string MapUnoTypeName (string typeName
)
570 System
.Text
.StringBuilder buf
= new System
.Text
.StringBuilder ();
571 string unoName
= String
.Copy (typeName
);
573 //determine if the type is a sequence and its dimensions
575 if (typeName
.StartsWith ("[")) {
578 if (typeName
[index
++] == ']')
580 if (typeName
[index
++] != '[')
583 unoName
= unoName
.Substring (index
- 1);
586 if (unoName
.Equals (Constants
.UnoBool
))
587 buf
.Append (Constants
.Boolean
);
588 else if (unoName
.Equals(Constants
.UnoChar
))
589 buf
.Append (Constants
.Char
);
590 else if (unoName
.Equals(Constants
.UnoByte
))
591 buf
.Append (Constants
.Byte
);
592 else if (unoName
.Equals(Constants
.UnoShort
))
593 buf
.Append (Constants
.Int16
);
594 else if (unoName
.Equals(Constants
.UnoUShort
))
595 buf
.Append (Constants
.UInt16
);
596 else if (unoName
.Equals(Constants
.UnoLong
))
597 buf
.Append (Constants
.Int32
);
598 else if (unoName
.Equals(Constants
.UnoULong
))
599 buf
.Append (Constants
.UInt32
);
600 else if (unoName
.Equals(Constants
.UnoHyper
))
601 buf
.Append (Constants
.Int64
);
602 else if (unoName
.Equals(Constants
.UnoUHyper
))
603 buf
.Append (Constants
.UInt64
);
604 else if (unoName
.Equals(Constants
.UnoFloat
))
605 buf
.Append (Constants
.Single
);
606 else if (unoName
.Equals(Constants
.UnoDouble
))
607 buf
.Append (Constants
.Double
);
608 else if (unoName
.Equals(Constants
.UnoString
))
609 buf
.Append (Constants
.String
);
610 else if (unoName
.Equals(Constants
.UnoVoid
))
611 buf
.Append (Constants
.Void
);
612 else if (unoName
.Equals(Constants
.UnoType
))
613 buf
.Append (Constants
.Type
);
614 else if (unoName
.Equals(Constants
.UnoXInterface
))
615 buf
.Append (Constants
.Object
);
616 else if (unoName
.Equals(Constants
.UnoAny
)) {
617 buf
.Append (Constants
.Any
);
619 //put "unoidl." at the beginning
620 buf
.Append (Constants
.Unoidl
);
621 buf
.Append (MapUnoPolymorphicName (unoName
));
626 buf
.Append (Constants
.Brackets
);
628 // Console.WriteLine ("MapUnoTypeName {0} => {1}", typeName, buf.ToString ());
630 return buf
.ToString();
633 public Type
GetType (UnoXCompoundTypeDescription xtd
)
635 // Console.WriteLine ("get compound type {0}", xtd.Name);
637 if (xtd
.TypeClass
== UnoTypeClass
.Exception
) {
638 if (xtd
.Name
.Equals ("com.sun.star.uno.Exception"))
639 return TypeException ();
641 if (xtd
.Name
.Equals ("com.sun.star.uno.RuntimeException"))
642 return TypeRuntimeException ();
645 string name
= "unoidl." + xtd
.Name
;
647 // if the struct is an instantiated polymorpic struct then we create the simple struct name
649 // void func ([in] PolyStruct<boolean> arg);
650 // PolyStruct<boolean> will be converted to PolyStruct
651 name
= PolymorphicStructNameToStructName (name
);
653 Type ret
= GetType (name
, false /* no exc */);
654 UnoXStructTypeDescription xsd
= xtd
as UnoXStructTypeDescription
;
657 // Console.WriteLine ("create compound type {0}", name);
658 UnoXTypeDescription baseTD
= xtd
.BaseType
;
659 Type baseType
= baseTD
!= null ? GetType (baseTD
) : typeof (object);
660 CustomAttributeBuilder attrBuilder
;
661 TypeBuilder typeBuilder
= mModuleBuilder
.DefineType (name
,
662 TypeAttributes
.Public
|
663 TypeAttributes
.BeforeFieldInit
|
664 TypeAttributes
.AnsiClass
,
668 // Polymorphic struct, define uno.TypeParametersAttribute
669 // A polymorphic struct cannot have a basetype.
670 // When we create the template of the struct then we have no exact types
671 // and the name does not contain a parameter list
672 if (xsd
!= null && xsd
.TypeParameters
> 0) {
673 object[] args
= new object [xsd
.TypeParameters
];
675 for (i
= 0; i
< xsd
.TypeParameters
; i
++)
676 args
[i
] = xsd
.TypeParameter (i
);
677 object[] aargs
= { args }
;
679 Type
[] typesCtor
= { typeof (string[]) }
;
680 attrBuilder
= new CustomAttributeBuilder (typeof (uno
.TypeParametersAttribute
).GetConstructor (typesCtor
), aargs
);
681 typeBuilder
.SetCustomAttribute (attrBuilder
);
684 // optional: lookup base type whether generated entry of this session
685 StructEntry baseTypeEntry
= null;
686 if (baseType
!= null)
687 baseTypeEntry
= (StructEntry
) mGeneratedStructs
[baseType
.FullName
];
690 for (i
= 0; i
< xtd
.MemberTypes
; i
++) {
691 if (xtd
.MemberType (i
) == null)
692 throw new Exception ("Missing type description . Check if you need to " +
693 "specify additional RDBs with the --extra option. Type missing for: " +
694 xtd
.Name
+ "::" + xtd
.MemberName (i
));
697 // collect base types; wrong order
698 ArrayList baseTypes
= new ArrayList (3);
699 int allMembersLength
= 0;
700 for (Type baseTypePos
= baseType
; !baseTypePos
.Equals (typeof (object)); baseTypePos
= baseTypePos
.BaseType
) {
701 baseTypes
.Add (baseTypePos
);
702 if (baseTypePos
.Equals (typeof (System
.Exception
))) {
703 // special Message member
705 break; // don't include System.Exception base classes
707 allMembersLength
+= baseTypePos
.GetFields (BindingFlags
.Instance
|
708 BindingFlags
.Public
|
709 BindingFlags
.DeclaredOnly
).Length
;
713 // create all_members arrays; right order
714 string[] allMemberNames
= new string [allMembersLength
+ xtd
.MemberTypes
];
715 Type
[] allParamTypes
= new Type
[allMembersLength
+ xtd
.MemberTypes
];
717 for (i
= baseTypes
.Count
- 1; i
>= 0; i
--) {
718 Type type
= baseTypes
[i
] as Type
;
720 if (type
.Equals (typeof (System
.Exception
))) {
721 allMemberNames
[memberPos
] = "Message";
722 allParamTypes
[memberPos
] = typeof (string);
725 StructEntry baseEntry
= mGeneratedStructs
[type
.FullName
] as StructEntry
;
726 if (baseEntry
== null) {
728 FieldInfo
[] fields
= type
.GetFields (BindingFlags
.Instance
|
729 BindingFlags
.Public
|
730 BindingFlags
.DeclaredOnly
);
731 foreach (FieldInfo fieldInfo
in fields
) {
732 allMemberNames
[memberPos
] = fieldInfo
.Name
;
733 allParamTypes
[memberPos
] = fieldInfo
.FieldType
;
737 // generated during this session:
738 // members may be incomplete ifaces
740 for (j
= 0; j
< baseEntry
.mParamTypes
.Length
; j
++) {
741 allMemberNames
[memberPos
] = baseEntry
.mMemberNames
[j
];
742 allParamTypes
[memberPos
] = baseEntry
.mParamTypes
[j
];
748 if (allMembersLength
!= memberPos
)
749 throw new Exception ("something went wrong, allMembersLength != memberPos");
753 StructEntry entry
= new StructEntry ();
754 entry
.mMemberNames
= new string [xtd
.MemberTypes
];
755 entry
.mParamTypes
= new Type
[xtd
.MemberTypes
];
758 FieldBuilder
[] members
= new FieldBuilder
[xtd
.MemberTypes
];
759 int curParamIndex
= 0;
760 int typeParamPos
= 0;
762 for (memberPos
= 0; memberPos
< xtd
.MemberTypes
; memberPos
++) {
763 string fieldName
= xtd
.MemberName (memberPos
);
766 //Special handling of struct parameter types
767 bool parameterizedType
= false;
768 if (xtd
.MemberType (memberPos
).TypeClass
== UnoTypeClass
.Unknown
) {
769 parameterizedType
= true;
770 if (xsd
!= null && typeParamPos
< xsd
.TypeParameters
) {
771 fieldType
= typeof (object);
774 throw new Exception ("unexpected member type in " + xtd
.Name
);
777 fieldType
= GetType (xtd
.MemberType (memberPos
));
779 members
[memberPos
] = typeBuilder
.DefineField (fieldName
, fieldType
, FieldAttributes
.Public
);
781 //parameterized type (polymorphic struct) ?
782 if (parameterizedType
&& xsd
!= null) {
783 object[] args
= { xsd.TypeParameter (curParamIndex ++) }
;
784 Type
[] ctorTypes
= { typeof (string) }
;
785 attrBuilder
= new CustomAttributeBuilder (typeof (uno
.ParameterizedTypeAttribute
).GetConstructor (ctorTypes
), args
);
786 members
[memberPos
].SetCustomAttribute (attrBuilder
);
789 // add to all_members
790 allMemberNames
[allMembersLength
+ memberPos
] = fieldName
;
791 allParamTypes
[allMembersLength
+ memberPos
] = fieldType
;
794 entry
.mMemberNames
[memberPos
] = fieldName
;
795 entry
.mParamTypes
[memberPos
] = fieldType
;
798 allMembersLength
+= xtd
.MemberTypes
;
801 ConstructorBuilder ctorBuilder
= typeBuilder
.DefineConstructor (cCtorMethodAttr
, CallingConventions
.Standard
, new Type
[0]);
802 ILGenerator code
= ctorBuilder
.GetILGenerator ();
804 code
.Emit (OpCodes
.Ldarg_0
);
805 // Console.WriteLine ("baseType: {0}", baseType);
806 code
.Emit (OpCodes
.Call
, baseTypeEntry
== null ? baseType
.GetConstructor (new Type
[0]) : baseTypeEntry
.mDefaultConstructor
);
808 // default initialize members
809 for (memberPos
= 0; memberPos
< xtd
.MemberTypes
; memberPos
++) {
810 FieldInfo fieldInfo
= members
[memberPos
];
812 // default initialize
813 // string, type, enum, sequence, struct, exception, any
814 if (fieldInfo
.FieldType
.Equals (typeof (string))) {
815 code
.Emit (OpCodes
.Ldarg_0
);
816 code
.Emit (OpCodes
.Ldstr
, "");
817 code
.Emit (OpCodes
.Stfld
, fieldInfo
);
818 } else if (fieldInfo
.FieldType
.Equals (typeof (Type
))) {
819 code
.Emit (OpCodes
.Ldarg_0
);
820 code
.Emit (OpCodes
.Ldtoken
, typeof (void));
821 code
.Emit (OpCodes
.Call
, mMethodInfoTypeGetTypeFromHandle
);
822 code
.Emit (OpCodes
.Stfld
, fieldInfo
);
823 } else if (fieldInfo
.FieldType
.IsArray
) {
824 code
.Emit (OpCodes
.Ldarg_0
);
825 code
.Emit (OpCodes
.Ldc_I4_0
);
826 code
.Emit (OpCodes
.Newarr
, fieldInfo
.FieldType
.GetElementType ());
827 code
.Emit (OpCodes
.Stfld
, fieldInfo
);
828 } else if (fieldInfo
.FieldType
.IsValueType
) {
829 if (fieldInfo
.FieldType
.FullName
.Equals ("uno.Any")) {
830 code
.Emit (OpCodes
.Ldarg_0
);
831 code
.Emit (OpCodes
.Ldsfld
, typeof (uno
.Any
).GetField ("VOID"));
832 code
.Emit (OpCodes
.Stfld
, fieldInfo
);
834 } else if (fieldInfo
.FieldType
.IsClass
) {
836 if (!fieldInfo
.FieldType
.Equals (typeof (object))) {
837 code
.Emit (OpCodes
.Ldarg_0
);
838 code
.Emit (OpCodes
.Newobj
, fieldInfo
.FieldType
.GetConstructor (new Type
[0]));
839 code
.Emit (OpCodes
.Stfld
, fieldInfo
);
844 code
.Emit (OpCodes
.Ret
);
845 entry
.mDefaultConstructor
= ctorBuilder
;
847 // parameterized .ctor including all base members
848 ctorBuilder
= typeBuilder
.DefineConstructor (cCtorMethodAttr
, CallingConventions
.Standard
, allParamTypes
);
849 for (memberPos
= 0; memberPos
< allMembersLength
; memberPos
++)
850 ctorBuilder
.DefineParameter (memberPos
+ 1, ParameterAttributes
.In
, allMemberNames
[memberPos
]);
852 code
= ctorBuilder
.GetILGenerator ();
854 code
.Emit (OpCodes
.Ldarg_0
); // push this
855 int baseMembersLength
= allMembersLength
- xtd
.MemberTypes
;
856 Type
[] paramTypes
= new Type
[baseMembersLength
];
858 for (memberPos
= 0; memberPos
< baseMembersLength
; memberPos
++) {
859 EmitLdarg (code
, memberPos
+ 1);
860 paramTypes
[memberPos
] = allParamTypes
[memberPos
];
863 code
.Emit (OpCodes
.Call
, baseTypeEntry
== null ? baseType
.GetConstructor (paramTypes
) : baseTypeEntry
.mConstructor
);
865 // initialize members
867 for (memberPos
= 0; memberPos
< xtd
.MemberTypes
; memberPos
++) {
868 code
.Emit (OpCodes
.Ldarg_0
); // push this
869 EmitLdarg (code
, memberPos
+ baseMembersLength
+ 1);
870 code
.Emit (OpCodes
.Stfld
, members
[memberPos
]);
873 code
.Emit (OpCodes
.Ret
);
874 entry
.mConstructor
= ctorBuilder
;
876 if (mConfig
.mVerbose
)
877 Console
.WriteLine ("> emitting {0} type {1}", xtd
is UnoXStructTypeDescription
? "struct" : "exception", name
);
880 mGeneratedStructs
.Add (name
, entry
);
881 //Console.WriteLine ("added entry to mGeneratedStructs: {0}", name);
882 //if (baseTD != null)
883 //Console.WriteLine ("baseTD: {0}", baseTD.Name, GetType (baseTD).Name);
884 ret
= typeBuilder
.CreateType ();
887 // In case of an instantiated polymorphic struct we want to return a
888 // uno.PolymorphicType (inherits Type) rather then Type.
889 if (xsd
!= null && xsd
.TypeArguments
> 0) {
890 //Console.WriteLine ("polymorphic struct: call uno.PolymorphicType.GetType ({0}, {1})", ret, xtd.Name);
891 ret
= uno
.PolymorphicType
.GetType (ret
, MapUnoTypeName (xtd
.Name
));
892 //Console.WriteLine ("polymorphic struct: {0} ({1})", ret, xtd.Name);
898 public Type
GetType (UnoXConstantTypeDescription xtd
)
900 //Console.WriteLine ("get constant type {0}", xtd.Name);
902 string name
= "unoidl." + xtd
.Name
;
904 Type ret
= GetType (name
, false /* no exc */);
907 object constant
= xtd
.ConstantValue
;
909 TypeBuilder typeBuilder
= mModuleBuilder
.DefineType (name
,
910 TypeAttributes
.Public
|
911 TypeAttributes
.Sealed
|
912 TypeAttributes
.BeforeFieldInit
|
913 TypeAttributes
.AnsiClass
);
914 FieldBuilder field
= typeBuilder
.DefineField (name
.Substring (name
.LastIndexOf ('.') + 1),
916 FieldAttributes
.Public
|
917 FieldAttributes
.Static
|
918 FieldAttributes
.Literal
);
919 field
.SetConstant (constant
);
921 if (mConfig
.mVerbose
)
922 Console
.WriteLine ("> emitting constant type {0}", name
);
924 ret
= typeBuilder
.CreateType ();
930 public Type
GetType (UnoXConstantsTypeDescription xtd
)
932 //Console.WriteLine ("get constants type {0}", xtd.Name);
934 string name
= "unoidl." + xtd
.Name
;
936 Type ret
= GetType (name
, false /* no exc */);
939 TypeBuilder typeBuilder
= mModuleBuilder
.DefineType (name
,
940 TypeAttributes
.Public
|
941 TypeAttributes
.Sealed
|
942 TypeAttributes
.BeforeFieldInit
|
943 TypeAttributes
.AnsiClass
);
945 for (i
= 0; i
< xtd
.Constants
; i
++) {
946 UnoXConstantTypeDescription constantTD
= xtd
.Constant (i
);
947 object constant
= constantTD
.ConstantValue
;
948 FieldBuilder field
= typeBuilder
.DefineField (constantTD
.Name
.Substring (constantTD
.Name
.LastIndexOf ('.') + 1),
950 FieldAttributes
.Public
|
951 FieldAttributes
.Static
|
952 FieldAttributes
.Literal
);
953 field
.SetConstant (constant
);
956 if (mConfig
.mVerbose
)
957 Console
.WriteLine ( "> emitting constants group type {0}", name
);
959 ret
= typeBuilder
.CreateType ();
965 public Type
GetType (UnoXSingletonTypeDescription xtd
)
967 //Console.WriteLine ("get singleton {0}", xtd.Name);
969 if (!xtd
.IsInterfaceBased
)
972 //Console.WriteLine ("singleton {0} is interface based", xtd.Name);
974 string name
= "unoidl." + xtd
.Name
;
976 Type ret
= GetType (name
, false /* no exc */);
981 TypeAttributes attr
=
982 TypeAttributes
.Public
|
983 TypeAttributes
.Sealed
|
984 TypeAttributes
.BeforeFieldInit
|
985 TypeAttributes
.AnsiClass
;
987 // insert to be completed
988 SingletonEntry entry
= new SingletonEntry ();
990 entry
.mTypeBuilder
= mModuleBuilder
.DefineType (name
, attr
);
991 mIncompleteSingletons
.Add (name
, entry
);
993 return entry
.mTypeBuilder
;
996 public Type
GetType (UnoXTypeDescription xtd
)
998 UnoTypeClass tc
= xtd
.TypeClass
;
1001 case UnoTypeClass
.Void
:
1002 return typeof (void);
1003 case UnoTypeClass
.Char
:
1004 return typeof (char);
1005 case UnoTypeClass
.Boolean
:
1006 return typeof (bool);
1007 case UnoTypeClass
.Byte
:
1008 return typeof (byte);
1009 case UnoTypeClass
.Short
:
1010 return typeof (Int16
);
1011 case UnoTypeClass
.UnsignedShort
:
1012 return typeof (UInt16
);
1013 case UnoTypeClass
.Long
:
1014 return typeof (Int32
);
1015 case UnoTypeClass
.UnsignedLong
:
1016 return typeof (UInt32
);
1017 case UnoTypeClass
.Hyper
:
1018 return typeof (Int64
);
1019 case UnoTypeClass
.UnsignedHyper
:
1020 return typeof (UInt64
);
1021 case UnoTypeClass
.Float
:
1022 return typeof (Single
);
1023 case UnoTypeClass
.Double
:
1024 return typeof (double);
1025 case UnoTypeClass
.String
:
1026 return typeof (string);
1027 case UnoTypeClass
.Type
:
1028 return typeof (Type
);
1029 case UnoTypeClass
.Any
:
1030 return typeof (uno
.Any
);
1031 case UnoTypeClass
.Enum
:
1032 return GetType (xtd
as UnoXEnumTypeDescription
);
1033 case UnoTypeClass
.Interface
:
1034 return GetType (xtd
as UnoXInterfaceTypeDescription
);
1035 case UnoTypeClass
.Struct
:
1036 case UnoTypeClass
.Exception
:
1037 return GetType (xtd
as UnoXCompoundTypeDescription
);
1038 case UnoTypeClass
.Module
:
1040 case UnoTypeClass
.Sequence
:
1041 Type elementType
= GetType ((xtd
as UnoXIndirectTypeDescription
).ReferencedType
);
1042 Type retType
= GetType (elementType
.FullName
+ "[]", true);
1043 uno
.PolymorphicType polyType
= elementType
as uno
.PolymorphicType
;
1044 if (polyType
!= null) {
1045 string name
= polyType
.PolymorphicName
+ "[]";
1046 retType
= uno
.PolymorphicType
.GetType (retType
, name
);
1049 case UnoTypeClass
.Typedef
:
1050 return GetType ((xtd
as UnoXIndirectTypeDescription
).ReferencedType
);
1051 case UnoTypeClass
.Constant
:
1052 return GetType (xtd
as UnoXConstantTypeDescription
);
1053 case UnoTypeClass
.Constants
:
1054 return GetType (xtd
as UnoXConstantsTypeDescription
);
1055 case UnoTypeClass
.Service
:
1056 return GetType (xtd
as UnoXServiceTypeDescription
);
1057 case UnoTypeClass
.Singleton
:
1058 return GetType (xtd
as UnoXSingletonTypeDescription
);
1060 // fixme, use double for unfinished types
1061 //Console.WriteLine ("warning: unfinished type reached: {0}", xtd.Name);
1062 return typeof (void);
1065 //throw new Exception (String.Format ("Unknown type requested {0}", tc));
1068 CustomAttributeBuilder
IFaceMethodExceptionAttribute (UnoXInterfaceMethodTypeDescription method
)
1070 Type
[] exceptionTypes
= new Type
[method
.ExceptionTypes
];
1073 for (i
= 0; i
< method
.ExceptionTypes
; i
++) {
1074 exceptionTypes
[i
] = GetType (method
.ExceptionType (i
));
1077 return ExceptionAttribute (exceptionTypes
);
1080 CustomAttributeBuilder
ExceptionAttribute (Type
[] exceptionTypes
)
1082 CustomAttributeBuilder attrBuilder
= null;
1084 if (exceptionTypes
.Length
> 0) {
1085 object[] args
= { exceptionTypes }
;
1086 Type
[] arTypesCtor
= { typeof (Type[]) }
;
1087 ConstructorInfo ctorInfo
= typeof (uno
.ExceptionAttribute
).GetConstructor (arTypesCtor
);
1088 attrBuilder
= new CustomAttributeBuilder (ctorInfo
, args
);
1094 Type
[] GetTypes (UnoXTypeDescription
[] tds
)
1096 Type
[] types
= new Type
[tds
.Length
];
1099 for (i
= 0; i
< tds
.Length
; i
++)
1100 types
[i
] = GetType (tds
[i
]);
1105 Type
CompleteIFaceType (IFaceEntry entry
)
1107 //Console.WriteLine ("going to complete {0}", entry.mTypeBuilder.FullName);
1109 // complete base interfaces first
1110 foreach (Type type
in entry
.mTypeBuilder
.GetInterfaces ()) {
1111 IFaceEntry baseEntry
= mIncompleteIFaces
[type
.FullName
] as IFaceEntry
;
1112 if (baseEntry
!= null)
1113 CompleteIFaceType (baseEntry
);
1117 const MethodAttributes methodAttr
=
1118 MethodAttributes
.Public
|
1119 MethodAttributes
.Abstract
|
1120 MethodAttributes
.Virtual
|
1121 MethodAttributes
.NewSlot
|
1122 MethodAttributes
.HideBySig
;
1125 for (i
= 0; i
< entry
.mType
.Members
; i
++) {
1126 UnoXInterfaceMemberTypeDescription member
= entry
.mType
.Member (i
);
1127 MethodBuilder methodBuilder
;
1128 CustomAttributeBuilder attrBuilder
;
1130 if (member
.TypeClass
== UnoTypeClass
.InterfaceMethod
) {
1131 UnoXInterfaceMethodTypeDescription method
= new UnoXInterfaceMethodTypeDescription (member
.Handle
);
1132 UnoXMethodParameter
[] parameters
= new UnoXMethodParameter
[method
.Parameters
];
1133 Type
[] pTypes
= new Type
[method
.Parameters
];
1136 //Console.WriteLine ("method {0}.{1}", entry.mTypeBuilder.FullName, method.MemberName);
1138 // first determine all types
1139 for (j
= 0; j
< method
.Parameters
; j
++) {
1140 parameters
[j
] = method
.Parameter (j
);
1141 pTypes
[j
] = GetType (parameters
[j
].Type
);
1142 if (parameters
[j
].IsOut
)
1143 pTypes
[j
] = GetType (pTypes
[j
].FullName
+ "&", true);
1147 methodBuilder
= entry
.mTypeBuilder
.DefineMethod (method
.MemberName
,
1149 GetType (method
.ReturnType
),
1152 // then define parameter infos
1153 for (j
= 0; j
< method
.Parameters
; j
++) {
1154 ParameterAttributes attr
= 0;
1155 if (parameters
[j
].IsIn
)
1156 attr
|= ParameterAttributes
.In
;
1157 if (parameters
[j
].IsOut
)
1158 attr
|= ParameterAttributes
.Out
;
1161 throw new Exception ("wrong parameter attributes");
1163 methodBuilder
.DefineParameter (parameters
[j
].Position
+ 1,
1165 parameters
[j
].Name
);
1168 if (method
.ReturnsStruct
) {
1169 // Console.WriteLine ("returns struct");
1171 UnoXStructTypeDescription std
= method
.ReturnType
as UnoXStructTypeDescription
;
1172 if (std
.TypeArguments
!= 0) {
1173 // Console.WriteLine ("std: '{0}' '{1}' {2}", std, method.ReturnType, method.ReturnType.Name);
1174 Type
[] ats
= new Type
[std
.TypeArguments
];
1176 // Console.WriteLine ("type arguments: {0}", std.TypeArguments);
1178 for (j
= 0; j
< std
.TypeArguments
; j
++) {
1179 ats
[j
] = GetType (std
.TypeArgument (j
));
1180 //Console.WriteLine ("ats [{0}] = {1}", j, ats [j]);
1183 object[] atso
= { ats }
;
1184 Type
[] ctor
= { typeof (Type[]) }
;
1185 attrBuilder
= new CustomAttributeBuilder (typeof (uno
.TypeArgumentsAttribute
).GetConstructor (ctor
),
1187 methodBuilder
.SetCustomAttribute (attrBuilder
);
1191 // define UNO exception attribute (exceptions) --------------------------------------
1192 attrBuilder
= IFaceMethodExceptionAttribute (method
);
1193 if (attrBuilder
!= null)
1194 methodBuilder
.SetCustomAttribute (attrBuilder
);
1197 if (method
.IsOneway
) {
1198 Type
[] arCtorOneway
= new Type
[0];
1199 object[] arArgs
= new object [0];
1200 attrBuilder
= new CustomAttributeBuilder (typeof (uno
.OnewayAttribute
).GetConstructor (arCtorOneway
),
1202 methodBuilder
.SetCustomAttribute (attrBuilder
);
1206 if (member
.TypeClass
!= UnoTypeClass
.InterfaceAttribute
)
1207 throw new Exception (String
.Format ("Unknown member type class: {0} ", member
.TypeClass
));
1209 UnoXInterfaceAttributeTypeDescription attribute
= new UnoXInterfaceAttributeTypeDescription (member
.Handle
);
1210 const MethodAttributes propMethodAttr
= methodAttr
| MethodAttributes
.SpecialName
;
1212 Type attrType
= GetType (attribute
.Type
);
1213 //Console.WriteLine ("attribute {2} type: {0} => {1}", attribute.Type, attrType, attribute.Name);
1214 Type
[] parameters
= new Type
[0];
1216 PropertyBuilder propBuilder
= entry
.mTypeBuilder
.DefineProperty (attribute
.MemberName
, PropertyAttributes
.None
, attrType
, parameters
);
1218 //set BoundAttribute, if necessary
1219 if (attribute
.IsBound
) {
1220 ConstructorInfo ctorBoundAttr
= typeof (uno
.BoundAttribute
).GetConstructor (new Type
[0]);
1221 attrBuilder
= new CustomAttributeBuilder (ctorBoundAttr
, new object [0]);
1222 propBuilder
.SetCustomAttribute (attrBuilder
);
1226 methodBuilder
= entry
.mTypeBuilder
.DefineMethod ("get_" + attribute
.MemberName
,
1227 propMethodAttr
, attrType
, parameters
);
1229 attrBuilder
= ExceptionAttribute (GetTypes (attribute
.GetExceptionTypes
));
1230 if (attrBuilder
!= null)
1231 methodBuilder
.SetCustomAttribute (attrBuilder
);
1233 propBuilder
.SetGetMethod (methodBuilder
);
1235 if (!attribute
.IsReadOnly
) {
1237 parameters
= new Type
[1];
1238 parameters
[0] = attrType
;
1239 //parameters [0] = null;
1240 //Console.WriteLine ("setter parameters: {0} ({1})", parameters, parameters [0]);
1241 methodBuilder
= entry
.mTypeBuilder
.DefineMethod ("set_" + attribute
.MemberName
,
1242 propMethodAttr
, typeof (void), parameters
);
1243 methodBuilder
.DefineParameter (1, ParameterAttributes
.In
, "value");
1245 attrBuilder
= ExceptionAttribute (GetTypes (attribute
.SetExceptionTypes
));
1246 if (attrBuilder
!= null)
1247 methodBuilder
.SetCustomAttribute (attrBuilder
);
1249 propBuilder
.SetSetMethod (methodBuilder
);
1253 // cmm_x_interface_type_description_members_release (membersHandle);
1255 if (mConfig
.mVerbose
)
1256 Console
.WriteLine ("> emitting interface type {0}", "unoidl." + entry
.mType
.Name
);
1258 mIncompleteIFaces
.Remove (entry
.mTypeBuilder
.FullName
);
1260 //Console.WriteLine ("completed {0}", entry.mTypeBuilder.FullName);
1262 return entry
.mTypeBuilder
.CreateType ();
1265 UnoXInterfaceTypeDescription
ResolveInterfaceTypedef (UnoXTypeDescription xtd
)
1267 UnoXInterfaceTypeDescription xtdIface
= xtd
as UnoXInterfaceTypeDescription
;
1269 if (xtdIface
!= null)
1272 UnoXIndirectTypeDescription xtdIndirect
= xtd
as UnoXIndirectTypeDescription
;
1274 if (xtdIndirect
!= null)
1275 return ResolveInterfaceTypedef (xtdIndirect
.ReferencedType
);;
1277 throw new Exception ("resolveInterfaceTypedef was called with an invalid argument");
1280 ArrayList
GetServiceConstructorMethodExceptionsReduced (UnoXTypeDescription
[] exceptionTypes
)
1282 if (exceptionTypes
.Length
== 0)
1283 return new ArrayList ();
1285 ArrayList types
= new ArrayList();
1288 for (i
= 0; i
< exceptionTypes
.Length
; i
++)
1289 types
.Add (GetType ("unoidl." + exceptionTypes
[i
].Name
, true));
1293 bool bRemove
= false;
1295 for (i
= start
; i
< types
.Count
; i
++) {
1296 Type t
= types
[i
] as Type
;
1299 for (j
= 0; j
< types
.Count
; j
++) {
1300 if (t
.IsSubclassOf (types
[j
] as Type
)) {
1311 if (bRemove
== false)
1318 Type
CompleteServiceType (ServiceEntry entry
)
1320 // Create the private default constructor
1321 ConstructorBuilder ctorBuilder
= entry
.mTypeBuilder
.DefineConstructor (MethodAttributes
.Private
|
1322 MethodAttributes
.HideBySig
|
1323 MethodAttributes
.SpecialName
|
1324 MethodAttributes
.RTSpecialName
,
1325 CallingConventions
.Standard
,
1328 ILGenerator ilGen
= ctorBuilder
.GetILGenerator ();
1329 ilGen
.Emit (OpCodes
.Ldarg_0
); // push this
1330 ilGen
.Emit (OpCodes
.Call
, entry
.mTypeBuilder
.BaseType
.GetConstructor (new Type
[0]));
1331 ilGen
.Emit (OpCodes
.Ret
);
1333 // Create the service constructors.
1334 // obtain the interface which makes up this service, it is the return
1335 // type of the constructor functions
1336 UnoXInterfaceTypeDescription iface
= entry
.mType
.Interface
as UnoXInterfaceTypeDescription
;
1339 iface
= ResolveInterfaceTypedef (entry
.mType
.Interface
);
1341 Type ret
= GetType (iface
);
1343 // Create the ConstructorInfo for a DeploymentException
1344 Type typeDeploymentException
= GetType ("unoidl.com.sun.star.uno.DeploymentException", true);
1345 Type
[] arTypeCtor
= { typeof (string), typeof (object) }
;
1346 ConstructorInfo ctorDeploymentException
= typeDeploymentException
.GetConstructor (arTypeCtor
);
1348 Type typeUnoException
= GetType ("unoidl.com.sun.star.uno.Exception", true);
1351 for (i
= entry
.mType
.Constructors
- 1; i
>= 0; i
--) {
1352 bool bParameterArray
= false;
1353 UnoXServiceConstructorDescription ctorDesc
= entry
.mType
.Constructor (i
);
1354 Type
[] typeParameters
= new Type
[ctorDesc
.Parameters
+ 1];
1355 typeParameters
[0] = GetType ("unoidl.com.sun.star.uno.XComponentContext", true);
1357 for (j
= 0; j
< ctorDesc
.Parameters
; j
++) {
1358 UnoXParameter parameter
= ctorDesc
.Parameter (j
);
1360 if (parameter
.IsRest
)
1361 typeParameters
[j
+ 1] = typeof (uno
.Any
[]);
1363 typeParameters
[j
+ 1] = GetType (parameter
.Type
);
1366 // The array typeParameters can contain:
1367 // System.Type and uno.PolymorphicType.
1368 // Passing PolymorphicType to MethodBuilder.DefineMethod will cause a problem.
1369 // The exception will read something like no on information for parameter # d
1370 // Maybe we need no override another Type method in PolymorphicType ...
1371 // Until we have figured this out, we will create another array of System.Type which
1372 // we pass on to DefineMethod.
1374 Type
[] paramTypes
= new Type
[ctorDesc
.Parameters
+ 1];
1376 for (j
= 0; j
< ctorDesc
.Parameters
+ 1; j
++) {
1377 if (typeParameters
[j
] is uno
.PolymorphicType
)
1378 paramTypes
[j
] = (typeParameters
[j
] as uno
.PolymorphicType
).OriginalType
;
1380 paramTypes
[j
] = typeParameters
[j
];
1385 if (ctorDesc
.IsDefault
)
1386 ctorName
= "create";
1388 ctorName
= ctorDesc
.Name
;
1390 MethodBuilder methodBuilder
= entry
.mTypeBuilder
.DefineMethod (ctorName
,
1391 MethodAttributes
.Public
|
1392 MethodAttributes
.HideBySig
|
1393 MethodAttributes
.Static
,
1397 //define UNO exception attribute (exceptions)--------------------------------------
1398 CustomAttributeBuilder attrBuilder
= ExceptionAttribute (GetTypes (ctorDesc
.ExceptionTypes
));
1399 if (attrBuilder
!= null)
1400 methodBuilder
.SetCustomAttribute (attrBuilder
);
1402 // define parameter attributes (paramarray), names etc.
1403 // The first parameter is the XComponentContext, which cannot be obtained
1405 // The context is not part of the idl description
1407 methodBuilder
.DefineParameter (1, ParameterAttributes
.In
, "the_context");
1409 ParameterBuilder
[] parameterBuilder
= new ParameterBuilder
[ctorDesc
.Parameters
];
1412 for (iparam
= 0; iparam
< ctorDesc
.Parameters
; iparam
++) {
1413 UnoXParameter parameter
= ctorDesc
.Parameter (iparam
);
1415 parameterBuilder
[iparam
] = methodBuilder
.DefineParameter (iparam
+ 2, ParameterAttributes
.In
, parameter
.Name
);
1417 if (parameter
.IsRest
) {
1418 bParameterArray
= true;
1419 //set the ParameterArrayAttribute
1420 ConstructorInfo ctorInfo
= typeof (System
.ParamArrayAttribute
).GetConstructor (new Type
[0]);
1421 attrBuilder
= new CustomAttributeBuilder (ctorInfo
, new object [0]);
1423 parameterBuilder
[iparam
].SetCustomAttribute (attrBuilder
);
1428 ilGen
= methodBuilder
.GetILGenerator ();
1430 // Define locals ---------------------------------
1431 // XMultiComponentFactory
1432 LocalBuilder localFactory
= ilGen
.DeclareLocal (GetType ("unoidl.com.sun.star.lang.XMultiComponentFactory", true));
1435 LocalBuilder localReturnVal
= ilGen
.DeclareLocal (ret
);
1437 // Obtain the XMultiComponentFactory and throw an exception if we do not get one
1438 ilGen
.Emit (OpCodes
.Ldarg_0
);
1440 MethodInfo methodGetServiceManager
= GetType ("unoidl.com.sun.star.uno.XComponentContext", true).GetMethod ("getServiceManager");
1441 ilGen
.Emit (OpCodes
.Callvirt
, methodGetServiceManager
);
1442 ilGen
.Emit (OpCodes
.Stloc
, localFactory
);
1443 ilGen
.Emit (OpCodes
.Ldloc
, localFactory
);
1444 Label label1
= ilGen
.DefineLabel ();
1445 ilGen
.Emit (OpCodes
.Brtrue
, label1
);
1447 // The string for the exception
1448 System
.Text
.StringBuilder strBuilder
= new System
.Text
.StringBuilder (256);
1450 strBuilder
.Append ("The service ");
1451 strBuilder
.Append ("unoidl." + entry
.mType
.Name
);
1452 strBuilder
.Append (" could not be created. The context failed to supply the service manager.");
1454 ilGen
.Emit (OpCodes
.Ldstr
, strBuilder
.ToString ());
1455 ilGen
.Emit (OpCodes
.Ldarg_0
);
1456 ilGen
.Emit (OpCodes
.Newobj
, ctorDeploymentException
);
1457 ilGen
.Emit (OpCodes
.Throw
);
1458 ilGen
.MarkLabel (label1
);
1460 // We create a try/ catch around the createInstanceWithContext, etc. functions
1461 // There are 3 cases
1462 // 1. function do not specify exceptions. Then RuntimeExceptions are retrhown and other
1463 // exceptions produce a DeploymentException.
1464 // 2. function specify Exception. Then all exceptions fly through
1465 // 3. function specifies exceptions but no Exception. Then these are rethrown
1466 // and other exceptions, except RuntimeException, produce a deployment exception.
1467 // In case there are no parameters we call
1468 // XMultiComponentFactory.createInstanceWithContext
1470 ArrayList exceptionTypes
= GetServiceConstructorMethodExceptionsReduced (ctorDesc
.ExceptionTypes
);
1471 if (!exceptionTypes
.Contains (typeUnoException
)) {
1472 ilGen
.BeginExceptionBlock ();
1475 if (ctorDesc
.Parameters
== 0) {
1476 ilGen
.Emit (OpCodes
.Ldloc
, localFactory
);
1477 ilGen
.Emit (OpCodes
.Ldstr
, entry
.mType
.Name
);
1478 ilGen
.Emit (OpCodes
.Ldarg_0
);
1480 MethodInfo methodCreate
= localFactory
.LocalType
.GetMethod ("createInstanceWithContext");
1481 ilGen
.Emit (OpCodes
.Callvirt
, methodCreate
);
1482 } else if(bParameterArray
) {
1483 //Service constructor with parameter array
1484 ilGen
.Emit (OpCodes
.Ldloc
, localFactory
);
1485 ilGen
.Emit (OpCodes
.Ldstr
, entry
.mType
.Name
);
1486 ilGen
.Emit (OpCodes
.Ldarg_1
);
1487 ilGen
.Emit (OpCodes
.Ldarg_0
);
1488 MethodInfo methodCreate
= localFactory
.LocalType
.GetMethod ("createInstanceWithArgumentsAndContext");
1489 ilGen
.Emit(OpCodes
.Callvirt
, methodCreate
);
1491 // Any param1, Any param2, etc.
1492 // For each parameter,except the component context, and parameter array
1493 // and Any is created.
1494 LocalBuilder
[] localAny
= new LocalBuilder
[ctorDesc
.Parameters
];
1496 for (iparam
= 0; iparam
< ctorDesc
.Parameters
; iparam
++) {
1497 localAny
[iparam
] = ilGen
.DeclareLocal (typeof (uno
.Any
));
1500 // Any[]. This array is filled with the created Anys which contain the parameters
1501 // and the values contained in the parameter array
1502 LocalBuilder localAnyParams
= ilGen
.DeclareLocal (typeof (uno
.Any
[]));
1504 // Create the Any for every argument, except for the parameter array
1505 // arLocalAny contains the LocalBuilder for all these parameters.
1506 // we call the ctor Any(Type, Object)
1507 // If the parameter is an Any then the Any is created with Any(param.Type, param.Value);
1508 Type
[] typesCtorAny
= { typeof (Type
),
1510 ConstructorInfo ctorAny
= typeof (uno
.Any
).GetConstructor (typesCtorAny
);
1511 MethodInfo methodAnyGetType
= typeof (uno
.Any
).GetProperty ("Type").GetGetMethod ();
1512 MethodInfo methodAnyGetValue
= typeof (uno
.Any
).GetProperty ("Value").GetGetMethod ();
1514 for (j
= 0; j
< localAny
.Length
; j
++) {
1515 //check if the parameter is a polymorphic struct
1516 if (typeParameters
[j
+ 1] is uno
.PolymorphicType
) {
1517 // It is a polymorphic struct
1518 uno
.PolymorphicType polyType
= typeParameters
[j
+ 1] as uno
.PolymorphicType
;
1520 // Load the uninitialized local Any on which we will call the ctor
1521 ilGen
.Emit (OpCodes
.Ldloca
, localAny
[j
]);
1523 // Call PolymorphicType PolymorphicType::GetType(Type t, String polyName)
1524 // Prepare the first parameter
1525 ilGen
.Emit (OpCodes
.Ldtoken
, polyType
.OriginalType
);
1526 Type
[] typeParams
= { typeof (System.RuntimeTypeHandle) }
;
1527 ilGen
.Emit (OpCodes
.Call
, typeof(Type
).GetMethod ("GetTypeFromHandle", typeParams
));
1529 // Prepare the second parameter
1530 ilGen
.Emit (OpCodes
.Ldstr
, polyType
.PolymorphicName
);
1532 // Make the actual call
1533 Type
[] typeParamGetType
= { typeof (Type), typeof (string) }
;
1534 ilGen
.Emit (OpCodes
.Call
,
1535 typeof (uno
.PolymorphicType
).GetMethod("GetType",
1538 // Stack is: localAny, PolymorphicType
1539 // Call Any::Any(Type, Object)
1540 // Prepare the second parameter for the any ctor
1541 ilGen
.Emit (OpCodes
.Ldarg
, j
+ 1);
1543 // if the parameter is a value type then we need to box it, because
1544 // the Any ctor takes an Object
1545 if (typeParameters
[j
+ 1].IsValueType
)
1546 ilGen
.Emit (OpCodes
.Box
, typeParameters
[j
+ 1]);
1547 ilGen
.Emit (OpCodes
.Call
, ctorAny
);
1548 } else if (typeParameters
[j
+ 1] == typeof (uno
.Any
)) {
1549 // Create the call new Any(param.Type,param,Value)
1550 // Stack must be Any,Type,Value
1551 // First load the Any which is to be constructed
1552 ilGen
.Emit (OpCodes
.Ldloca
, localAny
[j
]);
1554 //Load the Type, which is obtained by calling param.Type
1555 ilGen
.Emit (OpCodes
.Ldarga
, j
+ 1);
1556 ilGen
.Emit (OpCodes
.Call
, methodAnyGetType
);
1558 //Load the Value, which is obtained by calling param.Value
1559 ilGen
.Emit (OpCodes
.Ldarga
, j
+ 1);
1560 ilGen
.Emit (OpCodes
.Call
, methodAnyGetValue
);
1562 //Call the Any ctor.
1563 ilGen
.Emit (OpCodes
.Call
, ctorAny
);
1565 ilGen
.Emit (OpCodes
.Ldloca
, localAny
[j
]);
1566 ilGen
.Emit (OpCodes
.Ldtoken
, typeParameters
[j
+ 1]);
1568 Type
[] typeParams
= { typeof (System.RuntimeTypeHandle) }
;
1569 ilGen
.Emit (OpCodes
.Call
, typeof (Type
).GetMethod ("GetTypeFromHandle", typeParams
));
1570 ilGen
.Emit(OpCodes
.Ldarg
, j
+ 1);
1572 // if the parameter is a value type then we need to box it, because
1573 // the Any ctor takes an Object
1574 if (typeParameters
[j
+ 1].IsValueType
)
1575 ilGen
.Emit (OpCodes
.Box
, typeParameters
[j
+ 1]);
1576 ilGen
.Emit(OpCodes
.Call
, ctorAny
);
1581 // Create the Any[] that is passed to the
1582 // createInstanceWithContext[AndArguments] function
1583 ilGen
.Emit (OpCodes
.Ldc_I4
, localAny
.Length
);
1584 ilGen
.Emit (OpCodes
.Newarr
, typeof (uno
.Any
));
1585 ilGen
.Emit (OpCodes
.Stloc
, localAnyParams
);
1587 // Assign all anys created from the parameters
1588 // array to the Any[]
1589 for (j
= 0; j
< localAny
.Length
; j
++) {
1590 ilGen
.Emit (OpCodes
.Ldloc
, localAnyParams
);
1591 ilGen
.Emit (OpCodes
.Ldc_I4
, j
);
1592 ilGen
.Emit (OpCodes
.Ldelema
, typeof (uno
.Any
));
1593 ilGen
.Emit (OpCodes
.Ldloc
, localAny
[j
]);
1594 ilGen
.Emit (OpCodes
.Stobj
, typeof (uno
.Any
));
1597 // call createInstanceWithContextAndArguments
1598 ilGen
.Emit (OpCodes
.Ldloc
, localFactory
);
1599 ilGen
.Emit (OpCodes
.Ldstr
, entry
.mType
.Name
);
1600 ilGen
.Emit (OpCodes
.Ldloc
, localAnyParams
);
1601 ilGen
.Emit (OpCodes
.Ldarg_0
);
1602 MethodInfo methodCreate
= localFactory
.LocalType
.GetMethod ("createInstanceWithArgumentsAndContext");
1603 ilGen
.Emit (OpCodes
.Callvirt
, methodCreate
);
1606 // cast the object returned by the functions createInstanceWithContext or
1607 // createInstanceWithArgumentsAndContext to the interface type
1608 ilGen
.Emit (OpCodes
.Castclass
, ret
);
1609 ilGen
.Emit (OpCodes
.Stloc
, localReturnVal
);
1611 //catch exceptions thrown by createInstanceWithArgumentsAndContext and createInstanceWithContext
1612 if (!exceptionTypes
.Contains (typeUnoException
)) {
1613 // catch (unoidl.com.sun.star.uno.RuntimeException) {throw;}
1614 ilGen
.BeginCatchBlock (GetType ("unoidl.com.sun.star.uno.RuntimeException", true));
1615 ilGen
.Emit (OpCodes
.Pop
);
1616 ilGen
.Emit (OpCodes
.Rethrow
);
1618 //catch and rethrow all other defined Exceptions
1619 for (j
= 0; j
< exceptionTypes
.Count
; j
++) {
1620 Type excType
= exceptionTypes
[j
] as Type
;
1621 if (excType
.IsInstanceOfType (GetType ("unoidl.com.sun.star.uno.RuntimeException", true))) {
1622 // we have a catch for RuntimeException already defined
1626 //catch Exception and rethrow
1627 ilGen
.BeginCatchBlock (excType
);
1628 ilGen
.Emit (OpCodes
.Pop
);
1629 ilGen
.Emit (OpCodes
.Rethrow
);
1632 //catch (unoidl.com.sun.star.uno.Exception) {throw DeploymentException...}
1633 ilGen
.BeginCatchBlock (typeUnoException
);
1635 //Define the local variabe that keeps the exception
1636 LocalBuilder localException
= ilGen
.DeclareLocal (typeUnoException
);
1638 //Store the exception
1639 ilGen
.Emit (OpCodes
.Stloc
, localException
);
1641 //prepare the construction of the exception
1642 strBuilder
= new System
.Text
.StringBuilder (256);
1643 strBuilder
.Append ("The context (com.sun.star.uno.XComponentContext) failed to supply the service ");
1644 strBuilder
.Append ("unoidl." + entry
.mType
.Name
);
1645 strBuilder
.Append (": ");
1647 ilGen
.Emit (OpCodes
.Ldstr
, strBuilder
.ToString());
1649 // add to the string the Exception.Message
1650 ilGen
.Emit (OpCodes
.Ldloc
, localException
);
1651 //Console.WriteLine ("get message property of type: {0}", typeUnoException);
1652 ilGen
.Emit (OpCodes
.Callvirt
, typeUnoException
.GetProperty ("Message").GetGetMethod ());
1653 Type
[] concatParams
= { typeof (string), typeof (string)}
;
1654 ilGen
.Emit (OpCodes
.Call
, typeof (string).GetMethod ("Concat", concatParams
));
1656 //load contex argument
1657 ilGen
.Emit (OpCodes
.Ldarg_0
);
1658 ilGen
.Emit (OpCodes
.Newobj
, ctorDeploymentException
);
1659 ilGen
.Emit (OpCodes
.Throw
); //Exception(typeDeploymentExc);
1661 ilGen
.EndExceptionBlock();
1665 // Check if the service instance was create and throw a exception if not.
1666 Label labelServiceCreated
= ilGen
.DefineLabel ();
1667 ilGen
.Emit (OpCodes
.Ldloc
, localReturnVal
);
1668 ilGen
.Emit (OpCodes
.Brtrue_S
, labelServiceCreated
);
1670 strBuilder
= new System
.Text
.StringBuilder(256);
1671 strBuilder
.Append ("The context (com.sun.star.uno.XComponentContext) failed to supply the service ");
1672 strBuilder
.Append ("unoidl." + entry
.mType
.Name
);
1673 strBuilder
.Append (".");
1674 ilGen
.Emit (OpCodes
.Ldstr
, strBuilder
.ToString());
1675 ilGen
.Emit (OpCodes
.Ldarg_0
);
1676 ilGen
.Emit (OpCodes
.Newobj
, ctorDeploymentException
);
1677 ilGen
.Emit (OpCodes
.Throw
); //Exception(typeDeploymentExc);
1679 ilGen
.MarkLabel (labelServiceCreated
);
1680 ilGen
.Emit (OpCodes
.Ldloc
, localReturnVal
);
1681 ilGen
.Emit (OpCodes
.Ret
);
1684 mIncompleteServices
.Remove (entry
.mTypeBuilder
.FullName
);
1686 if (mConfig
.mVerbose
)
1687 Console
.WriteLine("> emitting service type {0}", "unoidl." + entry
.mType
.Name
);
1689 //Console.WriteLine ("completed service {0}", entry.mTypeBuilder.FullName);
1691 return entry
.mTypeBuilder
.CreateType ();
1694 Type
CompleteSingletonType (SingletonEntry entry
)
1696 string name
= "unoidl." + entry
.mType
.Name
;
1698 // Create the private default constructor
1699 ConstructorBuilder ctorBuilder
= entry
.mTypeBuilder
.DefineConstructor (MethodAttributes
.Private
|
1700 MethodAttributes
.HideBySig
|
1701 MethodAttributes
.SpecialName
|
1702 MethodAttributes
.RTSpecialName
,
1703 CallingConventions
.Standard
, null);
1705 ILGenerator ilGen
= ctorBuilder
.GetILGenerator ();
1706 ilGen
.Emit (OpCodes
.Ldarg_0
); // push this
1707 ilGen
.Emit (OpCodes
.Call
, entry
.mTypeBuilder
.BaseType
.GetConstructor (new Type
[0]));
1708 ilGen
.Emit (OpCodes
.Ret
);
1710 // obtain the interface which makes up this service, it is the return
1711 // type of the constructor functions
1712 UnoXTypeDescription ifaceTD
= entry
.mType
.Interface
;
1713 if (!(ifaceTD
is UnoXInterfaceTypeDescription
))
1714 ifaceTD
= ResolveInterfaceTypedef (ifaceTD
);
1715 Type retType
= GetType (ifaceTD
);
1718 Type
[] typeParameters
= { GetType ("unoidl.com.sun.star.uno.XComponentContext", true) }
;
1719 MethodBuilder methodBuilder
= entry
.mTypeBuilder
.DefineMethod ("get", MethodAttributes
.Public
|
1720 MethodAttributes
.HideBySig
|
1721 MethodAttributes
.Static
,
1725 // The first parameter is the XComponentContext, which cannot be obtained
1727 // The context is not part of the idl description
1728 methodBuilder
.DefineParameter (1, ParameterAttributes
.In
, "the_context");
1730 ilGen
= methodBuilder
.GetILGenerator();
1731 // Define locals ---------------------------------
1732 // Any, returned by XComponentContext.getValueByName
1733 LocalBuilder localAny
= ilGen
.DeclareLocal (typeof (uno
.Any
));
1735 // Call XContext.getValueByName
1736 ilGen
.Emit (OpCodes
.Ldarg_0
);
1738 // build the singleton name : /singleton/unoidl.com.sun.star.XXX
1739 ilGen
.Emit(OpCodes
.Ldstr
, "/singletons/" + name
);
1741 MethodInfo methodGetValueByName
= GetType ("unoidl.com.sun.star.uno.XComponentContext", true).GetMethod ("getValueByName");
1742 ilGen
.Emit(OpCodes
.Callvirt
, methodGetValueByName
);
1743 ilGen
.Emit(OpCodes
.Stloc_0
);
1745 //Contains the returned Any a value?
1746 ilGen
.Emit(OpCodes
.Ldloca_S
, localAny
);
1747 MethodInfo methodHasValue
= typeof (uno
.Any
).GetMethod ("hasValue");
1748 ilGen
.Emit (OpCodes
.Call
, methodHasValue
);
1750 //If not, then throw an DeploymentException
1751 Label labelSingletonExists
= ilGen
.DefineLabel ();
1752 ilGen
.Emit (OpCodes
.Brtrue_S
, labelSingletonExists
);
1753 ilGen
.Emit (OpCodes
.Ldstr
, "Component context fails to supply singleton " + name
+ " of type " + retType
.FullName
+ ".");
1754 ilGen
.Emit (OpCodes
.Ldarg_0
);
1755 Type
[] typesCtorDeploymentException
= { typeof (string), typeof (object) }
;
1756 ilGen
.Emit (OpCodes
.Newobj
, GetType ("unoidl.com.sun.star.uno.DeploymentException", true).GetConstructor (typesCtorDeploymentException
));
1757 ilGen
.Emit (OpCodes
.Throw
);
1758 ilGen
.MarkLabel (labelSingletonExists
);
1760 //Cast the singleton contained in the Any to the expected interface and return it.
1761 ilGen
.Emit (OpCodes
.Ldloca_S
, localAny
);
1762 ilGen
.Emit (OpCodes
.Call
, typeof (uno
.Any
).GetProperty ("Value").GetGetMethod ());
1763 ilGen
.Emit (OpCodes
.Castclass
, retType
);
1764 ilGen
.Emit (OpCodes
.Ret
);
1766 if (mConfig
.mVerbose
)
1767 Console
.WriteLine ("> emitting singleton type {0}", name
);
1769 mIncompleteSingletons
.Remove (entry
.mTypeBuilder
.FullName
);
1771 //Console.WriteLine ("completed singleton {0}", entry.mTypeBuilder.FullName);
1773 return entry
.mTypeBuilder
.CreateType ();
1776 public void Dispose ()
1778 while (mIncompleteIFaces
.Count
> 0) {
1779 IDictionaryEnumerator e
= mIncompleteIFaces
.GetEnumerator ();
1781 CompleteIFaceType (e
.Value
as IFaceEntry
);
1784 while (mIncompleteServices
.Count
> 0) {
1785 IDictionaryEnumerator e
= mIncompleteServices
.GetEnumerator ();
1787 CompleteServiceType (e
.Value
as ServiceEntry
);
1790 while (mIncompleteSingletons
.Count
> 0) {
1791 IDictionaryEnumerator e
= mIncompleteSingletons
.GetEnumerator ();
1793 CompleteSingletonType (e
.Value
as SingletonEntry
);
1800 public ArrayList mMandatoryRegistries
= new ArrayList ();
1801 public ArrayList mExplicitTypes
= new ArrayList ();
1802 public ArrayList mExtraAssemblies
= new ArrayList ();
1803 public ArrayList mExtraRegistries
= new ArrayList ();
1804 public bool mVerbose
= false;
1808 mDescription
= null,
1821 // string mOutputDir = "./";
1822 // string mOutputFile = "cli_uretypes.dll";
1823 // string mName = "cli_uretypes";
1831 AssemblyBuilder mAssemblyBuilder
;
1832 ResolveEventHandler mTypeResolver
= null;
1834 public CliMaker (Config config
)
1837 mUnoHelper
= cmm_uno_helper_new ();
1842 cmm_uno_helper_delete (mUnoHelper
);
1845 public void OpenRegistries ()
1847 if (mConfig
.mExplicitTypes
.Count
> 0)
1848 foreach (string typeName
in mConfig
.mExplicitTypes
)
1849 cmm_uno_helper_add_explicit_type (mUnoHelper
, typeName
);
1851 foreach (string registry
in mConfig
.mMandatoryRegistries
)
1852 cmm_uno_helper_add_mandatory_registry (mUnoHelper
, registry
);
1854 foreach (string registry
in mConfig
.mExtraRegistries
)
1855 cmm_uno_helper_add_extra_registry (mUnoHelper
, registry
);
1857 cmm_uno_helper_open_registries (mUnoHelper
);
1860 public void PrepareAssembly ()
1862 // Get the key pair for making a strong name
1863 StrongNameKeyPair kp
= null;
1864 if (mConfig
.mKeyfile
!= null) {
1866 System
.IO
.FileStream fs
= new System
.IO
.FileStream (mConfig
.mKeyfile
, System
.IO
.FileMode
.Open
);
1867 kp
= new StrongNameKeyPair (fs
);
1869 } catch (System
.IO
.FileNotFoundException
) {
1870 throw new Exception ("Could not find the keyfile. Verify the --keyfile argument!");
1873 if (mConfig
.mVerbose
)
1874 Console
.WriteLine ("> no key file specified. Cannot create strong name!");
1877 mOutputFile
= System
.IO
.Path
.GetFileName (mConfig
.mOutput
);
1878 mOutputDir
= System
.IO
.Path
.GetDirectoryName (mConfig
.mOutput
);
1879 mName
= System
.IO
.Path
.GetFileNameWithoutExtension (mConfig
.mOutput
);
1880 // int idx = mOutputFile.LastIndexOf (System.IO.Path.DirectorySeparatorChar);
1882 // mOutputDir = mOutputFile.Substring (0, idx);
1884 // mOutputDir = System.IO.Path.CurrentDirectory;
1885 // idx = mOutputFile.LastIndexOf (".dll");
1886 // mName = mOutputFile.Substring (0, idx);
1887 //Console.WriteLine ("file {0}\ndir {1}\nname {2}", mOutputFile, mOutputDir, mName);
1889 // setup assembly info: xxx todo set more? e.g. avoid strong versioning
1890 AssemblyName assemblyName
= new AssemblyName();
1891 assemblyName
.CodeBase
= mOutputDir
;
1892 assemblyName
.Name
= mName
;
1895 assemblyName
.KeyPair
= kp
;
1897 if (mConfig
.mVersion
!= null)
1898 assemblyName
.Version
= new System
.Version (mConfig
.mVersion
);
1901 mAssemblyBuilder
= AppDomain
.CurrentDomain
.DefineDynamicAssembly (assemblyName
, AssemblyBuilderAccess
.Save
, mOutputDir
);
1903 if (mConfig
.mProduct
!= null) {
1904 Type
[] paramTypes
= { typeof (string) }
;
1905 object[] args
= { mConfig.mProduct }
;
1906 mAssemblyBuilder
.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyProductAttribute
).GetConstructor (paramTypes
), args
));
1909 if (mConfig
.mDescription
!= null) {
1910 Type
[] paramTypes
= { typeof (string) }
;
1911 object[] args
= { mConfig.mDescription }
;
1912 mAssemblyBuilder
.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyDescriptionAttribute
).GetConstructor (paramTypes
), args
));
1915 if (mConfig
.mCompany
!= null) {
1916 Type
[] paramTypes
= { typeof (string) }
;
1917 object[] args
= { mConfig.mCompany }
;
1918 mAssemblyBuilder
.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyCompanyAttribute
).GetConstructor (paramTypes
), args
));
1921 if (mConfig
.mCopyright
!= null) {
1922 Type
[] paramTypes
= { typeof (string) }
;
1923 object[] args
= { mConfig.mCopyright }
;
1924 mAssemblyBuilder
.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyCopyrightAttribute
).GetConstructor (paramTypes
), args
));
1927 if (mConfig
.mTrademark
!= null) {
1928 Type
[] paramTypes
= { typeof (string) }
;
1929 object[] args
= { mConfig.mTrademark }
;
1930 mAssemblyBuilder
.SetCustomAttribute (new CustomAttributeBuilder (typeof (AssemblyTrademarkAttribute
).GetConstructor (paramTypes
), args
));
1934 public UnoXTypeDescription
NextType ()
1936 IntPtr handle
= cmm_uno_helper_next_type (mUnoHelper
);
1938 if (handle
!= IntPtr
.Zero
) {
1939 return UnoXTypeDescription
.Create (handle
);
1947 TypeEmitter mTypeEmitter
= new TypeEmitter (mConfig
, mAssemblyBuilder
.DefineDynamicModule( mOutputFile
));
1949 AppDomain
.CurrentDomain
.TypeResolve
+= mTypeEmitter
.ResolveEventHandler
;
1951 UnoXTypeDescription type
;
1953 while ((type
= NextType ()) != null)
1954 mTypeEmitter
.GetType (type
);
1956 mTypeEmitter
.Dispose ();
1961 if (mConfig
.mVerbose
)
1962 Console
.Write ("> saving assembly {0}{1}{2}...",
1964 System
.IO
.Path
.DirectorySeparatorChar
,
1967 mAssemblyBuilder
.Save (mOutputFile
);
1969 if (mConfig
.mVerbose
)
1970 Console
.WriteLine ("ok.");
1972 AppDomain
.CurrentDomain
.TypeResolve
-= mTypeResolver
;
1975 [DllImport("climaker")]
1976 private static extern IntPtr
cmm_uno_helper_new ();
1978 [DllImport("climaker")]
1979 private static extern void cmm_uno_helper_delete (IntPtr handle
);
1981 [DllImport("climaker")]
1982 private static extern void cmm_uno_helper_add_mandatory_registry (IntPtr handle
, string registry
);
1984 [DllImport("climaker")]
1985 private static extern void cmm_uno_helper_add_extra_registry (IntPtr handle
, string registry
);
1987 [DllImport("climaker")]
1988 private static extern void cmm_uno_helper_add_explicit_type (IntPtr handle
, string typeName
);
1990 [DllImport("climaker")]
1991 private static extern void cmm_uno_helper_open_registries (IntPtr handle
);
1993 [DllImport("climaker")]
1994 private static extern IntPtr
cmm_uno_helper_next_type (IntPtr handle
);
1999 protected IntPtr mHandle
;
2001 protected UnoObject (IntPtr handle
)
2006 public IntPtr Handle
2015 class UnoXTypeDescription
: UnoObject
2017 protected UnoXTypeDescription (IntPtr handle
) : base (handle
)
2021 public static UnoXTypeDescription
Create (IntPtr handle
)
2023 //Console.WriteLine ("Create {0}", handle);
2024 if (handle
== IntPtr
.Zero
)
2027 UnoTypeClass tc
= cmm_x_type_description_get_type_class (handle
);
2029 case UnoTypeClass
.Enum
:
2030 return new UnoXEnumTypeDescription (handle
);
2031 case UnoTypeClass
.Interface
:
2032 return new UnoXInterfaceTypeDescription (handle
);
2033 case UnoTypeClass
.Struct
:
2034 return new UnoXStructTypeDescription (handle
);
2035 case UnoTypeClass
.Exception
:
2036 return new UnoXExceptionTypeDescription (handle
);
2037 case UnoTypeClass
.Sequence
:
2038 case UnoTypeClass
.Typedef
:
2039 return new UnoXIndirectTypeDescription (handle
);
2040 case UnoTypeClass
.Constant
:
2041 return new UnoXConstantTypeDescription (handle
);
2042 case UnoTypeClass
.Constants
:
2043 return new UnoXConstantsTypeDescription (handle
);
2044 case UnoTypeClass
.Service
:
2045 return new UnoXServiceTypeDescription (handle
);
2046 case UnoTypeClass
.Singleton
:
2047 return new UnoXSingletonTypeDescription (handle
);
2050 //Console.WriteLine ("unknown type {0}", tc);
2052 return new UnoXTypeDescription (handle
);
2059 return Marshal
.PtrToStringAnsi (cmm_x_type_description_get_name (mHandle
));
2063 public UnoTypeClass TypeClass
2067 return cmm_x_type_description_get_type_class (mHandle
);
2071 [DllImport("climaker")]
2072 private static extern IntPtr
cmm_x_type_description_get_name (IntPtr handle
);
2074 [DllImport("climaker")]
2075 private static extern UnoTypeClass
cmm_x_type_description_get_type_class (IntPtr handle
);
2078 class UnoXEnumTypeDescription
: UnoXTypeDescription
2080 public UnoXEnumTypeDescription (IntPtr mHandle
) : base (mHandle
)
2088 return cmm_x_enum_type_description_get_length (mHandle
);
2092 public string ValueName (int idx
)
2094 return Marshal
.PtrToStringAnsi (cmm_x_enum_type_description_get_name (mHandle
, idx
));
2097 public int Value (int idx
)
2099 return cmm_x_enum_type_description_get_value (mHandle
, idx
);
2102 [DllImport("climaker")]
2103 private static extern int cmm_x_enum_type_description_get_length (IntPtr handle
);
2105 [DllImport("climaker")]
2106 private static extern int cmm_x_enum_type_description_get_value (IntPtr handle
, int idx
);
2108 [DllImport("climaker")]
2109 private static extern IntPtr
cmm_x_enum_type_description_get_name (IntPtr handle
, int idx
);
2112 class UnoXInterfaceTypeDescription
: UnoXTypeDescription
2115 IntPtr mMembersHandle
= IntPtr
.Zero
;
2117 public UnoXInterfaceTypeDescription (IntPtr mHandle
) : base (mHandle
)
2121 public int BaseTypes
2125 return cmm_x_interface_type_description_get_length (mHandle
);
2129 public UnoXInterfaceTypeDescription
BaseType (int idx
)
2131 return new UnoXInterfaceTypeDescription (cmm_x_interface_type_description_get_base_type (mHandle
, idx
));
2134 void AssureMembers ()
2136 if (mMembersHandle
== IntPtr
.Zero
)
2137 mMembers
= cmm_x_interface_type_description_get_members (mHandle
, out mMembersHandle
);
2150 public UnoXInterfaceMemberTypeDescription
Member (int idx
)
2154 if (idx
< 0 || idx
>= mMembers
)
2157 return new UnoXInterfaceMemberTypeDescription (cmm_x_interface_type_description_get_member (mMembersHandle
, idx
));
2160 [DllImport("climaker")]
2161 private static extern int cmm_x_interface_type_description_get_length (IntPtr handle
);
2163 [DllImport("climaker")]
2164 private static extern IntPtr
cmm_x_interface_type_description_get_base_type (IntPtr handle
, int idx
);
2166 [DllImport("climaker")]
2167 private static extern int cmm_x_interface_type_description_get_members (IntPtr handle
, out IntPtr membersHandle
);
2169 [DllImport("climaker")]
2170 private static extern IntPtr
cmm_x_interface_type_description_get_member (IntPtr membersArrayHandle
, int idx
);
2173 class UnoXInterfaceMemberTypeDescription
: UnoXTypeDescription
2175 public UnoXInterfaceMemberTypeDescription (IntPtr handle
) : base (handle
)
2179 public string MemberName
2183 return Marshal
.PtrToStringAnsi (cmm_x_interface_member_type_description_get_member_name (mHandle
));
2187 [DllImport("climaker")]
2188 private static extern IntPtr
cmm_x_interface_member_type_description_get_member_name (IntPtr handle
);
2191 class UnoXInterfaceAttributeTypeDescription
: UnoXInterfaceMemberTypeDescription
2193 public UnoXInterfaceAttributeTypeDescription (IntPtr handle
) : base (handle
)
2197 public UnoXTypeDescription Type
2201 return UnoXTypeDescription
.Create (cmm_x_interface_attribute_type_description_get_type (mHandle
));
2209 return cmm_x_interface_attribute_type_description_is_bound (mHandle
);
2213 public bool IsReadOnly
2217 return cmm_x_interface_attribute_type_description_is_read_only (mHandle
);
2221 UnoXTypeDescription
[] ExceptionTypes (IntPtr handle
, int count
)
2225 UnoXTypeDescription
[] types
= new UnoXTypeDescription
[count
];
2226 for (i
= 0; i
< count
; i
++) {
2227 types
[i
] = UnoXTypeDescription
.Create (cmm_x_interface_attribute_type_description_get_exception_type (handle
, i
));
2233 public UnoXTypeDescription
[] GetExceptionTypes
2240 count
= cmm_x_interface_attribute_type_description_get_get_exception_types (mHandle
, out handle
);
2241 UnoXTypeDescription
[] types
= ExceptionTypes (handle
, count
);
2244 cmm_x_interface_attribute_type_description_free_exception_types (handle
, count
);
2250 public UnoXTypeDescription
[] SetExceptionTypes
2257 count
= cmm_x_interface_attribute_type_description_get_set_exception_types (mHandle
, out handle
);
2258 UnoXTypeDescription
[] types
= ExceptionTypes (handle
, count
);
2261 cmm_x_interface_attribute_type_description_free_exception_types (handle
, count
);
2267 [DllImport("climaker")]
2268 private static extern IntPtr
cmm_x_interface_attribute_type_description_get_type (IntPtr handle
);
2270 [DllImport("climaker")][return:MarshalAs(UnmanagedType
.I1
)]
2271 private static extern bool cmm_x_interface_attribute_type_description_is_bound (IntPtr handle
);
2273 [DllImport("climaker")][return:MarshalAs(UnmanagedType
.I1
)]
2274 private static extern bool cmm_x_interface_attribute_type_description_is_read_only (IntPtr handle
);
2276 [DllImport("climaker")]
2277 private static extern int cmm_x_interface_attribute_type_description_get_get_exception_types (IntPtr handle
, out IntPtr exceptionTypesHandle
);
2279 [DllImport("climaker")]
2280 private static extern int cmm_x_interface_attribute_type_description_get_set_exception_types (IntPtr handle
, out IntPtr exceptionTypesHandle
);
2282 [DllImport("climaker")]
2283 private static extern IntPtr
cmm_x_interface_attribute_type_description_get_exception_type (IntPtr handle
, int index
);
2285 [DllImport("climaker")]
2286 private static extern void cmm_x_interface_attribute_type_description_free_exception_types (IntPtr handle
, int count
);
2289 class UnoXInterfaceMethodTypeDescription
: UnoXInterfaceMemberTypeDescription
2291 int mParameters
= 0;
2292 IntPtr mParametersHandle
= IntPtr
.Zero
;
2293 IntPtr mParametersArrayHandle
= IntPtr
.Zero
;
2295 bool mGotExceptionTypes
= false;
2296 int mExceptionTypes
= 0;
2297 IntPtr mExceptionTypesHandle
= IntPtr
.Zero
;
2298 IntPtr mExceptionTypesArrayHandle
= IntPtr
.Zero
;
2300 public UnoXInterfaceMethodTypeDescription (IntPtr handle
) : base (handle
)
2304 public UnoXTypeDescription ReturnType
2308 return UnoXTypeDescription
.Create (cmm_x_interface_method_type_description_get_return_type (mHandle
));
2312 public bool ReturnsStruct
2316 return cmm_x_interface_method_type_description_returns_struct (mHandle
);
2320 void AssureParameters ()
2322 if (mParametersHandle
== IntPtr
.Zero
)
2323 mParameters
= cmm_x_interface_method_type_description_get_parameters (mHandle
, out mParametersHandle
, out mParametersArrayHandle
);
2326 public int Parameters
2330 AssureParameters ();
2336 public UnoXMethodParameter
Parameter (int idx
)
2338 AssureParameters ();
2340 if (idx
< 0 || idx
>= mParameters
)
2343 return new UnoXMethodParameter (cmm_x_interface_method_type_description_get_parameter (mParametersArrayHandle
, idx
));
2346 void AssureExceptionTypes ()
2348 if (!mGotExceptionTypes
) {
2349 mExceptionTypes
= cmm_x_interface_method_type_description_get_exception_types (mHandle
, out mExceptionTypesHandle
, out mExceptionTypesArrayHandle
);
2350 mGotExceptionTypes
= true;
2354 public int ExceptionTypes
2358 AssureExceptionTypes ();
2360 return mExceptionTypes
;
2364 public UnoXCompoundTypeDescription
ExceptionType (int idx
)
2366 AssureExceptionTypes ();
2368 if (idx
< 0 || idx
>= mExceptionTypes
)
2371 return UnoXTypeDescription
.Create (cmm_x_interface_method_type_description_get_exception_type (mExceptionTypesArrayHandle
, idx
)) as UnoXCompoundTypeDescription
;
2374 public bool IsOneway
2378 return cmm_x_interface_method_type_description_is_oneway (mHandle
);
2382 [DllImport("climaker")]
2383 private static extern int cmm_x_interface_method_type_description_get_parameters (IntPtr handle
, out IntPtr parametersHandle
, out IntPtr parametersArrayHandle
);
2385 [DllImport("climaker")]
2386 private static extern IntPtr
cmm_x_interface_method_type_description_get_parameter (IntPtr parametersArrayHandle
, int idx
);
2388 [DllImport("climaker")]
2389 private static extern int cmm_x_interface_method_type_description_get_exception_types (IntPtr handle
, out IntPtr exceptionTypesHandle
, out IntPtr exceptionTypesArrayHandle
);
2391 [DllImport("climaker")]
2392 private static extern IntPtr
cmm_x_interface_method_type_description_get_exception_type (IntPtr exceptionTypesArrayHandle
, int idx
);
2394 [DllImport("climaker")]
2395 private static extern IntPtr
cmm_x_interface_method_type_description_get_return_type (IntPtr handle
);
2397 [DllImport("climaker")][return:MarshalAs(UnmanagedType
.I1
)]
2398 private static extern bool cmm_x_interface_method_type_description_returns_struct (IntPtr handle
);
2400 [DllImport("climaker")][return:MarshalAs(UnmanagedType
.I1
)]
2401 private static extern bool cmm_x_interface_method_type_description_is_oneway (IntPtr handle
);
2404 class UnoXCompoundTypeDescription
: UnoXTypeDescription
2406 int mMemberNames
= 0;
2407 IntPtr mMemberNamesHandle
= IntPtr
.Zero
;
2409 int mMemberTypes
= 0;
2410 IntPtr mMemberTypesHandle
= IntPtr
.Zero
;
2412 public UnoXCompoundTypeDescription (IntPtr handle
) : base (handle
)
2416 public UnoXTypeDescription BaseType
2420 return UnoXTypeDescription
.Create (cmm_x_compound_type_description_get_base_type (mHandle
));
2424 void AssureMemberNames ()
2426 if (mMemberNamesHandle
== IntPtr
.Zero
)
2427 mMemberNames
= cmm_x_compound_type_description_get_member_names (mHandle
, out mMemberNamesHandle
);
2430 public string MemberName (int idx
)
2432 AssureMemberNames ();
2434 if (idx
< 0 || idx
>= mMemberNames
)
2437 return Marshal
.PtrToStringAnsi (cmm_x_compound_type_description_get_member_name (mMemberNamesHandle
, idx
));
2440 public int MemberNames
2444 AssureMemberNames ();
2446 return mMemberNames
;
2450 void AssureMemberTypes ()
2452 if (mMemberTypesHandle
== IntPtr
.Zero
)
2453 mMemberTypes
= cmm_x_compound_type_description_get_member_types (mHandle
, out mMemberTypesHandle
);
2456 public UnoXTypeDescription
MemberType (int idx
)
2458 AssureMemberTypes ();
2460 if (idx
< 0 || idx
>= mMemberTypes
)
2463 return UnoXTypeDescription
.Create (cmm_x_compound_type_description_get_member_type (mMemberTypesHandle
, idx
));
2466 public int MemberTypes
2470 AssureMemberTypes ();
2472 return mMemberTypes
;
2476 [DllImport("climaker")]
2477 private static extern IntPtr
cmm_x_compound_type_description_get_base_type (IntPtr handle
);
2479 [DllImport("climaker")]
2480 private static extern int cmm_x_compound_type_description_get_member_names (IntPtr handle
, out IntPtr memberNamesHandle
);
2482 [DllImport("climaker")]
2483 private static extern IntPtr
cmm_x_compound_type_description_get_member_name (IntPtr handle
, int idx
);
2485 [DllImport("climaker")]
2486 private static extern int cmm_x_compound_type_description_get_member_types (IntPtr handle
, out IntPtr memberTypesHandle
);
2488 [DllImport("climaker")]
2489 private static extern IntPtr
cmm_x_compound_type_description_get_member_type (IntPtr handle
, int idx
);
2492 class UnoXExceptionTypeDescription
: UnoXCompoundTypeDescription
2494 public UnoXExceptionTypeDescription (IntPtr handle
) : base (handle
)
2499 class UnoXStructTypeDescription
: UnoXCompoundTypeDescription
2501 int mTypeArguments
= 0;
2502 IntPtr mTypeArgumentsHandle
= IntPtr
.Zero
;
2503 IntPtr mTypeArgumentsArrayHandle
= IntPtr
.Zero
;
2505 int mTypeParameters
= 0;
2506 IntPtr mTypeParametersHandle
= IntPtr
.Zero
;
2508 public UnoXStructTypeDescription (IntPtr handle
) : base (handle
)
2512 void AssureTypeParameters ()
2514 if (mTypeParametersHandle
== IntPtr
.Zero
)
2515 mTypeParameters
= cmm_x_struct_type_description_get_type_parameters (mHandle
, out mTypeParametersHandle
);
2518 public string TypeParameter (int idx
)
2520 AssureTypeParameters ();
2522 if (idx
< 0 || idx
>= mTypeParameters
)
2525 return Marshal
.PtrToStringAnsi (cmm_x_struct_type_description_get_type_parameter (mTypeParametersHandle
, idx
));
2528 public int TypeParameters
2532 AssureTypeParameters ();
2534 return mTypeParameters
;
2538 void AssureTypeArguments ()
2540 if (mTypeArgumentsHandle
== IntPtr
.Zero
)
2541 mTypeArguments
= cmm_x_struct_type_description_get_type_arguments (mHandle
, out mTypeArgumentsHandle
, out mTypeArgumentsArrayHandle
);
2544 public int TypeArguments
2548 AssureTypeArguments ();
2550 return mTypeArguments
;
2554 public UnoXTypeDescription
TypeArgument (int idx
)
2556 AssureTypeArguments ();
2558 if (idx
< 0 || idx
>= mTypeArguments
)
2561 return UnoXTypeDescription
.Create (cmm_x_struct_type_description_get_type_argument (mTypeArgumentsArrayHandle
, idx
));
2564 [DllImport("climaker")]
2565 private static extern int cmm_x_struct_type_description_get_type_parameters (IntPtr handle
, out IntPtr typeParametersHandle
);
2567 [DllImport("climaker")]
2568 private static extern IntPtr
cmm_x_struct_type_description_get_type_parameter (IntPtr typeParametersHandle
, int idx
);
2570 [DllImport("climaker")]
2571 private static extern int cmm_x_struct_type_description_get_type_arguments (IntPtr handle
, out IntPtr typeArgumentsHandle
, out IntPtr typeArgumentsArrayHandle
);
2573 [DllImport("climaker")]
2574 private static extern IntPtr
cmm_x_struct_type_description_get_type_argument (IntPtr typeArgumentsArrayHandle
, int idx
);
2577 class UnoXMethodParameter
: UnoObject
2579 public UnoXMethodParameter (IntPtr handle
) : base (handle
)
2587 return cmm_x_method_parameter_is_out (mHandle
);
2595 return cmm_x_method_parameter_is_in (mHandle
);
2603 return Marshal
.PtrToStringAnsi (cmm_x_method_parameter_name (mHandle
));
2611 return cmm_x_method_parameter_position (mHandle
);
2615 public UnoXTypeDescription Type
2619 return UnoXTypeDescription
.Create (cmm_x_method_parameter_type (mHandle
));
2623 [DllImport("climaker")][return:MarshalAs(UnmanagedType
.I1
)]
2624 private static extern bool cmm_x_method_parameter_is_out (IntPtr handle
);
2626 [DllImport("climaker")][return:MarshalAs(UnmanagedType
.I1
)]
2627 private static extern bool cmm_x_method_parameter_is_in (IntPtr handle
);
2629 [DllImport("climaker")]
2630 private static extern int cmm_x_method_parameter_position (IntPtr handle
);
2632 [DllImport("climaker")]
2633 private static extern IntPtr
cmm_x_method_parameter_name (IntPtr handle
);
2635 [DllImport("climaker")]
2636 private static extern IntPtr
cmm_x_method_parameter_type (IntPtr handle
);
2639 class UnoXParameter
: UnoXMethodParameter
2641 public UnoXParameter (IntPtr handle
) : base (handle
)
2649 return cmm_x_parameter_is_rest (mHandle
);
2653 [DllImport("climaker")][return:MarshalAs(UnmanagedType
.I1
)]
2654 private static extern bool cmm_x_parameter_is_rest (IntPtr handle
);
2657 class UnoXIndirectTypeDescription
: UnoXTypeDescription
2659 public UnoXIndirectTypeDescription (IntPtr handle
) : base (handle
)
2663 public UnoXTypeDescription ReferencedType
2667 return UnoXTypeDescription
.Create (cmm_x_indirect_type_description_get_referenced_type (mHandle
));
2671 [DllImport("climaker")]
2672 private static extern IntPtr
cmm_x_indirect_type_description_get_referenced_type (IntPtr handle
);
2675 class UnoXServiceTypeDescription
: UnoXTypeDescription
2677 int mConstructors
= 0;
2678 IntPtr mConstructorsHandle
= IntPtr
.Zero
;
2680 public UnoXServiceTypeDescription (IntPtr handle
) : base (handle
)
2684 public bool IsSingleInterfaceBased
2688 return cmm_x_service_type_description_is_single_interface_based (mHandle
);
2692 public UnoXTypeDescription Interface
2696 return UnoXTypeDescription
.Create (cmm_x_service_type_description_get_interface (mHandle
));
2700 void AssureConstructors ()
2702 if (mConstructorsHandle
== IntPtr
.Zero
)
2703 mConstructors
= cmm_x_service_type_description_get_constructors (mHandle
, out mConstructorsHandle
);
2706 public int Constructors
2710 AssureConstructors ();
2712 return mConstructors
;
2716 public UnoXServiceConstructorDescription
Constructor (int idx
)
2718 AssureConstructors ();
2720 if (idx
< 0 || idx
>= mConstructors
)
2723 return new UnoXServiceConstructorDescription (cmm_x_service_type_description_get_constructor (mConstructorsHandle
, idx
));
2726 [DllImport("climaker")]
2727 private static extern int cmm_x_service_type_description_get_constructors (IntPtr handle
, out IntPtr constructorssHandle
);
2729 [DllImport("climaker")]
2730 private static extern IntPtr
cmm_x_service_type_description_get_constructor (IntPtr constructorsHandle
, int idx
);
2732 [DllImport("climaker")][return:MarshalAs(UnmanagedType
.I1
)]
2733 private static extern bool cmm_x_service_type_description_is_single_interface_based (IntPtr handle
);
2735 [DllImport("climaker")]
2736 private static extern IntPtr
cmm_x_service_type_description_get_interface (IntPtr handle
);
2739 class UnoXSingletonTypeDescription
: UnoXTypeDescription
2741 public UnoXSingletonTypeDescription (IntPtr handle
) : base (handle
)
2745 public bool IsInterfaceBased
2749 return cmm_x_singleton_type_description_is_interface_based (mHandle
);
2753 public UnoXTypeDescription Interface
2757 return UnoXTypeDescription
.Create (cmm_x_singleton_type_description_get_interface (mHandle
));
2761 [DllImport("climaker")]
2762 private static extern IntPtr
cmm_x_singleton_type_description_get_interface (IntPtr handle
);
2764 [DllImport("climaker")][return:MarshalAs(UnmanagedType
.I1
)]
2765 private static extern bool cmm_x_singleton_type_description_is_interface_based (IntPtr handle
);
2768 class UnoXConstantTypeDescription
: UnoXTypeDescription
2770 public UnoXConstantTypeDescription (IntPtr handle
) : base (handle
)
2774 public object ConstantValue
2782 System
.UInt16 tUInt16
;
2784 System
.UInt32 tUInt32
;
2786 System
.UInt64 tUInt64
;
2787 System
.Single tFloat
;
2790 UnoTypeClass tc
= cmm_any_to_cli_constant (mHandle
,
2791 out tChar
, out tBool
,
2793 out tInt16
, out tUInt16
,
2794 out tInt32
, out tUInt32
,
2795 out tInt64
, out tUInt64
,
2796 out tFloat
, out tDouble
);
2798 case UnoTypeClass
.Char
:
2800 case UnoTypeClass
.Boolean
:
2802 case UnoTypeClass
.Byte
:
2804 case UnoTypeClass
.Short
:
2806 case UnoTypeClass
.UnsignedShort
:
2808 case UnoTypeClass
.Long
:
2810 case UnoTypeClass
.UnsignedLong
:
2812 case UnoTypeClass
.Hyper
:
2814 case UnoTypeClass
.UnsignedHyper
:
2816 case UnoTypeClass
.Float
:
2818 case UnoTypeClass
.Double
:
2822 throw new Exception (String
.Format ("Unexpected type {0} for XConstantTypeDescription", tc
));
2826 [DllImport("climaker")]
2827 private static extern UnoTypeClass
cmm_any_to_cli_constant (IntPtr handle
,
2828 out char tChar
, out bool tBool
,
2830 out Int16 tInt16
, out System
.UInt16 tUInt16
,
2831 out Int32 tInt32
, out System
.UInt32 tUInt32
,
2832 out Int64 tInt64
, out System
.UInt64 tUInt64
,
2833 out System
.Single tFloat
, out double tDouble
);
2836 class UnoXConstantsTypeDescription
: UnoXTypeDescription
2839 IntPtr mConstantsHandle
= IntPtr
.Zero
;
2841 public UnoXConstantsTypeDescription (IntPtr handle
) : base (handle
)
2845 void AssureConstants ()
2847 if (mConstantsHandle
== IntPtr
.Zero
)
2848 mConstants
= cmm_x_constants_type_description_get_constants (mHandle
, out mConstantsHandle
);
2851 public int Constants
2861 public UnoXConstantTypeDescription
Constant (int idx
)
2865 if (idx
< 0 || idx
>= mConstants
)
2868 return UnoXTypeDescription
.Create (cmm_x_constants_type_description_get_constant (mConstantsHandle
, idx
)) as UnoXConstantTypeDescription
;
2871 [DllImport("climaker")]
2872 private static extern int cmm_x_constants_type_description_get_constants (IntPtr handle
, out IntPtr constantssHandle
);
2874 [DllImport("climaker")]
2875 private static extern IntPtr
cmm_x_constants_type_description_get_constant (IntPtr constantsHandle
, int idx
);
2878 class UnoXServiceConstructorDescription
: UnoObject
2880 int mParameters
= 0;
2881 IntPtr mParametersHandle
= IntPtr
.Zero
;
2883 public UnoXServiceConstructorDescription (IntPtr handle
) : base (handle
)
2887 void AssureParameters ()
2889 if (mParametersHandle
== IntPtr
.Zero
)
2890 mParameters
= cmm_x_service_constructor_description_get_parameters (mHandle
, out mParametersHandle
);
2893 public int Parameters
2897 AssureParameters ();
2903 public UnoXParameter
Parameter (int idx
)
2905 AssureParameters ();
2907 if (idx
< 0 || idx
>= mParameters
)
2910 return new UnoXParameter (cmm_x_service_constructor_description_get_parameter (mParametersHandle
, idx
));
2917 return Marshal
.PtrToStringAnsi (cmm_x_service_constructor_description_get_name (mHandle
));
2921 public bool IsDefault
2925 return cmm_x_service_constructor_description_is_default_constructor (mHandle
);
2929 public UnoXTypeDescription
[] ExceptionTypes
2936 count
= cmm_x_service_constructor_description_get_exception_types (mHandle
, out handle
);
2937 UnoXTypeDescription
[] types
= new UnoXTypeDescription
[count
];
2939 for (i
= 0; i
< count
; i
++) {
2940 types
[i
] = UnoXTypeDescription
.Create (cmm_x_service_constructor_description_get_exception_type (handle
, i
));
2944 cmm_x_service_constructor_description_free_exception_types (handle
, count
);
2950 [DllImport("climaker")]
2951 private static extern int cmm_x_service_constructor_description_get_parameters (IntPtr handle
, out IntPtr parameterssHandle
);
2953 [DllImport("climaker")]
2954 private static extern IntPtr
cmm_x_service_constructor_description_get_parameter (IntPtr parametersHandle
, int idx
);
2956 [DllImport("climaker")]
2957 private static extern IntPtr
cmm_x_service_constructor_description_get_name (IntPtr handle
);
2959 [DllImport("climaker")][return:MarshalAs(UnmanagedType
.I1
)]
2960 private static extern bool cmm_x_service_constructor_description_is_default_constructor (IntPtr handle
);
2962 [DllImport("climaker")]
2963 private static extern int cmm_x_service_constructor_description_get_exception_types (IntPtr handle
, out IntPtr exceptionTypesHandle
);
2965 [DllImport("climaker")]
2966 private static extern IntPtr
cmm_x_service_constructor_description_get_exception_type (IntPtr handle
, int index
);
2968 [DllImport("climaker")]
2969 private static extern void cmm_x_service_constructor_description_free_exception_types (IntPtr handle
, int count
);
2973 public class MainClass
2977 public string mName
;
2978 public char mShortOption
;
2979 public bool mHasArgument
;
2981 public OptionInfo (string name
, char shortOption
, bool hasArgument
)
2984 mShortOption
= shortOption
;
2985 mHasArgument
= hasArgument
;
2989 static OptionInfo
[] sOptionInfos
= {
2990 new OptionInfo ("out", 'O', true ),
2991 new OptionInfo ("types", 'T', true),
2992 new OptionInfo ("extra", 'X', true),
2993 new OptionInfo ("reference", 'r', true),
2994 new OptionInfo ("keyfile", 'k', true),
2995 new OptionInfo ("delaySign", 'd', true),
2996 new OptionInfo ("assembly-version", '\0', true),
2997 new OptionInfo ("assembly-description", '\0', true),
2998 new OptionInfo ("assembly-product", '\0', true),
2999 new OptionInfo ("assembly-company", '\0', true),
3000 new OptionInfo ("assembly-copyright", '\0', true),
3001 new OptionInfo ("assembly-trademark", '\0', true),
3002 new OptionInfo ("verbose", 'v', false),
3003 new OptionInfo ("help", 'h', false)
3006 static string sUsingText
=
3008 "using: climaker <switches> [registry-file-1 registry-file-2 ...]\n" +
3011 " -O, --out <output-file> output assembly file;\n" +
3012 " defaults to cli_unotypes.dll if more than one\n" +
3013 " registry-file is given, else <registry-file>.dll\n" +
3014 " -T, --types types to be generated (if none is given,\n" +
3015 " <type1[;type2;...]> then all types of given registries are emitted\n" +
3016 " -X, --extra <rdb-file> additional rdb to saturate referenced types in\n" +
3017 " given registry file(s); these types will not be\n" +
3018 " emitted into the output assembly file\n" +
3019 " -r, --reference reference metadata from assembly file\n" +
3020 " <assembly-file>\n" +
3021 " -k, --keyfile keyfile needed for strong name\n" +
3022 " --assembly-version <version> sets assembly version\n" +
3023 " --assembly-description <text> sets assembly description text\n" +
3024 " --assembly-product <text> sets assembly product name\n" +
3025 " --assembly-company <text> sets assembly company\n" +
3026 " --assembly-copyright <text> sets assembly copyright\n" +
3027 " --assembly-trademark <text> sets assembly trademark\n" +
3028 " -v, --verbose verbose output to stdout\n" +
3029 " -h, --help this message\n" +
3031 "example: climaker --out cli_mytypes.dll \\\n" +
3032 " --reference cli_uretypes.dll \\\n" +
3033 " --extra types.rdb \\\n" +
3037 static OptionInfo
GetOptionInfo (string opt
)
3039 return GetOptionInfo (opt
, '\0');
3042 static OptionInfo
GetOptionInfo (string opt
, char shortOpt
)
3046 for (pos
= 0; pos
< sOptionInfos
.Length
; pos
++) {
3047 if (opt
.Length
> 0) {
3048 if (opt
.Equals (sOptionInfos
[pos
].mName
))
3049 return sOptionInfos
[pos
];
3051 if (sOptionInfos
[pos
].mShortOption
== shortOpt
)
3052 return sOptionInfos
[pos
];
3059 static bool IsOption (OptionInfo optionInfo
, ref int pIndex
)
3061 if (sArgs
.Length
<= pIndex
)
3063 string arg
= sArgs
[pIndex
];
3065 if (arg
.Length
< 2 || arg
[0] != '-')
3068 if (arg
.Length
== 2 && arg
[ 1 ] == optionInfo
.mShortOption
) {
3074 if (arg
[1] == '-' && arg
.Substring (2).Equals (optionInfo
.mName
)) {
3083 static bool ReadOption (ref bool flag
, OptionInfo optionInfo
, ref int pIndex
)
3085 bool ret
= IsOption (optionInfo
, ref pIndex
);
3092 static bool ReadArgument (ref string pValue
, OptionInfo optionInfo
, ref int pIndex
)
3094 if (IsOption (optionInfo
, ref pIndex
)) {
3095 if (pIndex
< sArgs
.Length
) {
3096 pValue
= sArgs
[pIndex
];
3107 static string[] sArgs
;
3109 public static int Main (string[] args
)
3111 if (args
.Length
<= 0) {
3112 Console
.Write (sUsingText
);
3120 Config config
= new Config ();
3122 OptionInfo infoHelp
= GetOptionInfo ("help");
3123 OptionInfo infoVerbose
= GetOptionInfo ("verbose");
3124 OptionInfo infoOut
= GetOptionInfo ("out");
3125 OptionInfo infoTypes
= GetOptionInfo ("types");
3126 OptionInfo infoReference
= GetOptionInfo ("reference");
3127 OptionInfo infoExtra
= GetOptionInfo ("extra");
3128 OptionInfo infoKeyfile
= GetOptionInfo ("keyfile");
3129 OptionInfo infoDelaysign
= GetOptionInfo ("delaySign");
3130 OptionInfo infoVersion
= GetOptionInfo ("assembly-version");
3131 OptionInfo infoProduct
= GetOptionInfo ("assembly-product");
3132 OptionInfo infoDescription
= GetOptionInfo ("assembly-description");
3133 OptionInfo infoCompany
= GetOptionInfo ("assembly-company");
3134 OptionInfo infoCopyright
= GetOptionInfo ("assembly-copyright");
3135 OptionInfo infoTrademark
= GetOptionInfo ("assembly-trademark");
3138 string cmdArg
= null;
3140 for (pos
= 0; pos
< sArgs
.Length
; ) {
3142 if (IsOption (infoHelp
, ref pos
)) {
3143 Console
.Write (sUsingText
);
3145 } else if (ReadArgument (ref cmdArg
, infoTypes
, ref pos
)) {
3146 config
.mExplicitTypes
.AddRange (cmdArg
.Split (new char[] {';'}
));
3147 } else if (ReadArgument (ref cmdArg
, infoExtra
, ref pos
)) {
3148 config
.mExtraRegistries
.Add (cmdArg
);
3149 } else if (ReadArgument (ref cmdArg
, infoReference
, ref pos
)) {
3150 config
.mExtraAssemblies
.Add (cmdArg
);
3151 } else if (!ReadOption (ref config
.mVerbose
, infoVerbose
, ref pos
) &&
3152 !ReadArgument (ref config
.mOutput
, infoOut
, ref pos
) &&
3153 !ReadArgument (ref config
.mVersion
, infoVersion
, ref pos
) &&
3154 !ReadArgument (ref config
.mDescription
, infoDescription
, ref pos
) &&
3155 !ReadArgument (ref config
.mProduct
, infoProduct
, ref pos
) &&
3156 !ReadArgument (ref config
.mCompany
, infoCompany
, ref pos
) &&
3157 !ReadArgument (ref config
.mCopyright
, infoCopyright
, ref pos
) &&
3158 !ReadArgument (ref config
.mTrademark
, infoTrademark
, ref pos
) &&
3159 !ReadArgument (ref config
.mKeyfile
, infoKeyfile
, ref pos
) &&
3160 !ReadArgument (ref config
.mDelaySign
, infoDelaysign
, ref pos
)) {
3161 cmdArg
= args
[pos
];
3163 cmdArg
= cmdArg
.Trim();
3165 if (cmdArg
.Length
> 0) {
3166 if (cmdArg
[0] == '-') { // is option
3167 OptionInfo optionInfo
= null;
3169 if (cmdArg
.Length
> 2 && cmdArg
[1] == '-') {
3171 optionInfo
= GetOptionInfo (cmdArg
.Substring (2));
3172 } else if (cmdArg
.Length
== 2 && cmdArg
[1] != '-') {
3174 optionInfo
= GetOptionInfo ("", cmdArg
[1]);
3177 if (optionInfo
== null) {
3178 Console
.WriteLine ("unknown option " + cmdArg
+ "! Use climaker --help\nto print all options.");
3181 Console
.WriteLine ("unhandled valid option?! " + cmdArg
);
3182 if (optionInfo
.mHasArgument
)
3186 config
.mMandatoryRegistries
.Add (cmdArg
);
3192 CliMaker cliMaker
= new CliMaker (config
);
3194 cliMaker
.OpenRegistries ();
3195 cliMaker
.PrepareAssembly ();
3198 } catch (Exception e
) {
3199 Console
.WriteLine ("\n> error: " + e
+ "\n> dying abnormally...");