Merge pull request #495 from nicolas-raoul/fix-for-issue2907-with-no-formatting-changes
[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 #if !FULL_AOT_RUNTIME
36 using System;
37 using System.Reflection;
38 using System.Reflection.Emit;
39 using System.Globalization;
40 using System.Runtime.CompilerServices;
41 using System.Runtime.InteropServices;
42
43 namespace System.Reflection.Emit {
44         [ComVisible (true)]
45         [ComDefaultInterface (typeof (_ParameterBuilder))]
46         [ClassInterface (ClassInterfaceType.None)]
47         [StructLayout (LayoutKind.Sequential)]
48         public class ParameterBuilder : _ParameterBuilder {
49
50 #pragma warning disable 169, 414
51                 private MethodBase methodb; /* MethodBuilder, ConstructorBuilder or DynamicMethod */
52                 private string name;
53                 private CustomAttributeBuilder[] cattrs;
54                 private UnmanagedMarshal marshal_info;
55                 private ParameterAttributes attrs;
56                 private int position;
57                 private int table_idx;
58                 object def_value;
59 #pragma warning restore 169, 414
60                 
61                 internal ParameterBuilder (MethodBase mb, int pos, ParameterAttributes attributes, string strParamName) {
62                         name = strParamName;
63                         position = pos;
64                         attrs = attributes;
65                         methodb = mb;
66                         if (mb is DynamicMethod)
67                                 table_idx = 0;
68                         else
69                                 table_idx = mb.get_next_table_index (this, 0x08, true);
70                 }
71
72                 public virtual int Attributes {
73                         get {return (int)attrs;}
74                 }
75                 public bool IsIn {
76                         get {return ((int)attrs & (int)ParameterAttributes.In) != 0;}
77                 }
78                 public bool IsOut {
79                         get {return ((int)attrs & (int)ParameterAttributes.Out) != 0;}
80                 }
81                 public bool IsOptional {
82                         get {return ((int)attrs & (int)ParameterAttributes.Optional) != 0;}
83                 }
84                 public virtual string Name {
85                         get {return name;}
86                 }
87                 public virtual int Position {
88                         get {return position;}
89                 }
90
91                 public virtual ParameterToken GetToken() {
92                         return new ParameterToken (0x08 | table_idx);
93                 }
94
95                 public virtual void SetConstant (object defaultValue)
96                 {
97                         if (position > 0) {
98                                 Type t = methodb.GetParameterType (position - 1);
99                                 if (defaultValue != null && t != defaultValue.GetType ()) {
100                                         if(!t.IsEnum || t.UnderlyingSystemType != defaultValue.GetType ())
101                                                 throw new ArgumentException ("Constant does not match the defined type.");
102                                 }
103                                 if (t.IsValueType && !t.IsPrimitive && !t.IsEnum && t != typeof (DateTime))
104                                         throw new ArgumentException ("" + t + " is not a supported constant type.");
105                         }
106
107                         def_value = defaultValue;
108                         attrs |= ParameterAttributes.HasDefault;
109                 }
110                 
111                 public void SetCustomAttribute( CustomAttributeBuilder customBuilder) {
112                         string attrname = customBuilder.Ctor.ReflectedType.FullName;
113                         if (attrname == "System.Runtime.InteropServices.InAttribute") {
114                                 attrs |= ParameterAttributes.In;
115                                 return;
116                         } else if (attrname == "System.Runtime.InteropServices.OutAttribute") {
117                                 attrs |= ParameterAttributes.Out;
118                                 return;
119                         } else if (attrname == "System.Runtime.InteropServices.OptionalAttribute") {
120                                 attrs |= ParameterAttributes.Optional;
121                                 return;
122                         } else if (attrname == "System.Runtime.InteropServices.MarshalAsAttribute") {
123                                 attrs |= ParameterAttributes.HasFieldMarshal;
124                                 marshal_info = CustomAttributeBuilder.get_umarshal (customBuilder, false);
125                                 /* FIXME: check for errors */
126                                 return;
127                         } else if (attrname == "System.Runtime.InteropServices.DefaultParameterValueAttribute") {
128                                 /* MS.NET doesn't handle this attribute but we handle it for consistency */
129                                 CustomAttributeBuilder.CustomAttributeInfo cinfo = CustomAttributeBuilder.decode_cattr (customBuilder);
130                                 /* FIXME: check for type compatibility */
131                                 SetConstant (cinfo.ctorArgs [0]);
132                                 return;
133                         }
134
135                         if (cattrs != null) {
136                                 CustomAttributeBuilder[] new_array = new CustomAttributeBuilder [cattrs.Length + 1];
137                                 cattrs.CopyTo (new_array, 0);
138                                 new_array [cattrs.Length] = customBuilder;
139                                 cattrs = new_array;
140                         } else {
141                                 cattrs = new CustomAttributeBuilder [1];
142                                 cattrs [0] = customBuilder;
143                         }
144                 }
145
146                 [ComVisible (true)]
147                 public void SetCustomAttribute( ConstructorInfo con, byte[] binaryAttribute) {
148                         SetCustomAttribute (new CustomAttributeBuilder (con, binaryAttribute));
149                 }
150
151                 [Obsolete ("An alternate API is available: Emit the MarshalAs custom attribute instead.")]
152                 public virtual void SetMarshal( UnmanagedMarshal unmanagedMarshal) {
153                         marshal_info = unmanagedMarshal;
154                         attrs |= ParameterAttributes.HasFieldMarshal;
155                 }
156
157                 void _ParameterBuilder.GetIDsOfNames([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
158                 {
159                         throw new NotImplementedException ();
160                 }
161
162                 void _ParameterBuilder.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
163                 {
164                         throw new NotImplementedException ();
165                 }
166
167                 void _ParameterBuilder.GetTypeInfoCount (out uint pcTInfo)
168                 {
169                         throw new NotImplementedException ();
170                 }
171
172                 void _ParameterBuilder.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams, IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
173                 {
174                         throw new NotImplementedException ();
175                 }
176         }
177 }
178
179 #endif