private type_node GetMostCommonType(int kind = 0)
        {
            if (resultExpressionsTypes.Count == 0)
            {
                if (kind == 0)
                {
                    syntaxTreeVisitor.AddError(syntaxTreeVisitor.get_location(lambdaHeader), "IMPOSSIBLE_TO_INFER_RESULT_TYPE_IN_LAMBDA");
                }
                else
                {
                    syntaxTreeVisitor.AddError(syntaxTreeVisitor.get_location(lambdaHeader), "IMPOSSIBLE_TO_INFER_RESULT_TYPE");
                }
            }

            var mostCommonType = resultExpressionsTypes[0].Item1;

            for (var i = 1; i < resultExpressionsTypes.Count; i++)
            {
                if (convertion_data_and_alghoritms.eq_type_nodes(mostCommonType, resultExpressionsTypes[i].Item1))
                {
                    continue;
                }
                var typeComparisonResult = type_table.compare_types(resultExpressionsTypes[i].Item1, mostCommonType);
                if (typeComparisonResult == type_compare.non_comparable_type)
                {
                    syntaxTreeVisitor.AddError(new CanNotConvertTypes(resultExpressionsTypes[i].Item3, resultExpressionsTypes[i].Item1, mostCommonType, syntaxTreeVisitor.get_location(resultExpressionsTypes[i].Item2)));
                }
                if (typeComparisonResult == type_compare.greater_type)
                {
                    mostCommonType = resultExpressionsTypes[i].Item1;
                }
            }

            return(mostCommonType);
        }
        public override void visit(statement_list stmtList)
        {
            if (stmtList.IsInternal) // просто обойти как продолжение объемлющего statement_list
            {
                foreach (var stmt in stmtList.subnodes)
                {
                    ProcessNode(stmt);
                }
                return;
            }

            var stl = new statements_list(_visitor.get_location(stmtList),
                                          _visitor.get_location_with_check(stmtList.left_logical_bracket),
                                          _visitor.get_location_with_check(stmtList.right_logical_bracket));

            _visitor.convertion_data_and_alghoritms.statement_list_stack_push(stl);

            var newTreeNode = new CapturedVariablesTreeNodeBlockScope(_currentTreeNode, stl.Scope.ScopeNum, stmtList);

            if (_rootNode == null)
            {
                _rootNode = newTreeNode;
            }
            if (_currentTreeNode != null)
            {
                _currentTreeNode.ChildNodes.Add(newTreeNode);
            }
            _currentTreeNode = newTreeNode;

            _scopesCapturedVarsNodesDictionary.Add(stl.Scope.ScopeNum, _currentTreeNode);
            foreach (var csi in _pendingCapturedSymbols)
            {
                _currentTreeNode.VariablesDefinedInScope.Add(csi);
            }
            _pendingCapturedSymbols.Clear();
            if (stmtList.subnodes != null)
            {
                foreach (var stmt in stmtList.subnodes)
                {
                    ProcessNode(stmt);
                }
            }

            _visitor.convertion_data_and_alghoritms.statement_list_stack.pop();

            _currentTreeNode = _currentTreeNode.ParentNode;
        }
Esempio n. 3
0
        public static void Substitute(syntax_tree_visitor _visitor, declarations decls, statement_list _statementList)
        {
            var tree   = new CapturedVariablesTreeBuilder(_visitor).BuildTree(_statementList);
            var substs = new CapturedVariablesSubstitutionClassGenerator(tree.RootNode).GenerateSubstitutions();

            new CapturedVariablesSubstitutor(tree.IdentsReferences, substs.GeneratedScopeClassesInfo, substs.LambdasToBeAddedAsMethods, substs.SubstitutionsInfo, tree.CapturedVarsNodesDictionary, substs.ConvertingClassNonPublicMembersMapping, _visitor)
            .Substitute(_statementList);

            if (_visitor.context.converting_block() == block_type.function_block && tree.ProcedureScope != null)
            {
                if (decls != null && decls.defs != null)
                {
                    foreach (var def in decls.defs.Where(d => d is const_definition ||
                                                         d is consts_definitions_list ||
                                                         d is variable_definitions))
                    {
                        var constDef = def as const_definition;
                        if (constDef != null)
                        {
                            var finder = new FindMainIdentsVisitor();
                            finder.ProcessNode(constDef.const_value);

                            foreach (var v in finder.vars)
                            {
                                SymbolInfo si = _visitor.context.find_first(v.name);
                                if (si == null)
                                {
                                    continue;
                                }

                                if (tree.ProcedureScope.VariablesDefinedInScope.Any(var => var.SymbolInfo == si))
                                {
                                    _visitor.AddError(new UsingCapturedParameterIsNotAllowedInInitializers(_visitor.get_location(v)));
                                }
                            }
                            continue;
                        }

                        var constDefList = def as consts_definitions_list;
                        if (constDefList != null)
                        {
                            foreach (var cd in constDefList.const_defs)
                            {
                                var finder = new FindMainIdentsVisitor();
                                finder.ProcessNode(cd.const_value);

                                foreach (var v in finder.vars)
                                {
                                    SymbolInfo si = _visitor.context.find_first(v.name);
                                    if (si == null)
                                    {
                                        continue;
                                    }

                                    if (tree.ProcedureScope.VariablesDefinedInScope.Any(var => var.SymbolInfo == si))
                                    {
                                        _visitor.AddError(new UsingCapturedParameterIsNotAllowedInInitializers(_visitor.get_location(v)));
                                    }
                                }
                            }
                            continue;
                        }

                        var varDefList = def as variable_definitions;
                        if (varDefList != null)
                        {
                            foreach (var d in varDefList.var_definitions)
                            {
                                var finder = new FindMainIdentsVisitor();
                                finder.ProcessNode(d.inital_value);

                                foreach (var v in finder.vars)
                                {
                                    SymbolInfo si = _visitor.context.find_first(v.name);
                                    if (si == null)
                                    {
                                        continue;
                                    }

                                    if (tree.ProcedureScope.VariablesDefinedInScope.Any(var => var.SymbolInfo == si))
                                    {
                                        _visitor.AddError(new UsingCapturedParameterIsNotAllowedInInitializers(_visitor.get_location(v)));
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
Esempio n. 4
0
        public override void visit(statement_list stmtList)
        {
            var stl = new statements_list(_visitor.get_location(stmtList),
                                          _visitor.get_location_with_check(stmtList.left_logical_bracket),
                                          _visitor.get_location_with_check(stmtList.right_logical_bracket));

            _visitor.convertion_data_and_alghoritms.statement_list_stack_push(stl);

            var newTreeNode = new CapturedVariablesTreeNodeBlockScope(_currentTreeNode, stl.Scope.ScopeNum, stmtList);

            if (_rootNode == null)
            {
                _rootNode = newTreeNode;
            }
            if (_currentTreeNode != null)
            {
                _currentTreeNode.ChildNodes.Add(newTreeNode);
            }
            _currentTreeNode = newTreeNode;

            _scopesCapturedVarsNodesDictionary.Add(stl.Scope.ScopeNum, _currentTreeNode);

            if (stmtList.subnodes != null)
            {
                foreach (var stmt in stmtList.subnodes)
                {
                    ProcessNode(stmt);
                }
            }

            _visitor.convertion_data_and_alghoritms.statement_list_stack.pop();

            _currentTreeNode = _currentTreeNode.ParentNode;
        }