| 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 |
[email protected] |
| 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 |
switch (customAttributeBuilder.KnownCA) |
| 100 |
{ |
| 101 |
case KnownCA.InAttribute: |
| 102 |
flags |= (short)ParameterAttributes.In; |
| 103 |
break; |
| 104 |
case KnownCA.OutAttribute: |
| 105 |
flags |= (short)ParameterAttributes.Out; |
| 106 |
break; |
| 107 |
case KnownCA.OptionalAttribute: |
| 108 |
flags |= (short)ParameterAttributes.Optional; |
| 109 |
break; |
| 110 |
case KnownCA.MarshalAsAttribute: |
| 111 |
FieldMarshal.SetMarshalAsAttribute(moduleBuilder, PseudoToken, customAttributeBuilder); |
| 112 |
flags |= (short)ParameterAttributes.HasFieldMarshal; |
| 113 |
break; |
| 114 |
default: |
| 115 |
moduleBuilder.SetCustomAttribute(PseudoToken, customAttributeBuilder); |
| 116 |
break; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
public void SetConstant(object defaultValue) |
| 121 |
{ |
| 122 |
flags |= (short)ParameterAttributes.HasDefault; |
| 123 |
moduleBuilder.AddConstant(PseudoToken, defaultValue); |
| 124 |
} |
| 125 |
|
| 126 |
internal void WriteParamRecord(MetadataWriter mw) |
| 127 |
{ |
| 128 |
mw.Write(flags); |
| 129 |
mw.Write(sequence); |
| 130 |
mw.WriteStringIndex(nameIndex); |
| 131 |
} |
| 132 |
|
| 133 |
internal void FixupToken(int parameterToken) |
| 134 |
{ |
| 135 |
if (lazyPseudoToken != 0) |
| 136 |
{ |
| 137 |
moduleBuilder.RegisterTokenFixup(lazyPseudoToken, parameterToken); |
| 138 |
} |
| 139 |
} |
| 140 |
} |
| 141 |
} |