本文整理汇总了C#中CodeGenContext.CreateConstructor方法的典型用法代码示例。如果您正苦于以下问题:C# CodeGenContext.CreateConstructor方法的具体用法?C# CodeGenContext.CreateConstructor怎么用?C# CodeGenContext.CreateConstructor使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类CodeGenContext
的用法示例。
在下文中一共展示了CodeGenContext.CreateConstructor方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GenConstructor
private MethodDef GenConstructor(ClassDef Class, CodeGenContext context)
{
// internal Block_N(outer locals ...): base(defining_scope) { ... }
List<Param> parameters = new List<Param>();
foreach (FieldDef field in frameFields)
parameters.Add(new Param(ParamAttr.Default, field.Name(), field.GetFieldType()));
CodeGenContext constructor = context.CreateConstructor(Class, parameters.ToArray());
constructor.ldarg(0); // this
constructor.ldarg(1); // defining_scope
constructor.call(Runtime.Block.ctor);
int N = 0;
foreach (FieldDef field in frameFields)
{
// locals.field = localsN;
constructor.ldarg(0);
constructor.ldarg("locals" + (N++));
constructor.stfld(field);
}
constructor.ret();
constructor.Close();
return constructor.Method;
}
示例2: AddScopeLocals
internal void AddScopeLocals(CodeGenContext context)
{
// ------------------ Start new Context ----------------------------
// [InteropMethod("MyClass")]
// private class ActivationFrame: Ruby.Frame { ... }
frame_def = context.CreateGlobalClass("_Internal", "Frame" + (N++), Runtime.FrameRef);
Scope parentClass;
for (parentClass = this; parentClass != null && !(parentClass is CLASS_OR_MODULE); parentClass = parentClass.parent_scope) ;
string className = "";
if (parentClass != null)
className = ((CLASS_OR_MODULE)parentClass).internal_name;
ClassDef fileClass = FileClass();
string src = "";
if (fileClass != null && fileClass.Name().StartsWith("SourceFile_"))
{
src = fileClass.Name().Substring(11);
frame_def.AddCustomAttribute(Runtime.FrameAttribute.ctor, new Constant[] { new StringConst(src), new StringConst(className) });
}
foreach (string local in locals_list)
CodeGenContext.AddField(frame_def, PERWAPI.FieldAttr.Public, ID.ToDotNetName(local), PrimitiveType.Object);
// internal ActivationFrame(Frame caller): base(caller) { }
CodeGenContext frame_ctor = context.CreateConstructor(frame_def, new Param(ParamAttr.Default, "caller", Runtime.FrameRef));
frame_ctor.ldarg(0);
frame_ctor.ldarg("caller");
frame_ctor.call(Runtime.Frame.ctor);
frame_ctor.ret();
frame_ctor.Close();
// internal string file() {
CodeGenContext file = context.CreateMethod(frame_def, PERWAPI.MethAttr.PublicVirtual, "file", PrimitiveType.String);
file.startMethod(this.location);
// return "thisfile.rb"
file.ldstr(this.location.file);
file.ret();
file.Close();
// internal override string methodName() {
CodeGenContext methodName = context.CreateMethod(frame_def, PERWAPI.MethAttr.PublicVirtual, "methodName", PrimitiveType.String);
methodName.startMethod(this.location);
// return "CurrentMethodName"
methodName.ldstr(CurrentMethodName());
methodName.ret();
methodName.Close();
CreateNestingMethod(frame_def, context);
CreateLastClassMethod(frame_def, context);
// ------------------ Return to Old Context ----------------------
// ActivationFrame frame = new ActivationFrame(caller);
context.ldarg("caller");
context.newobj(frame_ctor.Method);
int frame = context.StoreInTemp("frame", FrameClass, location);
Debug.Assert(frame == 0);
// frame.block_arg = block;
context.ldloc(frame);
LoadBlock0(context);
context.stfld(Runtime.Frame.block_arg);
if (this is BLOCK)
{
// frame.current_block = this;
context.ldloc(frame);
context.ldarg(0);
context.stfld(Runtime.Frame.current_block);
}
}
示例3: GenerateClassForMethod
private FieldDef GenerateClassForMethod(CodeGenContext context)
{
ClassRef baseClass;
if (formals.ShortAndSimple())
baseClass = Runtime.MethodBodyNRef(formals.min_args);
else
baseClass = Runtime.MethodBodyRef;
// private class MyMethod: MethodBody? {
ClassDef methodClass = context.CreateGlobalClass("_Internal", "Method_" + ID.ToDotNetName(method_id), baseClass);
methodClass.AddCustomAttribute(Runtime.InteropMethodAttribute.ctor, System.Text.UnicodeEncoding.UTF8.GetBytes(method_id));
// internal static Calln(Class last_class, object recv, ...) { body }
GenCallMethod(methodClass, context);
// internal MyMethod() {}
CodeGenContext constructor = context.CreateConstructor(methodClass);
constructor.ldarg(0);
constructor.call(Runtime.MethodBodyCtor(baseClass));
constructor.ret();
constructor.Close();
// internal static MyMethod singleton;
FieldDef singleton = CodeGenContext.AddField(methodClass, FieldAttr.PublicStatic, "myRubyMethod", methodClass);
// static MyMethod() {
// singleton = new MyMethod();
// }
CodeGenContext staticConstructor = context.CreateStaticConstructor(methodClass);
staticConstructor.newobj(constructor.Method);
staticConstructor.stsfld(singleton);
staticConstructor.ret();
staticConstructor.Close();
// }
// ------------------- Return to original context -----------------------------
return singleton;
}
示例4: AddInteropConstructor
private void AddInteropConstructor(CodeGenContext context)
{
List<Param> args = new List<Param>();
if (formals.arity < 0)
{
Param param = new Param(ParamAttr.Default, "args", new PERWAPI.ZeroBasedArray(PERWAPI.PrimitiveType.Object));
param.AddCustomAttribute(Runtime.ParamArrayAttribute.ctor, new byte[0]);
args.Add(param);
}
else
for (int i = 1; i <= this.formals.arity; i++)
args.Add(new Param(ParamAttr.Default, "p" + i, PrimitiveType.Object));
// public .ctor(...) {
try
{
// remove existing zero-arg constructor if it exists
if (formals.arity == 0 && CLASS_OR_MODULE.CurrentInteropClass().GetMethodDesc(".ctor", new Type[0]) != null)
CLASS_OR_MODULE.CurrentInteropClass().RemoveMethod(".ctor", new Type[0]);
CodeGenContext method = context.CreateConstructor(CLASS_OR_MODULE.CurrentInteropClass(), args.ToArray());
method.startMethod(this.location);
// call base class constructor
PERWAPI.Method superClassConstructor0 = null;
PERWAPI.Method superClassConstructor1 = null;
PERWAPI.Class superClass = null;
ClassSkeleton superClassSkeleton = context.currentSkeleton.FindClass(CLASS_OR_MODULE.CurrentInteropClass().SuperType.Name());
if (superClassSkeleton != null)
{
superClass = superClassSkeleton.perwapiClass;
superClassConstructor0 = superClass.GetMethodDesc(".ctor", new Type[0]);
if (superClassConstructor0 is MethodDef)
superClassConstructor0 = ((MethodDef)superClassConstructor0).MakeRefOf();
superClassConstructor1 = superClass.GetMethodDesc(".ctor", new Type[] { Runtime.ClassRef });
if (superClassConstructor1 is MethodDef)
superClassConstructor1 = ((MethodDef)superClassConstructor1).MakeRefOf();
}
if (superClassConstructor1 != null)
{
method.ldarg(0);
method.ldsfld(((CLASS_OR_MODULE)this.parent_scope).singletonField);
method.call(superClassConstructor1);
}
else if (superClassConstructor0 != null)
{
method.ldarg(0);
method.call(superClassConstructor0);
}
// return Eval.CallPrivateN(recv, null, "methodId", null, ...);
method.ldarg(0);
method.ldnull();
method.ldstr(method_id);
method.ldnull();
if (formals.arity < 0)
{
method.ldarg(1);
method.call(Runtime.Eval.Call("Private"));
}
else
{
for (int i = 1; i <= args.Count; i++)
method.ldarg(i);
method.call(Runtime.Eval.Call("Private", args.Count));
}
method.pop();
method.ret();
method.Close();
}
catch (PERWAPI.DescriptorException)
{
}
}
示例5: GenConstructor
private MethodDef GenConstructor(ClassDef Class, CodeGenContext context)
{
// internal Eval(MyBlock block) { ... }
CodeGenContext constructor;
if (block_type != null)
constructor = context.CreateConstructor(Class, new Param[] { new Param(ParamAttr.Default, "frame", block_type) });
else
constructor = context.CreateConstructor(Class);
constructor.ldarg(0); // this
constructor.call(Runtime.SystemObject.ctor);
if (block_type != null)
{
for (int depth = 0; true; depth++)
{
FieldInfo field = frame.current_block.GetType().GetField("locals" + depth);
if (field == null)
break;
Field oldField = CodeGenContext.FindField(field);
FieldDef newField = CodeGenContext.AddField(Class, FieldAttr.Public, field.Name, CodeGenContext.FindClass(field.FieldType));
frameFields.Add(newField);
// this.myField = frame.myField
constructor.ldarg(0);
constructor.ldarg("frame");
constructor.ldfld(oldField);
constructor.stfld(newField);
}
}
constructor.ret();
constructor.Close();
return constructor.Method;
}
示例6: GenCode0
//.........这里部分代码省略.........
if (context.currentSkeleton.nestedClasses.ContainsKey(basename))
skeleton = context.currentSkeleton.nestedClasses[basename];
else
skeleton = new ClassSkeleton(basename, classRef);
skeleton.lexicalParent = context.currentSkeleton;
newContext.currentSkeleton = skeleton;
skeleton.lexicalParent.nestedClasses[basename] = skeleton;
if (!superClassFound && this is AST.CLASS && ((AST.CLASS)this).super_class != null && defineInteropClass)
{
// do not add to post pass list if supertype is a built-in Ruby class
//List<string> qualifiedSuperClass = ClassSkeleton.ConstNodeToClassList(((AST.CLASS)this).super_class);
//if (!(qualifiedSuperClass.Count == 1 && Ruby.Runtime.BuiltinClasses.IsBuiltinClass(qualifiedSuperClass[0])))
newContext.postPassList.Add(new ClassSkeletonPostPass(skeleton, interopClass, ((AST.CLASS)this).super_class));
}
// public static Class classname;
int seqNo = 0;
internal_name = basename;
while (FileClass().GetField(internal_name) != null)
internal_name = basename + (seqNo++);
// Define singleton in file class so that it gets initialized by .cctor
singletonField = FileClass().AddField(PERWAPI.FieldAttr.PublicStatic, internal_name, Runtime.ClassRef);
newContext.CurrentRubyClass = singletonField;
// public MyClass() : base(singleton) { };
CodeGenContext class_constructor = null;
if (defineInteropClass)
{
if (interopClass.GetMethod(".ctor", new Type[0]) == null)
{
CodeGenContext class_constructor0 = newContext.CreateConstructor(interopClass);
class_constructor0.ldarg(0);
if (superClassConstructor0 != null)
class_constructor0.call(superClassConstructor0);
else
{
class_constructor0.ldsfld(singletonField);
class_constructor0.call(superClassConstructor1);
}
class_constructor0.ret();
class_constructor0.Close();
}
// public MyClass(Class klass) : base(klass) { };
MethodDef ctor = interopClass.GetMethod(".ctor", new Type[] { Runtime.ClassRef });
//CodeGenContext class_constructor;
if (ctor == null)
{
class_constructor = newContext.CreateConstructor(interopClass, new Param(ParamAttr.Default, "klass", Runtime.ClassRef));
class_constructor.ldarg(0);
class_constructor.ldarg("klass");
class_constructor.call(superClassConstructor1);
class_constructor.ret();
class_constructor.Close();
}
else
{
class_constructor = new CodeGenContext();
class_constructor.Method = ctor;
}
}
示例7: DefineAllocator
internal override FieldDef DefineAllocator(CodeGenContext context, MethodDef ctor)
{
// Conservative - don't create allocator if we're not sure what the base class is
// BBTAG: should be able to create this in all cases now, since we do a test in
// define_alloc_func
//if (super_class != null)
// return null;
ClassRef baseClass = Runtime.MethodBodyNRef(0);
ClassDef allocator = context.CreateGlobalClass("_Internal", "Method_" + internal_name + "_Allocator", baseClass);
context.currentSkeleton.allocator = allocator;
// internal static Call0(Class last_class, object recv, ...) { body }
CodeGenContext Call = context.CreateMethod(allocator, MethAttr.PublicVirtual, "Call0", PrimitiveType.Object, new Param[] {
new Param(ParamAttr.Default, "last_class", Runtime.ClassRef),
new Param(ParamAttr.Default, "recv", PrimitiveType.Object),
new Param(ParamAttr.Default, "caller", Runtime.FrameRef),
new Param(ParamAttr.Default, "block", Runtime.ProcRef)});
// return new MyClass((Class)recv));
Call.ldarg("recv");
Call.cast(Runtime.ClassRef);
Call.newobj(ctor);
Call.ret();
Call.Close();
// internal MyAllocator() {}
CodeGenContext constructor = context.CreateConstructor(allocator);
constructor.ldarg(0);
constructor.call(Runtime.MethodBodyCtor(baseClass));
constructor.ret();
constructor.Close();
// internal static MyAllocator singleton;
FieldDef singleton = CodeGenContext.AddField(allocator, FieldAttr.PublicStatic, "myRubyMethod", allocator);
// static MyAllocator() {
// singleton = new MyAllocator();
// }
CodeGenContext staticConstructor = context.CreateStaticConstructor(allocator);
staticConstructor.newobj(constructor.Method);
staticConstructor.stsfld(singleton);
staticConstructor.ret();
staticConstructor.Close();
return singleton;
}