コード例 #1
0
        protected virtual void EmitInstantiableBlock(bool isConstructor)
        {
            var ctorBlock = new ConstructorBlock(this.Emitter, this.TypeInfo, false);

            if (isConstructor)
            {
                ctorBlock.Emit();
            }
            else
            {
                this.PushWriter("{0}");
                new MethodBlock(this.Emitter, this.TypeInfo, false).Emit();
                string methods = this.PopWriter(true);
                methods_.Append(methods);
            }
        }
コード例 #2
0
        protected void VisitObjectCreateExpression()
        {
            ObjectCreateExpression objectCreateExpression = this.ObjectCreateExpression;
            int pos = this.Emitter.Output.Length;

            var  resolveResult = this.Emitter.Resolver.ResolveNode(objectCreateExpression.Type, this.Emitter) as TypeResolveResult;
            bool isTypeParam   = resolveResult.Type.Kind == TypeKind.TypeParameter;
            var  type          = isTypeParam ? null : this.Emitter.GetTypeDefinition(objectCreateExpression.Type);

            if (type != null && type.BaseType != null && type.BaseType.FullName == "System.MulticastDelegate")
            {
                bool wrap   = false;
                var  parent = objectCreateExpression.Parent as InvocationExpression;
                if (parent != null && parent.Target == objectCreateExpression)
                {
                    wrap = true;
                }

                if (wrap)
                {
                    this.WriteOpenParentheses();
                }

                objectCreateExpression.Arguments.First().AcceptVisitor(this.Emitter);

                if (wrap)
                {
                    this.WriteCloseParentheses();
                }
                return;
            }

            var argsInfo        = new ArgumentsInfo(this.Emitter, objectCreateExpression);
            var argsExpressions = argsInfo.ArgumentsExpressions;
            var argsNames       = argsInfo.ArgumentsNames;
            var paramsArg       = argsInfo.ParamsExpression;
            var argsCount       = argsExpressions.Count();

            var    invocationResolveResult = this.Emitter.Resolver.ResolveNode(objectCreateExpression, this.Emitter) as InvocationResolveResult;
            string inlineCode = null;

            if (invocationResolveResult != null)
            {
                inlineCode = this.Emitter.GetInline(invocationResolveResult.Member);
            }

            var customCtor     = isTypeParam ? "" : (this.Emitter.Validator.GetCustomConstructor(type) ?? "");
            var hasInitializer = !objectCreateExpression.Initializer.IsNull && objectCreateExpression.Initializer.Elements.Count > 0;

            bool isCollectionInitializer            = false;
            AstNodeCollection <Expression> elements = null;

            if (hasInitializer)
            {
                elements = objectCreateExpression.Initializer.Elements;
                isCollectionInitializer = elements.Count > 0 && elements.First() is ArrayInitializerExpression;
            }

            if (inlineCode == null && Regex.Match(customCtor, @"\s*\{\s*\}\s*").Success)
            {
                this.WriteOpenBrace();
                this.WriteSpace();

                if (hasInitializer)
                {
                    this.WriteObjectInitializer(objectCreateExpression.Initializer.Elements, this.Emitter.AssemblyInfo.PreserveMemberCase, type, invocationResolveResult);
                    this.WriteSpace();
                }
                else if (this.Emitter.Validator.IsObjectLiteral(type))
                {
                    this.WriteObjectInitializer(null, this.Emitter.AssemblyInfo.PreserveMemberCase, type, invocationResolveResult);
                    this.WriteSpace();
                }

                this.WriteCloseBrace();
            }
            else
            {
                if (hasInitializer)
                {
                    this.Write(Bridge.Translator.Emitter.ROOT);
                    this.WriteDot();
                    this.Write(Bridge.Translator.Emitter.MERGE_OBJECT);
                    this.WriteOpenParentheses();
                }

                if (inlineCode != null)
                {
                    new InlineArgumentsBlock(this.Emitter, argsInfo, inlineCode).Emit();
                }
                else
                {
                    if (String.IsNullOrEmpty(customCtor))
                    {
                        objectCreateExpression.Type.AcceptVisitor(this.Emitter);
                        this.WriteOpenParentheses();
                    }
                    else
                    {
                        this.Write(customCtor);
                    }

                    if (!isTypeParam && !this.Emitter.Validator.IsIgnoreType(type) && type.Methods.Count(m => m.IsConstructor && !m.IsStatic) > (type.IsValueType ? 0 : 1))
                    {
                        string name  = OverloadsCollection.Create(this.Emitter, ((InvocationResolveResult)this.Emitter.Resolver.ResolveNode(objectCreateExpression, this.Emitter)).Member).GetOverloadName();
                        int    index = ConstructorBlock.GetCtorIndex(name);
                        this.Write(index);
                        this.WriteComma();
                    }

                    if (invocationResolveResult != null)
                    {
                        string index = Bridge.Translator.Lua.Emitter.GetConstructorIndex(invocationResolveResult.Member);
                        if (index != null)
                        {
                            this.Write(index);
                            this.WriteSpace();
                            this.WriteComma();
                        }
                    }

                    new ExpressionListBlock(this.Emitter, argsExpressions, paramsArg).Emit();
                    this.WriteCloseParentheses();
                }

                if (hasInitializer)
                {
                    this.WriteComma();
                    this.WriteFunction();
                    this.WriteOpenParentheses();
                    this.Write("t");
                    this.WriteCloseParentheses();
                    this.BeginFunctionBlock();

                    bool needComma = false;

                    /*
                     * if (isCollectionInitializer)
                     * {
                     *  this.Write("{");
                     *  this.WriteNewLine();
                     * }
                     * else
                     * {
                     *  this.BeginBlock();
                     * }*/

                    foreach (Expression item in elements)
                    {
                        if (needComma)
                        {
                            this.WriteSemiColon(true);
                        }

                        needComma = true;

                        if (item is NamedExpression)
                        {
                            var namedExpression = (NamedExpression)item;
                            new NameBlock(this.Emitter, namedExpression.Name, namedExpression, namedExpression.Expression).Emit();
                        }
                        else if (item is NamedArgumentExpression)
                        {
                            var namedArgumentExpression = (NamedArgumentExpression)item;
                            new NameBlock(this.Emitter, namedArgumentExpression.Name, namedArgumentExpression, namedArgumentExpression.Expression).Emit();
                        }
                        else if (item is ArrayInitializerExpression)
                        {
                            var arrayInitializer = (ArrayInitializerExpression)item;

                            this.Write("t:add");
                            this.WriteOpenParentheses();

                            foreach (var el in arrayInitializer.Elements)
                            {
                                this.EnsureComma(false);
                                el.AcceptVisitor(this.Emitter);
                                this.Emitter.Comma = true;
                            }

                            this.WriteCloseParentheses();
                            this.Emitter.Comma = false;
                        }
                        else if (item is IdentifierExpression)
                        {
                            var identifierExpression = (IdentifierExpression)item;
                            new IdentifierBlock(this.Emitter, identifierExpression).Emit();
                        }
                    }

                    this.WriteNewLine();
                    this.EndFunctionBlock();
                    this.WriteCloseParentheses();
                }
            }

            //Helpers.CheckValueTypeClone(invocationResolveResult, this.ObjectCreateExpression, this, pos);
        }
