2 Copyright (C) 2008 Jeroen Frijters
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.
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:
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.
25 using IKVM.Reflection.Writer;
26 using IKVM.Reflection.Metadata;
28 namespace IKVM.Reflection.Emit
30 public sealed class ParameterBuilder
32 private readonly ModuleBuilder moduleBuilder;
34 private readonly short sequence;
35 private readonly int nameIndex;
36 private readonly string name;
37 private int lazyPseudoToken;
39 internal ParameterBuilder(ModuleBuilder moduleBuilder, int sequence, ParameterAttributes attribs, string name)
41 this.moduleBuilder = moduleBuilder;
42 this.flags = (short)attribs;
43 this.sequence = (short)sequence;
44 this.nameIndex = name == null ? 0 : moduleBuilder.Strings.Add(name);
48 internal int PseudoToken
52 if (lazyPseudoToken == 0)
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();
57 return lazyPseudoToken;
68 // note that this differs from ParameterInfo.Position, which is zero based
69 get { return sequence; }
79 get { return (flags & (short)ParameterAttributes.In) != 0; }
84 get { return (flags & (short)ParameterAttributes.Out) != 0; }
87 public bool IsOptional
89 get { return (flags & (short)ParameterAttributes.Optional) != 0; }
92 public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
94 SetCustomAttribute(new CustomAttributeBuilder(con, binaryAttribute));
97 public void SetCustomAttribute(CustomAttributeBuilder customAttributeBuilder)
99 Universe u = moduleBuilder.universe;
100 if (customAttributeBuilder.Constructor.DeclaringType == u.System_Runtime_InteropServices_InAttribute)
102 flags |= (short)ParameterAttributes.In;
104 else if (customAttributeBuilder.Constructor.DeclaringType == u.System_Runtime_InteropServices_OutAttribute)
106 flags |= (short)ParameterAttributes.Out;
108 else if (customAttributeBuilder.Constructor.DeclaringType == u.System_Runtime_InteropServices_OptionalAttribute)
110 flags |= (short)ParameterAttributes.Optional;
112 else if (customAttributeBuilder.Constructor.DeclaringType == u.System_Runtime_InteropServices_MarshalAsAttribute)
114 FieldMarshal.SetMarshalAsAttribute(moduleBuilder, PseudoToken, customAttributeBuilder);
115 flags |= (short)ParameterAttributes.HasFieldMarshal;
119 moduleBuilder.SetCustomAttribute(PseudoToken, customAttributeBuilder);
123 public void SetConstant(object defaultValue)
125 flags |= (short)ParameterAttributes.HasDefault;
126 moduleBuilder.AddConstant(PseudoToken, defaultValue);
129 internal void WriteParamRecord(MetadataWriter mw)
133 mw.WriteStringIndex(nameIndex);
136 internal void FixupToken(int parameterToken)
138 if (lazyPseudoToken != 0)
140 moduleBuilder.RegisterTokenFixup(lazyPseudoToken, parameterToken);