do not check order sequence if option /order was not used
[mono.git] / mcs / class / IKVM.Reflection / Emit / ParameterBuilder.cs
1 /*
2   Copyright (C) 2008 Jeroen Frijters
3
4   This software is provided 'as-is', without any express or implied
5   warranty.  In no event will the authors be held liable for any damages
6   arising from the use of this software.
7
8   Permission is granted to anyone to use this software for any purpose,
9   including commercial applications, and to alter it and redistribute it
10   freely, subject to the following restrictions:
11
12   1. The origin of this software must not be misrepresented; you must not
13      claim that you wrote the original software. If you use this software
14      in a product, an acknowledgment in the product documentation would be
15      appreciated but is not required.
16   2. Altered source versions must be plainly marked as such, and must not be
17      misrepresented as being the original software.
18   3. This notice may not be removed or altered from any source distribution.
19
20   Jeroen Frijters
21   jeroen@frijters.net
22   
23 */
24 using System;
25 using IKVM.Reflection.Writer;
26 using IKVM.Reflection.Metadata;
27
28 namespace IKVM.Reflection.Emit
29 {
30         public sealed class ParameterBuilder
31         {
32                 private readonly ModuleBuilder moduleBuilder;
33                 private short flags;
34                 private readonly short sequence;
35                 private readonly int nameIndex;
36                 private readonly string name;
37                 private int lazyPseudoToken;
38
39                 internal ParameterBuilder(ModuleBuilder moduleBuilder, int sequence, ParameterAttributes attribs, string name)
40                 {
41                         this.moduleBuilder = moduleBuilder;
42                         this.flags = (short)attribs;
43                         this.sequence = (short)sequence;
44                         this.nameIndex = name == null ? 0 : moduleBuilder.Strings.Add(name);
45                         this.name = name;
46                 }
47
48                 internal int PseudoToken
49                 {
50                         get
51                         {
52                                 if (lazyPseudoToken == 0)
53                                 {
54                                         // we lazily create the token, because if we don't need it we don't want the token fixup cost
55                                         lazyPseudoToken = moduleBuilder.AllocPseudoToken();
56                                 }
57                                 return lazyPseudoToken;
58                         }
59                 }
60
61                 public string Name
62                 {
63                         get { return name; }
64                 }
65
66                 public int Position
67                 {
68                         // note that this differs from ParameterInfo.Position, which is zero based
69                         get { return sequence; }
70                 }
71
72                 public int Attributes
73                 {
74                         get { return flags; }
75                 }
76
77                 public bool IsIn
78                 {
79                         get { return (flags & (short)ParameterAttributes.In) != 0; }
80                 }
81
82                 public bool IsOut
83                 {
84                         get { return (flags & (short)ParameterAttributes.Out) != 0; }
85                 }
86
87                 public bool IsOptional
88                 {
89                         get { return (flags & (short)ParameterAttributes.Optional) != 0; }
90                 }
91
92                 public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
93                 {
94                         SetCustomAttribute(new CustomAttributeBuilder(con, binaryAttribute));
95                 }
96
97                 public void SetCustomAttribute(CustomAttributeBuilder customAttributeBuilder)
98                 {
99                         Universe u = moduleBuilder.universe;
100                         if (customAttributeBuilder.Constructor.DeclaringType == u.System_Runtime_InteropServices_InAttribute)
101                         {
102                                 flags |= (short)ParameterAttributes.In;
103                         }
104                         else if (customAttributeBuilder.Constructor.DeclaringType == u.System_Runtime_InteropServices_OutAttribute)
105                         {
106                                 flags |= (short)ParameterAttributes.Out;
107                         }
108                         else if (customAttributeBuilder.Constructor.DeclaringType == u.System_Runtime_InteropServices_OptionalAttribute)
109                         {
110                                 flags |= (short)ParameterAttributes.Optional;
111                         }
112                         else if (customAttributeBuilder.Constructor.DeclaringType == u.System_Runtime_InteropServices_MarshalAsAttribute)
113                         {
114                                 FieldMarshal.SetMarshalAsAttribute(moduleBuilder, PseudoToken, customAttributeBuilder);
115                                 flags |= (short)ParameterAttributes.HasFieldMarshal;
116                         }
117                         else
118                         {
119                                 moduleBuilder.SetCustomAttribute(PseudoToken, customAttributeBuilder);
120                         }
121                 }
122
123                 public void SetConstant(object defaultValue)
124                 {
125                         flags |= (short)ParameterAttributes.HasDefault;
126                         moduleBuilder.AddConstant(PseudoToken, defaultValue);
127                 }
128
129                 internal void WriteParamRecord(MetadataWriter mw)
130                 {
131                         mw.Write(flags);
132                         mw.Write(sequence);
133                         mw.WriteStringIndex(nameIndex);
134                 }
135
136                 internal void FixupToken(int parameterToken)
137                 {
138                         if (lazyPseudoToken != 0)
139                         {
140                                 moduleBuilder.RegisterTokenFixup(lazyPseudoToken, parameterToken);
141                         }
142                 }
143         }
144 }