本文整理汇总了C#中Mono.CSharp.Method.PrepareEmit方法的典型用法代码示例。如果您正苦于以下问题:C# Method.PrepareEmit方法的具体用法?C# Method.PrepareEmit怎么用?C# Method.PrepareEmit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Mono.CSharp.Method
的用法示例。
在下文中一共展示了Method.PrepareEmit方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DoDefineMembers
//.........这里部分代码省略.........
//
// Implementation of ToString () body using string concatenation
//
string_concat = new Binary (Binary.Operator.Addition,
new Binary (Binary.Operator.Addition,
string_concat,
new StringConstant (Compiler.BuiltinTypes, ", " + p.Name + " = ", loc)),
field_to_string);
rs_equals = new Binary (Binary.Operator.LogicalAnd, rs_equals, field_equal);
}
string_concat = new Binary (Binary.Operator.Addition,
string_concat,
new StringConstant (Compiler.BuiltinTypes, " }", loc));
//
// Equals (object obj) override
//
var other_variable_assign = new TemporaryVariableReference (li_other, loc);
equals_block.AddStatement (new StatementExpression (
new SimpleAssign (other_variable_assign,
new As (equals_block.GetParameterReference (0, loc),
current_type, loc), loc)));
Expression equals_test = new Binary (Binary.Operator.Inequality, other_variable, new NullLiteral (loc));
if (rs_equals != null)
equals_test = new Binary (Binary.Operator.LogicalAnd, equals_test, rs_equals);
equals_block.AddStatement (new Return (equals_test, loc));
equals.Block = equals_block;
equals.Define ();
equals.PrepareEmit ();
Members.Add (equals);
//
// GetHashCode () override
//
Method hashcode = new Method (this, new TypeExpression (Compiler.BuiltinTypes.Int, loc),
Modifiers.PUBLIC | Modifiers.OVERRIDE | Modifiers.DEBUGGER_HIDDEN,
new MemberName ("GetHashCode", loc),
ParametersCompiled.EmptyReadOnlyParameters, null);
//
// Modified FNV with good avalanche behavior and uniform
// distribution with larger hash sizes.
//
// const int FNV_prime = 16777619;
// int hash = (int) 2166136261;
// foreach (int d in data)
// hash = (hash ^ d) * FNV_prime;
// hash += hash << 13;
// hash ^= hash >> 7;
// hash += hash << 3;
// hash ^= hash >> 17;
// hash += hash << 5;
ToplevelBlock hashcode_top = new ToplevelBlock (Compiler, loc);
Block hashcode_block = new Block (hashcode_top, loc, loc);
hashcode_top.AddStatement (new Unchecked (hashcode_block, loc));
var li_hash = LocalVariable.CreateCompilerGenerated (Compiler.BuiltinTypes.Int, hashcode_top, loc);
hashcode_block.AddStatement (new BlockVariable (new TypeExpression (li_hash.Type, loc), li_hash));
LocalVariableReference hash_variable_assign = new LocalVariableReference (li_hash, loc);
hashcode_block.AddStatement (new StatementExpression (
示例2: CreateHoistedBaseCallProxy
//
// Creates a proxy base method call inside this container for hoisted base member calls
//
public MethodSpec CreateHoistedBaseCallProxy (ResolveContext rc, MethodSpec method)
{
Method proxy_method;
//
// One proxy per base method is enough
//
if (hoisted_base_call_proxies == null) {
hoisted_base_call_proxies = new Dictionary<MethodSpec, Method> ();
proxy_method = null;
} else {
hoisted_base_call_proxies.TryGetValue (method, out proxy_method);
}
if (proxy_method == null) {
string name = CompilerGeneratedContainer.MakeName (method.Name, null, "BaseCallProxy", hoisted_base_call_proxies.Count);
MemberName member_name;
TypeArguments targs = null;
TypeSpec return_type = method.ReturnType;
var local_param_types = method.Parameters.Types;
if (method.IsGeneric) {
//
// Copy all base generic method type parameters info
//
var hoisted_tparams = method.GenericDefinition.TypeParameters;
var tparams = new TypeParameters ();
targs = new TypeArguments ();
targs.Arguments = new TypeSpec[hoisted_tparams.Length];
for (int i = 0; i < hoisted_tparams.Length; ++i) {
var tp = hoisted_tparams[i];
var local_tp = new TypeParameter (tp, null, new MemberName (tp.Name, Location), null);
tparams.Add (local_tp);
targs.Add (new SimpleName (tp.Name, Location));
targs.Arguments[i] = local_tp.Type;
}
member_name = new MemberName (name, tparams, Location);
//
// Mutate any method type parameters from original
// to newly created hoisted version
//
var mutator = new TypeParameterMutator (hoisted_tparams, tparams);
return_type = mutator.Mutate (return_type);
local_param_types = mutator.Mutate (local_param_types);
} else {
member_name = new MemberName (name);
}
var base_parameters = new Parameter[method.Parameters.Count];
for (int i = 0; i < base_parameters.Length; ++i) {
var base_param = method.Parameters.FixedParameters[i];
base_parameters[i] = new Parameter (new TypeExpression (local_param_types [i], Location),
base_param.Name, base_param.ModFlags, null, Location);
base_parameters[i].Resolve (this, i);
}
var cloned_params = ParametersCompiled.CreateFullyResolved (base_parameters, method.Parameters.Types);
if (method.Parameters.HasArglist) {
cloned_params.FixedParameters[0] = new Parameter (null, "__arglist", Parameter.Modifier.NONE, null, Location);
cloned_params.Types[0] = Module.PredefinedTypes.RuntimeArgumentHandle.Resolve ();
}
// Compiler generated proxy
proxy_method = new Method (this, new TypeExpression (return_type, Location),
Modifiers.PRIVATE | Modifiers.COMPILER_GENERATED | Modifiers.DEBUGGER_HIDDEN,
member_name, cloned_params, null);
var block = new ToplevelBlock (Compiler, proxy_method.ParameterInfo, Location) {
IsCompilerGenerated = true
};
var mg = MethodGroupExpr.CreatePredefined (method, method.DeclaringType, Location);
mg.InstanceExpression = new BaseThis (method.DeclaringType, Location);
if (targs != null)
mg.SetTypeArguments (rc, targs);
// Get all the method parameters and pass them as arguments
var real_base_call = new Invocation (mg, block.GetAllParametersArguments ());
Statement statement;
if (method.ReturnType.Kind == MemberKind.Void)
statement = new StatementExpression (real_base_call);
else
statement = new Return (real_base_call, Location);
block.AddStatement (statement);
proxy_method.Block = block;
members.Add (proxy_method);
proxy_method.Define ();
proxy_method.PrepareEmit ();
hoisted_base_call_proxies.Add (method, proxy_method);
//.........这里部分代码省略.........
示例3: GenerateNumberMatcher
Method GenerateNumberMatcher ()
{
var loc = Location;
var parameters = ParametersCompiled.CreateFullyResolved (
new [] {
new Parameter (new TypeExpression (Compiler.BuiltinTypes.Object, loc), "obj", 0, null, loc),
new Parameter (new TypeExpression (Compiler.BuiltinTypes.Object, loc), "value", 0, null, loc),
new Parameter (new TypeExpression (Compiler.BuiltinTypes.Bool, loc), "enumType", 0, null, loc),
},
new [] {
Compiler.BuiltinTypes.Object,
Compiler.BuiltinTypes.Object,
Compiler.BuiltinTypes.Bool
});
var m = new Method (this, new TypeExpression (Compiler.BuiltinTypes.Bool, loc),
Modifiers.PUBLIC | Modifiers.STATIC | Modifiers.DEBUGGER_HIDDEN, new MemberName ("NumberMatcher", loc),
parameters, null);
parameters [0].Resolve (m, 0);
parameters [1].Resolve (m, 1);
parameters [2].Resolve (m, 2);
ToplevelBlock top_block = new ToplevelBlock (Compiler, parameters, loc);
m.Block = top_block;
//
// if (enumType)
// return Equals (obj, value);
//
var equals_args = new Arguments (2);
equals_args.Add (new Argument (top_block.GetParameterReference (0, loc)));
equals_args.Add (new Argument (top_block.GetParameterReference (1, loc)));
var if_type = new If (
top_block.GetParameterReference (2, loc),
new Return (new Invocation (new SimpleName ("Equals", loc), equals_args), loc),
loc);
top_block.AddStatement (if_type);
//
// if (obj is Enum || obj == null)
// return false;
//
var if_enum = new If (
new Binary (Binary.Operator.LogicalOr,
new Is (top_block.GetParameterReference (0, loc), new TypeExpression (Compiler.BuiltinTypes.Enum, loc), loc),
new Binary (Binary.Operator.Equality, top_block.GetParameterReference (0, loc), new NullLiteral (loc))),
new Return (new BoolLiteral (Compiler.BuiltinTypes, false, loc), loc),
loc);
top_block.AddStatement (if_enum);
var system_convert = new MemberAccess (new QualifiedAliasMember ("global", "System", loc), "Convert", loc);
var expl_block = new ExplicitBlock (top_block, loc, loc);
//
// var converted = System.Convert.ChangeType (obj, System.Convert.GetTypeCode (value));
//
var lv_converted = LocalVariable.CreateCompilerGenerated (Compiler.BuiltinTypes.Object, top_block, loc);
var arguments_gettypecode = new Arguments (1);
arguments_gettypecode.Add (new Argument (top_block.GetParameterReference (1, loc)));
var gettypecode = new Invocation (new MemberAccess (system_convert, "GetTypeCode", loc), arguments_gettypecode);
var arguments_changetype = new Arguments (1);
arguments_changetype.Add (new Argument (top_block.GetParameterReference (0, loc)));
arguments_changetype.Add (new Argument (gettypecode));
var changetype = new Invocation (new MemberAccess (system_convert, "ChangeType", loc), arguments_changetype);
expl_block.AddStatement (new StatementExpression (new SimpleAssign (new LocalVariableReference (lv_converted, loc), changetype, loc)));
//
// return converted.Equals (value)
//
var equals_arguments = new Arguments (1);
equals_arguments.Add (new Argument (top_block.GetParameterReference (1, loc)));
var equals_invocation = new Invocation (new MemberAccess (new LocalVariableReference (lv_converted, loc), "Equals"), equals_arguments);
expl_block.AddStatement (new Return (equals_invocation, loc));
var catch_block = new ExplicitBlock (top_block, loc, loc);
catch_block.AddStatement (new Return (new BoolLiteral (Compiler.BuiltinTypes, false, loc), loc));
top_block.AddStatement (new TryCatch (expl_block, new List<Catch> () {
new Catch (catch_block, loc)
}, loc, false));
m.Define ();
m.PrepareEmit ();
AddMember (m);
return m;
}