Esempio n. 1
0
        private void InjectStaticInitializerIfNoneSpecified(TypeNode typeNode)
        {
            if (typeNode.NodeType == NodeType.EnumNode)
            {
                return;
            }
            MemberList staticCons = typeNode.GetMembersNamed(StandardIds.CCtor);

            if (staticCons != null && staticCons.Count > 0)
            {
                return;
            }
            StatementList statements = null;
            MemberList    members    = typeNode.Members;

            for (int i = 0, n = members == null ? 0 : members.Count; i < n; i++)
            {
                Field f = members[i] as Field;
                if (f == null)
                {
                    continue;
                }
                if (!f.IsStatic)
                {
                    continue;
                }
                if (f.IsLiteral)
                {
                    continue;
                }
                if (f.Initializer == null)
                {
                    continue;
                }
                statements = new StatementList(1);
                break;
            }
            if (statements == null && typeNode.Contract != null && typeNode.Contract.FrameField != null)
            {
                statements = new StatementList(1);
            }
            if (statements != null)
            {
                FieldInitializerBlock finitBlock = new FieldInitializerBlock(typeNode, true);
                statements.Add(finitBlock);
                StaticInitializer cctor = new StaticInitializer(typeNode, null, new Block(statements));
                typeNode.Members.Add(cctor);
                if (typeNode.PartiallyDefines != null)
                {
                    staticCons = typeNode.PartiallyDefines.GetMembersNamed(StandardIds.CCtor);
                    if (staticCons == null || staticCons.Count == 0)
                    {
                        finitBlock.Type     = typeNode.PartiallyDefines;
                        cctor.DeclaringType = typeNode.PartiallyDefines;
                        typeNode.PartiallyDefines.Members.Add(cctor);
                    }
                }
            }
        }
Esempio n. 2
0
 public KernelRegistry()
 {
     _class = new Class {
         Name    = Identifier.For("ComputeKernels"),
         Members = new MemberList(),
         Flags   = TypeFlags.Sealed,
     };
     _initalizer = new StaticInitializer {
         DeclaringType = _class,
         Body          = new Block {
             Statements = new StatementList(),
         }
     };
     _class.Members.Add(_initalizer);
     _registeredKernels = new Dictionary <String, Field>();
 }
Esempio n. 3
0
 public virtual StaticInitializer VisitStaticInitializer(StaticInitializer cons, StaticInitializer changes, StaticInitializer deletions, StaticInitializer insertions){
   return (StaticInitializer)this.VisitMethod(cons, changes, deletions, insertions);
 }
