Exemple #1
0
        private DeclarationIL?BuildDefaultConstructor(
            IClassDeclaration classDeclaration,
            ISymbolTree symbolTree)
        {
            var constructorSymbol = classDeclaration.DefaultConstructorSymbol;

            if (constructorSymbol is null)
            {
                return(null);
            }

            if (declarationsIL.TryGetValue(constructorSymbol, out var declaration))
            {
                return(declaration);
            }

            var selfParameterSymbol = symbolTree.Children(constructorSymbol).OfType <SelfParameterSymbol>().Single();
            var selfParameter       = new SelfParameterIL(selfParameterSymbol);
            var parameters          = selfParameter.Yield().ToFixedList <ParameterIL>();

            var graph = new ControlFlowGraphBuilder(classDeclaration.File);

            graph.AddSelfParameter(selfParameterSymbol);
            var block = graph.NewBlock();

            block.End(new ReturnVoidInstruction(classDeclaration.NameSpan, Scope.Outer));

            //var il = new ControlFlowGraphBuilder(classDeclaration.File);
            //il.AddSelfParameter(selfType);
            //var block = il.NewBlock();
            //block.End(classDeclaration.NameSpan, Scope.Outer);

            var defaultConstructor = new ConstructorIL(// TODO how to get a name
                constructorSymbol,
                parameters, FixedList <FieldInitializationIL> .Empty, graph.Build());

            //defaultConstructor.ControlFlowOld.InsertedDeletes = new InsertedDeletes();
            declarationsIL.Add(constructorSymbol, defaultConstructor);
            return(defaultConstructor);
        }
Exemple #2
0
        public ControlFlowGraphFabrication(IConcreteInvocableDeclaration invocable)
        {
            this.invocable = invocable;
            graph          = new ControlFlowGraphBuilder(invocable.File);
            // We start in the outer scope and need that on the stack
            var scope = Scope.Outer;

            scopes.Push(scope);
            //nextScope = scope.Next();
            switch (invocable)
            {
            default:
                throw ExhaustiveMatch.Failed(invocable);

            case IConcreteMethodDeclaration method:
                selfParameter = method.SelfParameter.Symbol;
                returnType    = method.Symbol.ReturnDataType.Known();
                break;

            case IConstructorDeclaration constructor:
                selfParameter = constructor.ImplicitSelfParameter.Symbol;
                returnType    = DataType.Void;  // the body should `return;`
                break;

            case IAssociatedFunctionDeclaration associatedFunction:
                returnType = associatedFunction.Symbol.ReturnDataType.Known();
                break;

            case IFunctionDeclaration function:
                returnType = function.Symbol.ReturnDataType.Known();
                break;
            }

            // TODO really use return type
            _ = returnType;
        }