Exemple #1
0
    private static StatementData CheckStatement_LocalVarDef(
        ref PassData passData, TypeData retType,
        ES_AstLocalVarDefinition varDef
        )
    {
        var idPool   = passData.Env.IdPool;
        var symbols  = passData.Symbols;
        var irWriter = passData.IRWriter;

        var typeUnknMut = passData.GetUnknownType(Constness.Mutable);

        var implicitType = varDef.ValueType is null;

        var varType = !implicitType?UnpackFirstConst(GetTypeRef (varDef.ValueType)) : TypeData.Null;

        var allVarsFlags = (SymbolFlags)0;

        if (varDef.UsingVar)
        {
            allVarsFlags |= SymbolFlags.UsingVar;
        }

        foreach (var variable in varDef.Variables)
        {
            var varName   = variable.Name.Text.Span;
            var varNameId = idPool.GetIdentifier(varName);

            using var exprList = new ExpressionList(null, null);
            ExpressionData  varExprData;
            ESIR_Expression?valueExpr;
            int?            valueReg;

            var varFlags = allVarsFlags;

            if (!implicitType)
            {
                if (variable.InitializationExpression is not null)
                {
                    varExprData = CheckExpression(ref passData, variable.InitializationExpression, varType);
                    EnsureCompat(ref varExprData, varType, ref passData, varExprData.Expr.NodeBounds);

                    exprList.Merge(ref varExprData.Expressions);

                    valueExpr = varExprData.Value;
                    valueReg  = varExprData.ValueRegister;
                }
                else
                {
                    varExprData = default;
                    valueExpr   = null;
                    valueReg    = null;
                }
            }
            else
            {
                Debug.Assert(variable.InitializationExpression is not null);
                varExprData = CheckExpression(ref passData, variable.InitializationExpression, typeUnknMut);

                exprList.Merge(ref varExprData.Expressions);

                varType   = varExprData.Type;
                valueExpr = varExprData.Value;
                valueReg  = varExprData.ValueRegister;

                if (varType.Type is not null && varType.Type->TypeTag == ES_TypeTag.Null)
                {
                    passData.ErrorList.Add(new (passData.Source, varExprData.Expr.NodeBounds, ES_FrontendErrors.NoImplicitNullVars));

                    varType = typeUnknMut;
                }