Esempio n. 4
0
        public static void report(Node node, int shift)
        {
            if (node == null)
            {
                indent(shift);
                Console.WriteLine("NULL ENTITY");
                return;
            }

            switch (node.NodeType)
            {
            case NodeType.AliasDefinition:
            {
                AliasDefinition alias = node as AliasDefinition;
                indent(shift);
                Console.WriteLine("using {0} = {1};", alias.Alias.Name, alias.AliasedUri.Name);
                break;
            }

            case NodeType.CompilationUnit:
            case NodeType.CompilationUnitSnippet:
            {
                CompilationUnit cu = node as CompilationUnit;

                for (int i = 0, n = cu.Nodes.Length; i < n; i++)
                {
                    report(cu.Nodes[i], 0);
                }
                break;
            }

            case NodeType.Namespace:
            {
                Namespace ns = node as Namespace;

                if (ns.UsedNamespaces != null && ns.UsedNamespaces.Length != 0)
                {
                    indent(shift);
                    Console.WriteLine("using ");
                    for (int i = 0, n = ns.UsedNamespaces.Length; i < n; i++)
                    {
                        Console.Write("{0}", ns.UsedNamespaces[i].Namespace.Name);
                        if (i < n - 1)
                        {
                            Console.Write(", ");
                        }
                    }
                    Console.WriteLine();
                }

                indent(shift);
                Console.WriteLine("namespace {0}", ns.FullNameId.Name);

                indent(shift);
                Console.WriteLine("{");

                if (ns.AliasDefinitions != null && ns.AliasDefinitions.Length != 0)
                {
                    for (int i = 0, n = ns.AliasDefinitions.Length; i < n; i++)
                    {
                        report(ns.AliasDefinitions[i], shift + ind);
                    }
                }

                if (ns.NestedNamespaces != null && ns.NestedNamespaces.Length != 0)
                {
                    for (int i = 0, n = ns.NestedNamespaces.Length; i < n; i++)
                    {
                        report(ns.NestedNamespaces[i], shift + ind);
                    }
                }

                if (ns.Types != null && ns.Types.Length != 0)
                {
                    for (int i = 0, n = ns.Types.Length; i < n; i++)
                    {
                        report(ns.Types[i], shift + ind);
                    }
                }

                indent(shift);
                Console.WriteLine("}");

                break;
            }

            case NodeType.Class:
            {
                Class cls = node as Class;

                if (cls == SystemTypes.Object)
                {
                    Console.Write(cls.Name);
                    break;
                }

                indent(shift);

                if (cls.IsAbstract)
                {
                    Console.Write("abstract ");
                }
                if (cls.IsPrivate)
                {
                    Console.Write("private ");
                }
                else if (cls.IsPublic)
                {
                    Console.Write("public ");
                }
                else
                {
                    Console.Write("internal ");                             // ??????????????
                }
                if (cls.IsSealed)
                {
                    Console.Write("sealed ");
                }

                Console.Write("class ");
                if (cls.DeclaringType != null)
                {
                    Console.Write("{0}::", cls.DeclaringType.Name.Name);
                }
                Console.Write("{0}", cls.Name != null?cls.Name.Name:"<NONAME>");

                if (cls.BaseClass != null)
                {
                    Console.Write(" : {0}", cls.BaseClass.Name.Name);
                }

                if (cls.Interfaces != null && cls.Interfaces.Length != 0)
                {
                    if (cls.BaseClass != null)
                    {
                        Console.Write(",");
                    }
                    else
                    {
                        Console.Write(" :");
                    }

                    for (int i = 0, n = cls.Interfaces.Length; i < n; i++)
                    {
                        Interface interfac = cls.Interfaces[i];
                        if (interfac != null)
                        {
                            Console.Write(" {0}", interfac.Name.Name);
                        }
                        if (i < n - 1)
                        {
                            Console.Write(",");
                        }
                    }
                }
                Console.WriteLine();

                indent(shift);
                Console.WriteLine("{");

                if (cls.Members != null && cls.Members.Length != 0)
                {
                    for (int i = 0, n = cls.Members.Length; i < n; i++)
                    {
                        Member member = cls.Members[i];

                        if (member == null)
                        {
                            indent(shift + ind);
                            Console.WriteLine("<UNRESOLVED MEMBER>");
                            continue;
                        }
                        report(member, shift + ind);
                    }
                }
                indent(shift);
                Console.WriteLine("}");

                break;
            }

            case NodeType.Struct:
            {
                Struct struc = node as Struct;

                indent(shift);

                if (struc.IsAbstract)
                {
                    Console.Write("abstract ");
                }
                if (struc.IsPrivate)
                {
                    Console.Write("private ");
                }
                else if (struc.IsPublic)
                {
                    Console.Write("public ");
                }
                else
                {
                    Console.Write("internal ");                               // ??????????????
                }
                if (struc.IsSealed)
                {
                    Console.Write("sealed ");
                }

                Console.Write("struct ");
                if (struc.DeclaringType != null)
                {
                    Console.Write("{0}::", struc.DeclaringType.Name.Name);
                }
                Console.Write("{0}", struc.Name != null?struc.Name.Name:"<NONAME>");

                if (struc.Interfaces != null && struc.Interfaces.Length != 0)
                {
                    Console.Write(" :");
                    for (int i = 0, n = struc.Interfaces.Length; i < n; i++)
                    {
                        Interface interfac = struc.Interfaces[i];
                        if (interfac != null)
                        {
                            Console.Write(" {0}", interfac.Name.Name);
                        }
                        if (i < n - 1)
                        {
                            Console.Write(",");
                        }
                    }
                }
                Console.WriteLine();

                indent(shift);
                Console.WriteLine("{");

                if (struc.Members != null && struc.Members.Length != 0)
                {
                    for (int i = 0, n = struc.Members.Length; i < n; i++)
                    {
                        Member member = struc.Members[i];

                        if (member == null)
                        {
                            indent(shift + ind);
                            Console.WriteLine("<UNRESOLVED MEMBER>");
                            continue;
                        }
                        report(member, shift + ind);
                    }
                }
                indent(shift);
                Console.WriteLine("}");
                break;
            }

            case NodeType.EnumNode:
            {
                EnumNode enume = node as EnumNode;

                indent(shift);
                if (enume.Name != null && enume.Name.Name != null)
                {
                    Console.Write("enum {0} = ", enume.Name.Name);
                }
                else
                {
                    Console.Write("enum <NONAME> = ");
                }
                Console.Write("{");

                for (int i = 0, n = enume.Members.Length; i < n; i++)
                {
                    Field enumerator = (Field)enume.Members[i];
                    Console.Write("{0}", enumerator.Name.Name);
                    if (enumerator.DefaultValue != null)
                    {
                        Console.Write(" = {0}", enumerator.DefaultValue.ToString());
                    }
                    if (i < n - 1)
                    {
                        Console.Write(", ");
                    }
                }
                Console.WriteLine("};");

                break;
            }

            case NodeType.Interface:
            {
                Interface interfac = node as Interface;

                indent(shift);

                if (interfac.IsAbstract)
                {
                    Console.Write("abstract ");
                }
                if (interfac.IsPrivate)
                {
                    Console.Write("private ");
                }
                else if (interfac.IsPublic)
                {
                    Console.Write("public ");
                }
                else
                {
                    Console.Write("internal ");                                // ???????????
                }
                Console.WriteLine("interface {0}", interfac.Name.Name);
                indent(shift);
                Console.WriteLine("{");

                if (interfac.Members != null && interfac.Members.Length != 0)
                {
                    for (int i = 0, n = interfac.Members.Length; i < n; i++)
                    {
                        Member member = interfac.Members[i];

                        if (member == null)
                        {
                            indent(shift + ind);
                            Console.WriteLine("<UNRESOLVED MEMBER>");
                            continue;
                        }
                        report(member, shift + ind);
                    }
                }
                indent(shift);
                Console.WriteLine("}");

                break;
            }

            case NodeType.Method:
            case NodeType.InstanceInitializer:
            {
                Method method = node as Method;

                indent(shift);

                if (method.IsAbstract)
                {
                    Console.Write("abstract ");
                }
                if (method.IsPublic)
                {
                    Console.Write("public ");
                }
                if (method.IsStatic)
                {
                    Console.Write("static ");
                }
                if (method.IsVirtual)
                {
                    Console.Write("virtual ");
                }
                if (method.IsPrivate)
                {
                    Console.Write("private ");
                }
                if (method.OverriddenMethod != null)
                {
                    Console.Write("override ");
                }

                if (method.ReturnType != null && method.ReturnType.Name != null)
                {
                    Console.Write("{0} ", method.ReturnType.Name.Name);
                }
                if (method.Name != null)
                {
                    if (method.ImplementedInterfaceMethods != null && method.ImplementedInterfaceMethods.Length != 0)
                    {
                        Method interf = method.ImplementedInterfaceMethods[0];
                        if (interf != null)
                        {
                            string name = interf.DeclaringType.Name.Name;
                            Console.Write("{0}.", name);
                        }
                    }
                    Console.Write("{0}", method.Name.Name);
                }
                Console.Write(" (");

                if (method.Parameters != null && method.Parameters.Length != 0)
                {
                    for (int i = 0, n = method.Parameters.Length; i < n; i++)
                    {
                        Parameter par = method.Parameters[i];
                        if (par == null)
                        {
                            continue;
                        }
                        if ((par.Flags & ParameterFlags.In) != 0)
                        {
                            Console.Write("in ");
                        }
                        if ((par.Flags & ParameterFlags.Out) != 0)
                        {
                            Console.Write("out ");
                        }

                        if (par.Type != null && par.Type.Name != null)
                        {
                            Console.Write("{0}", par.Type.Name.Name);
                        }
                        else
                        {
                            report(par.Type, 0);
                        }
                        Console.Write(" {0}", par.Name.Name);
                        if (i < n - 1)
                        {
                            Console.Write(", ");
                        }
                    }
                }
                Console.Write(" )");

                // method body
                if (method.Body != null)
                {
                    Console.WriteLine();
                    report(method.Body, shift);
                }
                else
                {
                    Console.WriteLine(";");
                }
                break;
            }

            case NodeType.DelegateNode:
            {
                DelegateNode dn = node as DelegateNode;

                indent(shift);
                Console.Write("delegate ");

                if (dn.ReturnType != null && dn.ReturnType.Name != null)
                {
                    Console.Write("{0} ", dn.ReturnType.Name.Name);
                }
                if (dn.Name != null)
                {
                    Console.Write("{0}", dn.Name.Name);
                }
                Console.Write(" (");

                if (dn.Parameters != null && dn.Parameters.Length != 0)
                {
                    for (int i = 0, n = dn.Parameters.Length; i < n; i++)
                    {
                        Parameter par = dn.Parameters[i];
                        if (par == null)
                        {
                            continue;
                        }
                        if ((par.Flags & ParameterFlags.In) != 0)
                        {
                            Console.Write("in ");
                        }
                        if ((par.Flags & ParameterFlags.Out) != 0)
                        {
                            Console.Write("out ");
                        }

                        if (par.Type != null && par.Type.Name != null)
                        {
                            Console.Write("{0}", par.Type.Name.Name);
                        }
                        else
                        {
                            report(par.Type, 0);
                        }
                        Console.Write(" {0}", par.Name.Name);
                        if (i < n - 1)
                        {
                            Console.Write(", ");
                        }
                    }
                }
                Console.WriteLine(" );");
                break;
            }

            case NodeType.StaticInitializer:
            {
                StaticInitializer si = node as StaticInitializer;

                indent(shift);

                Console.WriteLine("static {0} ( )", si.Name.Name);

                // body
                if (si.Body != null)
                {
                    report(si.Body, shift);
                }
                else
                {
                    Console.WriteLine("NO BODY");
                }
                break;
            }

            case NodeType.FieldInitializerBlock:
            {
                FieldInitializerBlock initializers = node as FieldInitializerBlock;

                indent(shift);
                if (initializers.IsStatic)
                {
                    Console.Write("static ");
                }
                Console.WriteLine("init {");
                for (int i = 0, n = initializers.Statements.Length; i < n; i++)
                {
                    report(initializers.Statements[i], shift + ind);
                }
                indent(shift);
                Console.WriteLine("}");
                break;
            }

            case NodeType.Base:
            {
                Console.Write("base");
                break;
            }

            case NodeType.Field:
            {
                Field field = node as Field;

                indent(shift);

                if (field.IsPrivate)
                {
                    Console.Write("private ");
                }
                else if (field.IsPublic)
                {
                    Console.Write("public ");
                }

                if (field.IsStatic)
                {
                    Console.Write("static ");
                }
                if (field.IsInitOnly)
                {
                    Console.Write("readonly ");
                }

                if (field.Type != null)
                {
                    if (field.Type.Name != null)
                    {
                        Console.Write("{0}", field.Type.Name.Name);
                    }
                    else
                    {
                        report(field.Type, 0);
                    }
                }
                Console.Write(" {0}", field.Name.Name);

                if (field.Initializer != null)
                {
                    Console.Write(" = ");
                    report(field.Initializer, 0);
                }
                Console.WriteLine(";");

                break;
            }

            case NodeType.VariableDeclaration:
            {
                VariableDeclaration variable = node as VariableDeclaration;

                indent(shift);
                if (variable.Type != null && variable.Type.Name != null)
                {
                    Console.Write("{0}", variable.Type.Name.Name);
                }
                else
                {
                    report(variable.Type, 0);
                }

                Console.Write(" {0}", variable.Name.Name);

                if (variable.Initializer != null)
                {
                    Console.Write(" = ");
                    report(variable.Initializer, 0);
                }
                Console.WriteLine(";");

                break;
            }

            case NodeType.LocalDeclarationsStatement:
            {
                LocalDeclarationsStatement stmt = node as LocalDeclarationsStatement;

                indent(shift);

                TypeNode type = stmt.Type;
                if (type != null && type.Name != null)
                {
                    Console.Write("{0}", type.Name.Name);
                }
                else
                {
                    report(type, 0);
                }
                Console.Write(" ");

                LocalDeclarationList list = stmt.Declarations;
                for (int i = 0, n = list.Length; i < n; i++)
                {
                    LocalDeclaration local = list[i];
                    Console.Write("{0}", local.Name.Name);
                    if (local.InitialValue != null)
                    {
                        Console.Write(" = ");
                        report(local.InitialValue, 0);
                    }
                    if (i < n - 1)
                    {
                        Console.Write(", ");
                    }
                }
                Console.WriteLine(";");
                break;
            }

            case NodeType.Property:
            {
                Property property = node as Property;

                indent(shift);

                if (property.IsPrivate)
                {
                    Console.Write("private ");
                }
                else if (property.IsPublic)
                {
                    Console.Write("public ");
                }

                if (property.IsStatic)
                {
                    Console.Write("static ");
                }

                if (property != null)
                {
                    if (property.Type != null && property.Type.Name != null)
                    {
                        Console.Write("{0} ", property.Type.Name.Name);
                    }

                    if (property.ImplementedTypes != null)
                    {
                        TypeNode typ = property.ImplementedTypes[0];
                        Console.Write("{0}.", typ.Name.Name);
                    }
                    if (property.Name != null)
                    {
                        Console.WriteLine("{0}", property.Name.Name);
                    }
                }
                indent(shift);
                Console.WriteLine("{");

                if (property.Getter != null)
                {
                    report(property.Getter, shift + ind);
                }
                if (property.Setter != null)
                {
                    report(property.Setter, shift + ind);
                }

                indent(shift);
                Console.WriteLine("}");

                break;
            }

            case NodeType.Lock:
            {
                Lock _lock = node as Lock;

                indent(shift);
                Console.Write("lock(");
                report(_lock.Guard, shift);
                Console.WriteLine(")");
                report(_lock.Body, shift + ind);
                indent(shift);
                Console.WriteLine("}");

                break;
            }

            case NodeType.Block:
            {
                Block block = node as Block;
                if (block == null || block.Statements == null)
                {
                    break;
                }
                indent(shift);
                Console.WriteLine("{");

                for (int i = 0, n = block.Statements.Length; i < n; i++)
                {
                    report(block.Statements[i], shift + ind);
                    Console.WriteLine();
                }
                indent(shift);
                Console.WriteLine("}");

                break;
            }

            case NodeType.MemberBinding:
            {
                MemberBinding mb = node as MemberBinding;
                if (mb.TargetObject != null)
                {
                    report(mb.TargetObject, 0);
                }
                else if (mb.BoundMember != null && mb.BoundMember.DeclaringType != null)
                {
                    Console.Write(mb.BoundMember.DeclaringType.Name);
                }
                Console.Write(".");
                if (mb.BoundMember.Name != null)
                {
                    Console.Write(mb.BoundMember.Name.Name);
                }
                else
                {
                    report(mb.BoundMember, 0);
                }
                break;
            }

            case NodeType.AssignmentStatement:
            {
                AssignmentStatement assignment = node as AssignmentStatement;

                indent(shift);

                report(assignment.Target, 0);
                switch (assignment.Operator)
                {
                case NodeType.Nop: Console.Write(" = "); break;

                case NodeType.Add: Console.Write(" += "); break;

                case NodeType.Add_Ovf: Console.Write(" += "); break;

                case NodeType.Add_Ovf_Un: Console.Write(" += "); break;

                case NodeType.Sub: Console.Write(" -= "); break;

                case NodeType.Sub_Ovf: Console.Write(" -= "); break;

                case NodeType.Sub_Ovf_Un: Console.Write(" -= "); break;

                case NodeType.Mul: Console.Write(" *= "); break;

                case NodeType.Mul_Ovf: Console.Write(" *= "); break;

                case NodeType.Mul_Ovf_Un: Console.Write(" *= "); break;
                }
                report(assignment.Source, 0);
                Console.Write(";");
                break;
            }

            case NodeType.ExpressionStatement:
            {
                ExpressionStatement exprStatement = node as ExpressionStatement;

                indent(shift);

                report(exprStatement.Expression, 0);
                Console.Write(";");
                break;
            }

            case NodeType.Return:
            {
                Return return_stmt = node as Return;

                indent(shift);
                Console.Write("return");
                if (return_stmt.Expression != null)
                {
                    Console.Write(" ");
                    report(return_stmt.Expression, 0);
                }
                Console.Write(";");

                break;
            }

            case NodeType.Branch:
            {
                Branch branch = node as Branch;

                indent(shift);
                Console.WriteLine("break; (???)");

                break;
            }

            case NodeType.For:
            {
                For for_stmt = node as For;

                indent(shift);
                Console.Write("for ( ");
                for (int i = 0, n = for_stmt.Initializer.Length; i < n; i++)
                {
                    report(for_stmt.Initializer[i], 0);
                }
                report(for_stmt.Condition, 0);
                Console.Write("; ");
                for (int i = 0, n = for_stmt.Incrementer.Length; i < n; i++)
                {
                    report(for_stmt.Incrementer[i], 0);
                }
                Console.WriteLine(")");

                indent(shift);
                Console.WriteLine("{");
                report(for_stmt.Body, shift + ind);
                indent(shift);
                Console.WriteLine("}");

                break;
            }

            case NodeType.While:
            {
                While while_loop = node as While;

                indent(shift);
                Console.Write("while ( ");
                report(while_loop.Condition, 0);
                Console.WriteLine(" )");

                report(while_loop.Body, shift);

                break;
            }

            case NodeType.DoWhile:
            {
                DoWhile repeat = node as DoWhile;

                indent(shift);
                Console.WriteLine("do");
                report(repeat.Body, shift);

                indent(shift);
                Console.Write("while (");
                report(repeat.Condition, 0);
                Console.WriteLine(" );");

                break;
            }

            case NodeType.If:
            {
                If if_stmt = node as If;

                indent(shift);
                Console.Write("if ( ");
                report(if_stmt.Condition, 0);
                Console.WriteLine(" )");

                report(if_stmt.TrueBlock, shift);

                if (if_stmt.FalseBlock == null ||
                    if_stmt.FalseBlock.Statements == null ||
                    if_stmt.FalseBlock.Statements.Length == 0)
                {
                    break;
                }

                indent(shift);
                Console.WriteLine("else");
                report(if_stmt.FalseBlock, shift);

                break;
            }

            case NodeType.Switch:
            {
                Switch swtch = node as Switch;

                indent(shift);
                Console.Write("switch ( ");
                report(swtch.Expression, 0);
                Console.WriteLine(" )");

                indent(shift);
                Console.WriteLine("{");

                for (int i = 0, n = swtch.Cases.Length; i < n; i++)
                {
                    indent(shift + ind);
                    if (swtch.Cases[i].Label != null)
                    {
                        Console.Write("case ");
                        report(swtch.Cases[i].Label, 0);
                        Console.WriteLine(":");
                    }
                    else
                    {
                        Console.WriteLine("default:");
                    }
                    report(swtch.Cases[i].Body, shift + ind);
                }
                indent(shift);
                Console.WriteLine("}");

                break;
            }

            case NodeType.Throw:
            {
                Throw thro = node as Throw;

                indent(shift);
                Console.Write("throw (");
                report(thro.Expression, 0);
                Console.Write(");");
                break;
            }

            case NodeType.Exit:
            {
                indent(shift);
                Console.WriteLine("exit;");
                break;
            }

            case NodeType.Continue:
            {
                indent(shift);
                Console.WriteLine("continue;");
                break;
            }

            case NodeType.Try:
            {
                Try trys = node as Try;

                indent(shift);
                Console.WriteLine("try {");
                report(trys.TryBlock, shift + ind);
                indent(shift);
                Console.WriteLine("}");
                if (trys.Catchers != null)
                {
                    for (int i = 0, n = trys.Catchers.Length; i < n; i++)
                    {
                        indent(shift);
                        if (trys.Catchers[i].Type != null)
                        {
                            Console.Write("catch ( {0} ", trys.Catchers[i].Type.FullName);
                        }
                        else
                        {
                            Console.Write("catch ( ");
                        }
                        if (trys.Catchers[i].Variable != null)
                        {
                            report(trys.Catchers[i].Variable, 0);
                        }
                        Console.WriteLine(" ) {");
                        report(trys.Catchers[i].Block, shift + ind);
                        indent(shift);
                        Console.WriteLine("}");
                    }
                }
                if (trys.Finally != null && trys.Finally.Block != null)
                {
                    indent(shift);
                    Console.WriteLine("finally");
                    report(trys.Finally.Block, shift);
                }
                break;
            }

            case NodeType.BlockExpression:
            {
                BlockExpression be = node as BlockExpression;

                Console.WriteLine("(");
                StatementList sl = be.Block.Statements;
                for (int i = 0, n = sl.Length; i < n; i++)
                {
                    report(sl[i], shift + ind);
                }
                indent(shift);
                Console.Write(")");
                break;
            }

            case NodeType.ArrayTypeExpression:
            {
                ArrayTypeExpression array = node as ArrayTypeExpression;

                indent(shift);

                if (array.ElementType != null &&
                    array.ElementType.Name != null &&
                    array.ElementType.Name.Name != null)
                {
                    Console.Write(array.ElementType.Name.Name);
                }
                else
                {
                    report(array.ElementType, 0);
                }

                Console.Write("[");
                for (int i = 0, n = array.Rank; i < n; i++)
                {
                    if (array.Sizes != null)
                    {
                        Console.Write(array.Sizes[i]);
                    }
                    if (i < n - 1)
                    {
                        Console.Write(",");
                    }
                }
                Console.Write("]");

                break;
            }

            case NodeType.Construct:
            {
                Construct construct = node as Construct;

                indent(shift);
                Console.Write("new ");
                report(construct.Constructor, 0);
                Console.Write("(");
                if (construct.Operands != null)
                {
                    for (int i = 0, n = construct.Operands.Length; i < n; i++)
                    {
                        report(construct.Operands[i], 0);
                        if (i < n - 1)
                        {
                            Console.Write(",");
                        }
                    }
                }
                Console.Write(")");
                break;
            }

            case NodeType.ConstructArray:
            {
                ConstructArray initializer = node as ConstructArray;

                Console.Write("new ");

                if (initializer.ElementType != null &&
                    initializer.ElementType.Name != null &&
                    initializer.ElementType.Name.Name != null)
                {
                    Console.Write(initializer.ElementType.Name.Name);
                }
                else
                {
                    report(initializer.ElementType, 0);
                }

                Console.Write("[");
                for (int i = 0, n = initializer.Operands.Length; i < n; i++)
                {
                    report(initializer.Operands[i], 0);
                    if (i < n - 1)
                    {
                        Console.Write(",");
                    }
                }
                Console.Write("]");

                break;
            }

            case NodeType.ConstructDelegate:
            {
                ConstructDelegate cd = node as ConstructDelegate;

                // Console.Write("new {0}({1})",cd.DelegateType.Name.Name,cd.MethodName.Name);
                Console.Write("new {0}(", cd.DelegateType.Name.Name);
                report(cd.TargetObject, 0);
                Console.Write(".{0})", cd.MethodName.Name);
                // cd.Type;
                break;
            }

            default:
            {
                if (node is ZonnonCompilation)
                {
                    ZonnonCompilation zc = node as ZonnonCompilation;
                    report(zc.CompilationUnits[0], shift);
                }
                // Expression?

                else if (node is MethodCall)
                {
                    MethodCall call = node as MethodCall;

                    report(call.Callee, 0);
                    Console.Write("(");

                    if (call.Operands != null && call.Operands.Length != 0)
                    {
                        for (int i = 0, n = call.Operands.Length; i < n; i++)
                        {
                            report(call.Operands[i], 0);
                            if (i < n - 1)
                            {
                                Console.Write(",");
                            }
                        }
                    }

                    Console.Write(")");
                }
                else if (node is Variable)
                {
                    Variable variable = node as Variable;
                    Console.Write("{0}", variable.Name.Name);
                }
                else if (node is Identifier)
                {
                    Identifier identifier = node as Identifier;
                    Console.Write("{0}", identifier.Name);
                }
                else if (node is QualifiedIdentifier)
                {
                    QualifiedIdentifier qualid = node as QualifiedIdentifier;
                    report(qualid.Qualifier, 0);
                    Console.Write(".{0}", qualid.Identifier == null?"<UNRESOLVED>":qualid.Identifier.Name);
                }
                else if (node is Literal)
                {
                    Literal literal = node as Literal;
                    if (literal.Value == null)
                    {
                        Console.Write("null");
                    }
                    else
                    {
                        if (literal.Value is string)
                        {
                            Console.Write("\"");
                        }
                        else if (literal.Value is char)
                        {
                            Console.Write("'");
                        }
                        Console.Write("{0}", literal.Value.ToString());
                        if (literal.Value is string)
                        {
                            Console.Write("\"");
                        }
                        else if (literal.Value is char)
                        {
                            Console.Write("'");
                        }
                    }
                }
                else if (node is Indexer)
                {
                    Indexer indexer = node as Indexer;
                    report(indexer.Object, 0);
                    Console.Write("[");
                    for (int i = 0, n = indexer.Operands.Length; i < n; i++)
                    {
                        report(indexer.Operands[i], 0);
                        if (i < n - 1)
                        {
                            Console.Write(",");
                        }
                    }
                    Console.Write("]");
                }
                else if (node is UnaryExpression)
                {
                    UnaryExpression unexpr = node as UnaryExpression;

                    bool add_pars = unexpr.Operand is BinaryExpression ||
                                    unexpr.Operand is UnaryExpression;

                    switch (unexpr.NodeType)
                    {
                    case NodeType.Add: Console.Write("+");  break;

                    case NodeType.Sub: Console.Write("-");  break;

                    case NodeType.Neg: Console.Write("-");  break;

                    case NodeType.Not: Console.Write("~");  break;

                    case NodeType.UnaryPlus: Console.Write("+"); break;

                    case NodeType.LogicalNot: Console.Write("!"); break;

                    case NodeType.Conv_U2: Console.Write("(UInt16)"); break;

                    case NodeType.RefAddress: Console.Write("ref "); break;

                    case NodeType.Ckfinite: Console.Write("(Ckfinite)"); break;

                    default:           Console.Write("???");  break;
                    }

                    if (add_pars)
                    {
                        Console.Write("(");
                    }
                    report(unexpr.Operand, 0);
                    if (add_pars)
                    {
                        Console.Write(")");
                    }
                }
                else if (node is BinaryExpression)
                {
                    BinaryExpression binexpr = node as BinaryExpression;

                    bool add_pars = binexpr.Operand1 is BinaryExpression ||
                                    binexpr.Operand1 is UnaryExpression;

                    if (binexpr.NodeType == NodeType.Castclass)
                    {
                        Console.Write("(");
                        report(binexpr.Operand2, 0);
                        Console.Write(")");

                        if (add_pars)
                        {
                            Console.Write("(");
                        }
                        report(binexpr.Operand1, 0);
                        if (add_pars)
                        {
                            Console.Write(")");
                        }
                        break;
                    }

                    if (add_pars)
                    {
                        Console.Write("(");
                    }
                    report(binexpr.Operand1, 0);
                    if (add_pars)
                    {
                        Console.Write(")");
                    }

                    switch (binexpr.NodeType)
                    {
                    case NodeType.Add: Console.Write(" + "); break;

                    case NodeType.Add_Ovf: Console.Write(" + "); break;

                    case NodeType.Add_Ovf_Un: Console.Write(" + "); break;

                    case NodeType.Sub: Console.Write(" - "); break;

                    case NodeType.Sub_Ovf: Console.Write(" - "); break;

                    case NodeType.Sub_Ovf_Un: Console.Write(" - "); break;

                    case NodeType.Mul: Console.Write(" * "); break;

                    case NodeType.Mul_Ovf: Console.Write(" * "); break;

                    case NodeType.Mul_Ovf_Un: Console.Write(" * "); break;

                    case NodeType.Div: Console.Write(" / "); break;

                    // case NodeType.Div : Console.Write(" DIV "); break;  // "DIV" ?????
                    case NodeType.Rem: Console.Write(" % "); break;          // "MOD" ?????

                    case NodeType.Or: Console.Write(" | "); break;

                    case NodeType.And: Console.Write(" & "); break;

                    case NodeType.Eq: Console.Write(" == "); break;

                    case NodeType.Ne: Console.Write(" != "); break;

                    case NodeType.Lt: Console.Write(" < "); break;

                    case NodeType.Le: Console.Write(" <= "); break;

                    case NodeType.Gt: Console.Write(" > "); break;

                    case NodeType.Ge: Console.Write(" >= "); break;

                    case NodeType.LogicalOr: Console.Write(" || "); break;

                    case NodeType.LogicalAnd: Console.Write(" && "); break;

                    case NodeType.Is: Console.Write(" is "); break;

                    case NodeType.Comma: Console.Write(","); break;

                    // case OPERATORS.In           : expression.NodeType = NodeType  // "IN" break;
                    // case OPERATORS.Implements   : expression.NodeType = NodeType  // "IMPLEMENTS" break;
                    default: Console.Write(" !! "); break;
                    }

                    add_pars = binexpr.Operand2 is BinaryExpression ||
                               binexpr.Operand2 is UnaryExpression;

                    if (add_pars)
                    {
                        Console.Write("(");
                    }
                    report(binexpr.Operand2, 0);
                    if (add_pars)
                    {
                        Console.Write(")");
                    }
                }
                else if (node is TernaryExpression)
                {
                    TernaryExpression ter = node as TernaryExpression;
                    if (ter.NodeType == NodeType.Conditional)
                    {
                        report(ter.Operand1, 0);
                        Console.Write(" ? ");
                        report(ter.Operand2, 0);
                        Console.Write(" : ");
                        report(ter.Operand3, 0);
                    }
                }
                else if (node is PostfixExpression)
                {
                    PostfixExpression postfixExpr = node as PostfixExpression;

                    report(postfixExpr.Expression, 0);

                    switch (postfixExpr.Operator)
                    {
                    case NodeType.Increment: Console.Write("++"); break;

                    case NodeType.Decrement: Console.Write("--"); break;

                    default: Console.Write("???"); break;
                    }
                }
                else if (node is LabeledStatement)
                {
                    LabeledStatement lbst = node as LabeledStatement;
                    indent(shift);
                    report(lbst.Label, 0);
                    Console.Write(" : ");
                    report(lbst.Statement, 0);
                }
                else if (node is Goto)
                {
                    Goto gt = node as Goto;
                    indent(shift);
                    Console.Write("goto ");
                    report(gt.TargetLabel, 0);
                }
                else
                {
                    indent(shift);
                    Console.WriteLine("No code for reporting {0}:{1}", node.UniqueKey, node.ToString());
                }
                break;
            }
            }
        }
 public virtual Differences VisitStaticInitializer(StaticInitializer cons1, StaticInitializer cons2){
   return this.VisitMethod(cons1, cons2);
 }