コード例 #3
0
ファイル: ClassBlock.cs プロジェクト: yindongfei/bridge.lua
        protected virtual void EmitInstantiableBlock(bool isConstructor)
        {
            /*
            if (this.TypeInfo.IsEnum)
            {
                this.EnsureComma();
                this.Emitter.Comma = true;

                if (this.Emitter.GetTypeDefinition(this.TypeInfo.Type)
                        .CustomAttributes.Any(attr => attr.AttributeType.FullName == "System.FlagsAttribute"))
                {
                    this.EnsureComma();
                    this.Write("$flags: true");
                    this.Emitter.Comma = true;
                }
            }*/

            var ctorBlock = new ConstructorBlock(this.Emitter, this.TypeInfo, false);
            if (this.TypeInfo.HasInstantiable || this.Emitter.Plugins.HasConstructorInjectors(ctorBlock) || this.TypeInfo.ClassType == ClassType.Struct)
            {
                if(isConstructor) {
                    this.EnsureComma();
                    ctorBlock.Emit();
                }
                else {
                    this.PushWriter("{0}");
                    new MethodBlock(this.Emitter, this.TypeInfo, false).Emit();
                    string methods = this.PopWriter(true);
                    methods_.Append(methods);
                }
            }
            else
            {
                //this.Emitter.Comma = false;
            }
        }