Ejemplo n.º 1
0
        /// <summary>Creates a specific catch clause with given <paramref name="exceptionType"/> and <paramref name="catchBody"/>.</summary>
        /// <param name="exceptionType">Type of the exception to catch.</param>
        /// <param name="catchBody">Body of the created catch.</param>
        /// <param name="variableName">A name for catch variable.</param>
        public ISpecificCatchClause CreateSpecificCatchClause(IDeclaredType exceptionType, IBlock catchBody, string variableName)
        {
            var tryStatement = _factory.CreateStatement("try {} catch(Exception $0) {}", variableName) as ITryStatement;

            if (tryStatement == null)
            {
                return(null);
            }

            var catchClause = tryStatement.Catches[0] as ISpecificCatchClause;

            if (catchClause == null)
            {
                return(null);
            }

            if (catchBody == null)
            {
                catchBody = _factory.CreateBlock("{$1    // TODO: Handle the $0 $1}",
                                                 exceptionType.GetClrName().ShortName, Environment.NewLine);
            }

            if (exceptionType != null)
            {
                var exceptionDeclaration = catchClause.ExceptionDeclaration;
                if (exceptionDeclaration == null)
                {
                    return(null);
                }

#if R8
                var declaredTypeUsageNode = _factory.CreateDeclaredTypeUsageNode(exceptionType);
#else
                var declaredTypeUsageNode = _factory.CreateDeclaredTypeUsageNode(exceptionType, catchBody);
#endif
#if R2016_3
                catchClause.SetExceptionTypeUsage(declaredTypeUsageNode);
#else
                exceptionDeclaration.SetDeclaredTypeUsage(declaredTypeUsageNode);
#endif
            }

            catchClause.SetBody(catchBody);
            return(catchClause);
        }
Ejemplo n.º 2
0
        private static void GenerateDisposeBody(CSharpGeneratorContext context, ICSharpFunctionDeclaration methodDeclaration, ICollection <GeneratorDeclaredElement <ITypeOwner> > elements, CSharpElementFactory factory)
        {
            var builder = new StringBuilder();
            var owner   = (IParametersOwner)methodDeclaration.DeclaredElement;

            if (owner == null)
            {
                return;
            }

            var typeElement = context.ClassDeclaration.DeclaredElement;
            var args        = new List <object>();

            if (elements.Count == 0)
            {
                if (typeElement is IClass)
                {
                    builder.Append("base.Dispose();");
                }
                methodDeclaration.SetBody(factory.CreateBlock("{" + builder + "}", args.ToArray()));
                return;
            }

            foreach (var element in elements)
            {
                var typeOwner = element.DeclaredElement;
                var type      = typeOwner.Type;
                if (type.IsReferenceType() && context.GetElementOptionValue(element, CSharpBuilderOptions.CanBeNull) == bool.TrueString)
                {
                    builder.Append("if ($" + args.Count + " != null) $" + args.Count + ".Dispose();");
                }
                else
                {
                    builder.Append("$" + args.Count + ".Dispose();");
                }
                args.Add(typeOwner);
            }
            methodDeclaration.SetBody(factory.CreateBlock("{" + builder + "}", args.ToArray()));
        }
        private void Generate(CSharpGeneratorContext context,
                              IMethodDeclaration declaration,
                              IList <GeneratorDeclaredElement <ITypeOwner> > elements,
                              CSharpElementFactory factory)
        {
            var builder = new StringBuilder();

            builder.Append("{").AppendFormat("{0}(bytes, 0);", ReadMethodGenerator.Instance.MethodName).Append("}");

            IBlock body = factory.CreateBlock(builder.ToString());

            declaration.SetBody(body);
        }
        private static void Generate(CSharpGeneratorContext context,
                                     IMethodDeclaration declaration,
                                     IList <GeneratorDeclaredElement <ITypeOwner> > elements,
                                     CSharpElementFactory factory)
        {
            var owner = (IParametersOwner)declaration.DeclaredElement;

            if (owner == null)
            {
                return;
            }

            var ctx = new TypeHandlingContext(context);

            var size = ctx.GetSizeVariableName();

            ctx.Builder.Append("{");
            ctx.Builder.AppendFormat("var {0} = 0;", size);
            if (elements.Count > 0)
            {
                foreach (var element in elements)
                {
                    ctx.Resolve(element.DeclaredElement);

                    ITypeHandler handler = TypeHandlers.All.SingleOrDefault(h => h.CanHandle(ctx));
                    if (handler != null)
                    {
                        handler.HandleGetSize(ctx);
                    }
                }
            }
            ctx.Builder.AppendFormat("return {0};", size);
            ctx.Builder.Append("}");

            IBlock body = factory.CreateBlock(ctx.Builder.ToString(), ctx.Args.ToArray());

            declaration.SetBody(body);
        }
        protected override void Generate(CSharpGeneratorContext context,
                                         IMethodDeclaration declaration,
                                         IList <GeneratorDeclaredElement <ITypeOwner> > elements,
                                         CSharpElementFactory factory)
        {
            var owner = (IParametersOwner)declaration.DeclaredElement;

            if (owner == null)
            {
                return;
            }

            var ctx = new TypeHandlingContext(context);

            ctx.Builder.Append("{");
            ctx.Builder.Append("var index = startIndex;");
            ctx.Builder.Append("Byte[] tmp;");
            if (elements.Count > 0)
            {
                foreach (var element in elements)
                {
                    ctx.Resolve(element.DeclaredElement);

                    ITypeHandler handler = TypeHandlers.All.SingleOrDefault(h => h.CanHandle(ctx));
                    if (handler != null)
                    {
                        handler.HandleRead(ctx);
                    }
                }
            }
            ctx.Builder.Append("return index;");
            ctx.Builder.Append("}");

            IBlock body = factory.CreateBlock(ctx.Builder.ToString(), ctx.Args.ToArray());

            declaration.SetBody(body);
        }