Esempio n. 6
0
 /// <summary>
 /// Write out a static constructor name
 /// </summary>
 /// <param name="constructor">The static constructor for which to write out the name</param>
 /// <param name="sb">The string builder to which the name is written</param>
 private static void WriteStaticConstructor(StaticInitializer constructor, StringBuilder sb)
 {
     WriteType(constructor.DeclaringType, sb);
     sb.Append(".#cctor");
     WriteParameters(constructor.Parameters, sb);
 }
Esempio n. 7
0
 public virtual StaticInitializer VisitStaticInitializer(StaticInitializer cons){
   return (StaticInitializer)this.VisitMethod(cons);
 }
Esempio n. 8
0
        public static void Start(ProgramOptions programOptions)
        {
            Console.WriteLine(programOptions);
            Settings.SetProgramOptions(programOptions);
            var outputPath = SetupOutputFile();

            streamWriter.WriteLine(Resource.GetResourceAsString("TinyBCT.Resources.Prelude.bpl"));

            if (Settings.AsyncSupport || Settings.AsyncSupportGenerics)
            {
                streamWriter.WriteLine(Resource.GetResourceAsString("TinyBCT.Resources.CommonAsyncPrelude.bpl"));
            }

            if (Settings.AsyncSupport)
            {
                streamWriter.WriteLine(Resource.GetResourceAsString("TinyBCT.Resources.AsyncPrelude.bpl"));
            }

            if (Settings.AsyncSupportGenerics)
            {
                streamWriter.WriteLine(Resource.GetResourceAsString("TinyBCT.Resources.AsyncPreludeGeneric.bpl"));
            }

            using (var host = new PeReader.DefaultHost())
            {
                #region Load assemblies
                ISet <Assembly> inputAssemblies = new HashSet <Assembly>();
                foreach (string inputFile in Settings.InputFiles)
                {
                    Assembly assembly = new Assembly(host);
                    assembly.Load(inputFile);
                    inputAssemblies.Add(assembly);
                }
                #endregion

                #region Execute CHA
                var CHAnalysis = new ClassHierarchyAnalysis(host);
                CHAnalysis.Analyze();
                #endregion

                #region Initialize host types
                Types.Initialize(host);
                #endregion

                // TODO(diegog): Analysis not integrated yet
                // This can be used to obtain the allocated types and delegates
                //var allocationsAndDelelegatesAnalysis = new TypesAndDelegatesCollector(host);

                #region Write three address code for debugging
                TACWriter.WriteTAC(inputAssemblies);
                #endregion

                #region Look for references (used in mixed memory model)
                if (Settings.MemoryModel == ProgramOptions.MemoryModelOption.Mixed)
                {
                    ReferenceFinder.TraverseForFields(inputAssemblies);
                }
                #endregion

                #region Translate defined types and add axioms about subtyping
                TypeDefinitionTranslator.TranslateTypes(inputAssemblies);
                #endregion

                #region Translate defined methods
                MethodTranslator.TranslateAssemblies(inputAssemblies, CHAnalysis);
                #endregion

                #region Create main wrapper with static fields initialization and static constructors calls
                StaticInitializer.SearchStaticConstructorsAndMain(inputAssemblies);
                streamWriter.WriteLine(StaticInitializer.CreateInitializeGlobals());
                streamWriter.WriteLine(StaticInitializer.CreateMainWrappers());
                streamWriter.WriteLine(StaticInitializer.CreateStaticVariablesAllocProcedure());
                streamWriter.WriteLine(StaticInitializer.CreateDefaultValuesStaticVariablesProcedure());
                streamWriter.WriteLine(StaticInitializer.CreateStaticConstructorsCallsProcedure());
                #endregion

                #region Translate types that are referenced but not defined in the input assemblies
                TypeDefinitionTranslator.DefineUndeclaredSuperClasses();
                TypeDefinitionTranslator.ParametricTypeDeclarations();
                #endregion

                #region Translate string constants
                BoogieLiteral.Strings.WriteStringConsts(streamWriter);
                #endregion

                #region Translate delegates
                streamWriter.WriteLine(DelegateStore.DefineMethodsIdentifiers());
                streamWriter.WriteLine(DelegateStore.CreateDelegateMethod());
                streamWriter.WriteLine(DelegateStore.InvokeDelegateMethod());
                #endregion

                // CreateAllAsyncMethods(streamWriter);
                #region Heuristic to catch getters & setters. If they are in our input assemblies we generate a body using a field associated to that property
                IEnumerable <IMethodReference> usedProperties = new List <IMethodReference>();
                if (Settings.StubGettersSetters || Settings.StubGettersSettersWhitelist.Count > 0)
                {
                    GetterSetterStub getterSetterStub = new GetterSetterStub();
                    usedProperties = getterSetterStub.Stub(inputAssemblies, streamWriter);
                }
                #endregion

                #region Generate stubs for async methods
                if (Settings.AsyncSupport)
                {
                    AsyncStubs asyncStubs = new AsyncStubs(inputAssemblies);
                    streamWriter.WriteLine(asyncStubs.AsyncMethodBuilderStartStub(false));
                    streamWriter.WriteLine(asyncStubs.AsyncStubsScheduleTask(false));
                }

                if (Settings.AsyncSupportGenerics)
                {
                    AsyncStubs asyncStubs = new AsyncStubs(inputAssemblies);
                    streamWriter.WriteLine(asyncStubs.AsyncMethodBuilderStartStub(true));
                    streamWriter.WriteLine(asyncStubs.AsyncStubsScheduleTask(true));
                }

                if (!Settings.AsyncSupport && Settings.AsyncSupportGenerics)
                {
                    // this is only in AsyncSupport prelude
                    // we can't add it to AsyncSupportGenerics because we are not always sure there will be all defined types and functions

                    bool hasEventually = inputAssemblies.GetAllDefinedMethods().Any(m => m.Name.Value.Equals("Eventually"));
                    if (hasEventually)
                    {
                        AsyncStubs asyncStubs = new AsyncStubs(inputAssemblies);
                        streamWriter.WriteLine(asyncStubs.EventuallyStub());
                    }
                }

                #endregion

                streamWriter.WriteLine(StringTranslator.Stubs());

                #region Translate called methods as extern (bodyless methods or methods not present in our input assemblies)

                var externMethods = InstructionTranslator.CalledMethods.Except(inputAssemblies.GetAllDefinedMethods().Where(m => m.Body.Size > 0)).Except(usedProperties);
                externMethods = externMethods.Where(m => !StringTranslator.GetBoogieNamesForStubs().Contains(BoogieMethod.From(m).Name));
                foreach (var methodRef in externMethods)
                {
                    var head = Helpers.GetExternalMethodDefinition(Helpers.GetUnspecializedVersion(methodRef));
                    streamWriter.WriteLine(head);
                }

                #endregion

                #region Translate class fields
                // we declare read or written fields
                foreach (var field in FieldTranslator.GetFieldDefinitions())
                {
                    streamWriter.WriteLine(field);
                }
                #endregion

                streamWriter.Close();

                #region Append bpl input files
                foreach (var bplInputFile in Settings.BplInputFiles)
                {
                    var output = new FileStream(outputPath, FileMode.Append, FileAccess.Write);

                    using (var inputStream = File.OpenRead(bplInputFile))
                    {
                        inputStream.CopyTo(output);
                    }

                    output.Close();
                }
                #endregion
            }
        }
