Skip to content

Commit

Permalink
Add MetadataToken getter override to builder classes (#43330)
Browse files Browse the repository at this point in the history
  • Loading branch information
steveharter authored Oct 15, 2020
1 parent 9ede31a commit b72b13e
Show file tree
Hide file tree
Showing 42 changed files with 267 additions and 544 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ private void SetCustomAttributeNoLock(ConstructorInfo con, byte[] binaryAttribut
TypeBuilder.DefineCustomAttribute(
_manifestModuleBuilder, // pass in the in-memory assembly module
AssemblyBuilderData.AssemblyDefToken,
_manifestModuleBuilder.GetConstructorToken(con).Token,
_manifestModuleBuilder.GetConstructorToken(con),
binaryAttribute,
false,
typeof(DebuggableAttribute) == con.DeclaringType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ internal ConstructorBuilder(string name, MethodAttributes attributes, CallingCon

m_methodBuilder.GetMethodSignature().InternalGetSignature(out _);

m_methodBuilder.GetToken();
int dummy = m_methodBuilder.MetadataToken;
}

internal ConstructorBuilder(string name, MethodAttributes attributes, CallingConventions callingConvention,
Expand Down Expand Up @@ -60,7 +60,7 @@ public override string ToString()
#endregion

#region MemberInfo Overrides
internal int MetadataTokenInternal => m_methodBuilder.MetadataTokenInternal;
public override int MetadataToken => m_methodBuilder.MetadataToken;

public override Module Module => m_methodBuilder.Module;

Expand Down Expand Up @@ -122,11 +122,6 @@ public override bool IsDefined(Type attributeType, bool inherit)
#endregion

#region Public Members
public MethodToken GetToken()
{
return m_methodBuilder.GetToken();
}

public ParameterBuilder DefineParameter(int iSequence, ParameterAttributes attributes, string? strParamName)
{
// Theoretically we shouldn't allow iSequence to be 0 because in reflection ctors don't have
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -527,7 +527,7 @@ private static void EmitValue(BinaryWriter writer, Type type, object? value)
// return the byte interpretation of the custom attribute
internal void CreateCustomAttribute(ModuleBuilder mod, int tkOwner)
{
CreateCustomAttribute(mod, tkOwner, mod.GetConstructorToken(m_con).Token, false);
CreateCustomAttribute(mod, tkOwner, mod.GetConstructorToken(m_con), false);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public FieldBuilder DefineLiteral(string literalName, object? literalValue)
}

// Get the internal metadata token for this class.
public TypeToken TypeToken => m_typeBuilder.TypeToken;
internal int TypeToken => m_typeBuilder.TypeToken;


// return the underlying field for the enum
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ internal EventBuilder(
EventAttributes attr, // event attribute such as Public, Private, and Protected defined above
// int eventType, // event type
TypeBuilder type, // containing type
EventToken evToken)
int evToken)
{
m_name = name;
m_module = mod;
Expand All @@ -41,7 +41,7 @@ internal EventBuilder(

// Return the Token for this event within the TypeBuilder that the
// event is defined within.
public EventToken GetEventToken()
internal int GetEventToken()
{
return m_evToken;
}
Expand All @@ -57,9 +57,9 @@ private void SetMethodSemantics(MethodBuilder mdBuilder, MethodSemanticsAttribut
ModuleBuilder module = m_module;
TypeBuilder.DefineMethodSemantics(
new QCallModule(ref module),
m_evToken.Token,
m_evToken,
semantics,
mdBuilder.GetToken().Token);
mdBuilder.MetadataToken);
}

public void SetAddOnMethod(MethodBuilder mdBuilder)
Expand Down Expand Up @@ -94,8 +94,8 @@ public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)

TypeBuilder.DefineCustomAttribute(
m_module,
m_evToken.Token,
m_module.GetConstructorToken(con).Token,
m_evToken,
m_module.GetConstructorToken(con),
binaryAttribute,
false, false);
}
Expand All @@ -108,12 +108,12 @@ public void SetCustomAttribute(CustomAttributeBuilder customBuilder)
throw new ArgumentNullException(nameof(customBuilder));
}
m_type.ThrowIfCreated();
customBuilder.CreateCustomAttribute(m_module, m_evToken.Token);
customBuilder.CreateCustomAttribute(m_module, m_evToken);
}

// These are package private so that TypeBuilder can access them.
private string m_name; // The name of the event
private EventToken m_evToken; // The token of this event
private int m_evToken; // The token of this event
private ModuleBuilder m_module;
private EventAttributes m_attributes;
private TypeBuilder m_type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public sealed class FieldBuilder : FieldInfo
{
#region Private Data Members
private int m_fieldTok;
private FieldToken m_tkField;
private TypeBuilder m_typeBuilder;
private string m_fieldName;
private FieldAttributes m_Attributes;
Expand Down Expand Up @@ -48,9 +47,7 @@ internal FieldBuilder(TypeBuilder typeBuilder, string fieldName, Type type,

ModuleBuilder module = m_typeBuilder.GetModuleBuilder();
m_fieldTok = TypeBuilder.DefineField(new QCallModule(ref module),
typeBuilder.TypeToken.Token, fieldName, signature, sigLength, m_Attributes);

m_tkField = new FieldToken(m_fieldTok, type);
typeBuilder.TypeToken, fieldName, signature, sigLength, m_Attributes);
}

#endregion
Expand All @@ -59,12 +56,12 @@ internal FieldBuilder(TypeBuilder typeBuilder, string fieldName, Type type,
internal void SetData(byte[]? data, int size)
{
ModuleBuilder module = m_typeBuilder.GetModuleBuilder();
ModuleBuilder.SetFieldRVAContent(new QCallModule(ref module), m_tkField.Token, data, size);
ModuleBuilder.SetFieldRVAContent(new QCallModule(ref module), m_fieldTok, data, size);
}
#endregion

#region MemberInfo Overrides
internal int MetadataTokenInternal => m_fieldTok;
public override int MetadataToken => m_fieldTok;

public override Module Module => m_typeBuilder.Module;

Expand Down Expand Up @@ -140,17 +137,12 @@ public override bool IsDefined(Type attributeType, bool inherit)
#endregion

#region Public Members
public FieldToken GetToken()
{
return m_tkField;
}

public void SetOffset(int iOffset)
{
m_typeBuilder.ThrowIfCreated();

ModuleBuilder module = m_typeBuilder.GetModuleBuilder();
TypeBuilder.SetFieldLayoutOffset(new QCallModule(ref module), GetToken().Token, iOffset);
TypeBuilder.SetFieldLayoutOffset(new QCallModule(ref module), m_fieldTok, iOffset);
}

public void SetConstant(object? defaultValue)
Expand All @@ -164,7 +156,7 @@ public void SetConstant(object? defaultValue)
throw new ArgumentException(SR.Argument_ConstantNull);
}

TypeBuilder.SetConstantValue(m_typeBuilder.GetModuleBuilder(), GetToken().Token, m_fieldType, defaultValue);
TypeBuilder.SetConstantValue(m_typeBuilder.GetModuleBuilder(), m_fieldTok, m_fieldType, defaultValue);
}

