Update mcs/class/Commons.Xml.Relaxng/Commons.Xml.Relaxng/RelaxngPattern.cs
[mono.git] / mcs / class / corlib / System.Reflection.Emit / ParameterBuilder.cs
1
2 //
3 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining
6 // a copy of this software and associated documentation files (the
7 // "Software"), to deal in the Software without restriction, including
8 // without limitation the rights to use, copy, modify, merge, publish,
9 // distribute, sublicense, and/or sell copies of the Software, and to
10 // permit persons to whom the Software is furnished to do so, subject to
11 // the following conditions:
12 // 
13 // The above copyright notice and this permission notice shall be
14 // included in all copies or substantial portions of the Software.
15 // 
16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 //
24
25
26 //
27 // System.Reflection.Emit/ParameterBuilder.cs
28 //
29 // Author:
30 //   Paolo Molaro (lupus@ximian.com)
31 //
32 // (C) 2001 Ximian, Inc.  http://www.ximian.com
33 //
34
35 using System;
36 using System.Reflection;
37 using System.Reflection.Emit;
38 using System.Globalization;
39 using System.Runtime.CompilerServices;
40 using System.Runtime.InteropServices;
41
42 namespace System.Reflection.Emit {
43         [ComVisible (true)]
44         [ComDefaultInterface (typeof (_ParameterBuilder))]
45         [ClassInterface (ClassInterfaceType.None)]
46         [StructLayout (LayoutKind.Sequential)]
47         public class ParameterBuilder : _ParameterBuilder {
48
49 #pragma warning disable 169, 414
50                 private MethodBase methodb; /* MethodBuilder, ConstructorBuilder or DynamicMethod */
51                 private string name;
52                 private CustomAttributeBuilder[] cattrs;
53                 private UnmanagedMarshal marshal_info;
54                 private ParameterAttributes attrs;
55                 private int position;
56                 private int table_idx;
57                 object def_value;
58 #pragma warning restore 169, 414
59                 
60                 internal ParameterBuilder (MethodBase mb, int pos, ParameterAttributes attributes, string strParamName) {
61                         name = strParamName;
62                         position = pos;
63                         attrs = attributes;
64                         methodb = mb;
65                         if (mb is DynamicMethod)
66                                 table_idx = 0;
67                         else
68                                 table_idx = mb.get_next_table_index (this, 0x08, true);
69                 }
70
71                 public virtual int Attributes {
72                         get {return (int)attrs;}
73                 }
74                 public bool IsIn {
75                         get {return ((int)attrs & (int)ParameterAttributes.In) != 0;}
76                 }
77                 public bool IsOut {
78                         get {return ((int)attrs & (int)ParameterAttributes.Out) != 0;}
79                 }
80                 public bool IsOptional {
81                         get {return ((int)attrs & (int)ParameterAttributes.Optional) != 0;}
82                 }
83                 public virtual string Name {
84                         get {return name;}
85                 }
86                 public virtual int Position {
87                         get {return position;}
88                 }
89
90                 public virtual ParameterToken GetToken() {
91                         return new ParameterToken (0x08 | table_idx);
92                 }
93
94                 public virtual void SetConstant (object defaultValue)
95                 {
96                         if (position > 0) {
97                                 Type t = methodb.GetParameterType (position - 1);
98                                 if (defaultValue != null && t != defaultValue.GetType ()) {
99                                         if(!t.IsEnum || t.UnderlyingSystemType != defaultValue.GetType ())
100                                                 throw new ArgumentException ("Constant does not match the defined type.");
101                                 }
102                                 if (t.IsValueType && !t.IsPrimitive && !t.IsEnum && t != typeof (DateTime))
103                                         throw new ArgumentException ("" + t + " is not a supported constant type.");
104                         }
105
106                         def_value = defaultValue;
107                         attrs |= ParameterAttributes.HasDefault;
108                 }
109                 
110                 public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
111                         string attrname = customBuilder.Ctor.ReflectedType.FullName;
112                         if (attrname == "System.Runtime.InteropServices.InAttribute") {
113                                 attrs |= ParameterAttributes.In;
114                                 return;
115                         } else if (attrname == "System.Runtime.InteropServices.OutAttribute") {
116                                 attrs |= ParameterAttributes.Out;
117                                 return;
118                         } else if (attrname == "System.Runtime.InteropServices.OptionalAttribute") {
119                                 attrs |= ParameterAttributes.Optional;
120                                 return;
121                         } else if (attrname == "System.Runtime.InteropServices.MarshalAsAttribute") {
122                                 attrs |= ParameterAttributes.HasFieldMarshal;
123                                 marshal_info = CustomAttributeBuilder.get_umarshal (customBuilder, false);
124                                 /* FIXME: check for errors */
125                                 return;
126                         } else if (attrname == "System.Runtime.InteropServices.DefaultParameterValueAttribute") {
127                                 /* MS.NET doesn't handle this attribute but we handle it for consistency */
128                                 CustomAttributeBuilder.CustomAttributeInfo cinfo = CustomAttributeBuilder.decode_cattr (customBuilder);
129                                 /* FIXME: check for type compatibility */
130                                 SetConstant (cinfo.ctorArgs [0]);
131                                 return;
132                         }
133
134                         if (cattrs != null) {
135                                 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
136                                 cattrs.CopyTo (new_array, 0);
137                                 new_array [cattrs.Length] = customBuilder;
138                                 cattrs = new_array;
139                         } else {
140                                 cattrs = new CustomAttributeBuilder [1];
141                                 cattrs [0] = customBuilder;
142                         }
143                 }
144
145                 [ComVisible (true)]
146                 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
147                         SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
148                 }
149
150                 [Obsolete ("An alternate API is available: Emit the MarshalAs custom attribute instead.")]
151                 public virtual void SetMarshal( UnmanagedMarshal unmanagedMarshal) {
152                         marshal_info = unmanagedMarshal;
153                         attrs |= ParameterAttributes.HasFieldMarshal;
154                 }
155
156                 void _ParameterBuilder.GetIDsOfNames([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
157                 {
158                         throw new NotImplementedException ();
159                 }
160
161                 void _ParameterBuilder.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
162                 {
163                         throw new NotImplementedException ();
164                 }
165
166                 void _ParameterBuilder.GetTypeInfoCount (out uint pcTInfo)
167                 {
168                         throw new NotImplementedException ();
169                 }
170
171                 void _ParameterBuilder.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
172                 {
173                         throw new NotImplementedException ();
174                 }
175         }
176 }
177