Esempio n. 9
0
 public virtual void VisitStaticInitializer(StaticInitializer cons)
 {
   this.VisitMethod(cons);
 }
 public override StaticInitializer VisitStaticInitializer(StaticInitializer cons)
 {
   throw new ApplicationException("unimplemented");
 }
Esempio n. 11
0
 private static void WriteStaticConstructor(StaticInitializer constructor, TextWriter writer)
 {
     WriteType(constructor.DeclaringType, writer);
     writer.Write(".#cctor");
     WriteParameters(constructor.Parameters, writer);
 }
Esempio n. 12
0
 public override StaticInitializer VisitStaticInitializer(StaticInitializer cons){
   if (cons == null) return null;
   if (!cons.HasCompilerGeneratedSignature && cons.DeclaringType != null)
     cons.DeclaringType.Flags &= ~TypeFlags.BeforeFieldInit;
   return base.VisitStaticInitializer(cons);
 }
Esempio n. 13
0
 public override StaticInitializer VisitStaticInitializer(StaticInitializer cons)
 {
     throw new ApplicationException("unimplemented");
 }
Esempio n. 14
0
        public static void Start(ProgramOptions programOptions)
        {
            Console.WriteLine(programOptions);
            Settings.SetProgramOptions(programOptions);
            var outputPath = SetupOutputFile();

            Prelude.Write(); // writes prelude.bpl content into the output file

            using (var host = TinyBCTHost.NewHost())
            {
                var CHAnalysis = CreateCHAnalysis(host);
                Types.Initialize(host);

                // TODO(diegog): Analysis not integrated yet
                // This can be used to obtain the allocated types and delegates
                var allocationsAndDelelegatesAnalysis = new TypesAndDelegatesCollector(host);
                //allocationsAndDelelegatesAnalysis.Analyze();

                Action <string> writeTAC = (String inputFile) =>
                {
                    using (var assembly = new Assembly(host))
                    {
                        // analysis-net setup
                        assembly.Load(inputFile);

                        TACWriter.Open(inputFile);
                        var visitor = new Traverser(host, assembly.PdbReader, CHAnalysis);
                        visitor.AddMethodDefinitionAction(TACWriter.IMethodDefinitionTraverse); // saves tac code for debugging
                        visitor.Traverse(assembly.Module);
                        TACWriter.Close();
                    }
                };


                ProcessFiles(writeTAC);

                Action <string> translateTypeDefinitions = (String inputFile) =>
                {
                    using (var assembly = new Assembly(host))
                    {
                        // analysis-net setup
                        assembly.Load(inputFile);
                        Types.Initialize(host);

                        var visitor = new Traverser(host, assembly.PdbReader, CHAnalysis);
                        visitor.AddNamedTypeDefinitionAction(TypeDefinitionTranslator.TypeDefinitionTranslatorTraverse); // generates axioms for typing
                        visitor.Traverse(assembly.Module);
                    }
                };

                ProcessFiles(translateTypeDefinitions);


                Action <string> translateMethodDefinitions = (String inputFile) =>
                {
                    using (var assembly = new Assembly(host))
                    {
                        // analysis-net setup
                        assembly.Load(inputFile);
                        Types.Initialize(host);

                        var visitor = new Traverser(host, assembly.PdbReader, CHAnalysis);
                        visitor.AddMethodDefinitionAction(MethodTranslator.IMethodDefinitionTraverse); // given a IMethodDefinition and a MethodBody are passed to a MethodTranslator object
                        visitor.Traverse(assembly.Module);
                    }
                };

                ProcessFiles(translateMethodDefinitions);

                Action <string> translateCallsToStaticConstructors = (String inputFile) =>
                {
                    using (var assembly = new Assembly(host))
                    {
                        // analysis-net setup
                        assembly.Load(inputFile);
                        Types.Initialize(host);

                        var visitor = new Traverser(host, assembly.PdbReader, CHAnalysis);
                        visitor.AddMethodDefinitionAction(StaticInitializer.IMethodDefinitionTraverse); // given a IMethodDefinition and a MethodBody are passed to a MethodTranslator object
                        visitor.Traverse(assembly.Module);
                    }
                };

                ProcessFiles(translateCallsToStaticConstructors);
                streamWriter.WriteLine(StaticInitializer.CreateInitializeGlobals());
                streamWriter.WriteLine(StaticInitializer.CreateMainWrappers());

                // TypeDefinitionTranslator.TypeAxioms(); diego's axioms
                // information stored from previous steps is used
                TypeDefinitionTranslator.DefineUndeclaredSuperClasses();

                BoogieLiteral.Strings.WriteStringConsts(streamWriter);

                streamWriter.WriteLine(DelegateStore.DefineMethodsIdentifiers());
                streamWriter.WriteLine(DelegateStore.CreateDelegateMethod());
                streamWriter.WriteLine(DelegateStore.InvokeDelegateMethod());

                // CreateAllAsyncMethods(streamWriter);



                // extern method called
                foreach (var methodRef in InstructionTranslator.ExternMethodsCalled)
                {
                    var head = Helpers.GetExternalMethodDefinition(Helpers.GetUnspecializedVersion(methodRef));
                    streamWriter.WriteLine(head);
                }
                foreach (var methodRef in InstructionTranslator.PotentiallyMissingMethodsCalled)
                {
                    if (Helpers.IsCurrentlyMissing(methodRef))
                    {
                        var head = Helpers.GetExternalMethodDefinition(Helpers.GetUnspecializedVersion(methodRef));
                        streamWriter.WriteLine(head);
                    }
                }

                // we declare read or written fields
                foreach (var field in FieldTranslator.GetFieldDefinitions())
                {
                    streamWriter.WriteLine(field);
                }

                streamWriter.Close();

                foreach (var bplInputFile in Settings.BplInputFiles)
                {
                    var output = new FileStream(outputPath, FileMode.Append, FileAccess.Write);
                    ////output.WriteLine("// Appending {0}", bplInputFile);
                    ////streamWriter.Flush();

                    using (var inputStream = File.OpenRead(bplInputFile))
                    {
                        inputStream.CopyTo(output);//streamWriter.BaseStream);
                    }

                    output.Close();
                }
            }
        }