public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
Expand All @@ -180,7 +172,7 @@ public void SetCustomAttribute(ConstructorInfo con, byte[] binaryAttribute)
m_typeBuilder.ThrowIfCreated();

TypeBuilder.DefineCustomAttribute(module,
m_tkField.Token, module.GetConstructorToken(con).Token, binaryAttribute, false, false);
m_fieldTok, module.GetConstructorToken(con), binaryAttribute, false, false);
}

public void SetCustomAttribute(CustomAttributeBuilder customBuilder)
Expand All @@ -192,7 +184,7 @@ public void SetCustomAttribute(CustomAttributeBuilder customBuilder)

ModuleBuilder? module = m_typeBuilder.Module as ModuleBuilder;

customBuilder.CreateCustomAttribute(module!, m_tkField.Token);
customBuilder.CreateCustomAttribute(module!, m_fieldTok);
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public override bool Equals(object? o)

public override bool IsByRefLike => false;

internal int MetadataTokenInternal => m_type.MetadataTokenInternal;
public override int MetadataToken => m_type.MetadataToken;
#endregion

#region Type Overrides
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ public virtual void EmitCalli(OpCode opcode, CallingConventions callingConventio
UpdateStackSize(OpCodes.Calli, stackchange);

RecordTokenFixup();
PutInteger4(modBuilder.GetSignatureToken(sig).Token);
PutInteger4(modBuilder.GetSignatureToken(sig));
}

public virtual void EmitCalli(OpCode opcode, CallingConvention unmanagedCallConv, Type? returnType, Type[]? parameterTypes)
Expand Down Expand Up @@ -585,7 +585,7 @@ public virtual void EmitCalli(OpCode opcode, CallingConvention unmanagedCallConv
EnsureCapacity(7);
Emit(OpCodes.Calli);
RecordTokenFixup();
PutInteger4(modBuilder.GetSignatureToken(sig).Token);
PutInteger4(modBuilder.GetSignatureToken(sig));
}

public virtual void EmitCall(OpCode opcode, MethodInfo methodInfo, Type[]? optionalParameterTypes)
Expand Down Expand Up @@ -630,9 +630,9 @@ public virtual void Emit(OpCode opcode, SignatureHelper signature)

int stackchange = 0;
ModuleBuilder modBuilder = (ModuleBuilder)m_methodBuilder.Module;
SignatureToken sig = modBuilder.GetSignatureToken(signature);
int sig = modBuilder.GetSignatureToken(signature);

int tempVal = sig.Token;
int tempVal = sig;

EnsureCapacity(7);
InternalEmit(opcode);
Expand Down Expand Up @@ -709,13 +709,13 @@ public virtual void Emit(OpCode opcode, Type cls)
if (opcode == OpCodes.Ldtoken && cls != null && cls.IsGenericTypeDefinition)
{
// This gets the token for the generic type definition if cls is one.
tempVal = modBuilder.GetTypeToken(cls).Token;
tempVal = modBuilder.GetTypeToken(cls);
}
else
{
// This gets the token for the generic type instantiated on the formal parameters
// if cls is a generic type definition.
tempVal = modBuilder.GetTypeTokenInternal(cls!).Token;
tempVal = modBuilder.GetTypeTokenInternal(cls!);
}

EnsureCapacity(7);
Expand Down Expand Up @@ -800,7 +800,7 @@ public virtual void Emit(OpCode opcode, Label[] labels)
public virtual void Emit(OpCode opcode, FieldInfo field)
{
ModuleBuilder modBuilder = (ModuleBuilder)m_methodBuilder.Module;
int tempVal = modBuilder.GetFieldToken(field).Token;
int tempVal = modBuilder.GetFieldToken(field);
EnsureCapacity(7);
InternalEmit(opcode);
RecordTokenFixup();
Expand All @@ -814,7 +814,7 @@ public virtual void Emit(OpCode opcode, string str)
// fixups if the module is persisted to a PE.

ModuleBuilder modBuilder = (ModuleBuilder)m_methodBuilder.Module;
int tempVal = modBuilder.GetStringConstant(str).Token;
int tempVal = modBuilder.GetStringConstant(str);
EnsureCapacity(7);
InternalEmit(opcode);
PutInteger4(tempVal);
Expand Down
Loading

0 comments on commit b72b13e

Please sign in to comment.