public TypeSpecifier VisitIndexAccessExpression(IndexAccess exp)
        {
            if (!LocalTypes.ContainsKey(exp.Identifier.Source))
            {
                Console.WriteLine($"Trying access non-existant array '{exp.Identifier}'");
                return(TypeSpecifier.Null);
            }
            else
            {
                TypeSpecifier ts = LocalTypes[exp.Identifier.Source];

                if (ts.Dimensions != exp.Expressions.Count)
                {
                    Console.WriteLine($"Mismatched dimensions when accessing array at line: {exp.Identifier.Line}");
                    return(TypeSpecifier.Null);
                }

                for (int i = 0; i < ts.Dimensions; i++)
                {
                    var expr = Examine(exp.Expressions[i]);

                    if (!expr.IsInt())
                    {
                        Console.WriteLine($"Non-integer array index access at line: {exp.Identifier.Line}");
                        return(TypeSpecifier.Null);
                    }
                }

                return(new TypeSpecifier {
                    Type = ts.Type, Dimensions = 0
                });
            }
        }
        /// <summary>
        /// Evaluate object[index]
        /// </summary>
        /// <returns></returns>
        public object VisitIndex(IndexExpr expr)
        {
            var ndxVal     = expr.IndexExp.Evaluate(this);
            var listObject = expr.VarExp.Evaluate(this);

            // Check for empty objects.
            ExceptionHelper.NotNull(expr, listObject, "indexing");
            ExceptionHelper.NotNull(expr, ndxVal, "indexing");

            var lobj = (LObject)listObject;

            // CASE 1. Access
            //      e.g. Array: users[0]
            //      e.g. Map:   users['total']
            if (!expr.IsAssignment)
            {
                var result = EvalHelper.AccessIndex(Ctx.Methods, expr, lobj, (LObject)ndxVal);
                return(result);
            }

            // CASE 2.  Assignment
            //      e.g. Array: users[0]        = 'john'
            //      e.g. Map:   users['total']  = 200
            // NOTE: In this case of assignment, return back a MemberAccess object descripting what is assign
            var indexAccess = new IndexAccess();

            indexAccess.Instance   = lobj;
            indexAccess.MemberName = (LObject)ndxVal;
            return(indexAccess);
        }
        public TypeRef VisitIndexAccess(IndexAccess indexAccess)
        {
            var objType   = CompileExpression(indexAccess.Obj);
            var indexType = CompileExpression(indexAccess.Index);

            if (objType == allTypes.String && indexType == allTypes.Int)
            {
                var stringGetChars = typeof(string).GetMethod("get_Chars", new[] { typeof(int) });
                cil.Emit(OpCodes.Callvirt, module.ImportReference(stringGetChars));
                var charToString = typeof(char).GetMethod(nameof(char.ToString), new[] { typeof(char) });
                cil.Emit(OpCodes.Call, module.ImportReference(charToString));
                return(allTypes.String);
            }
            if (objType.TypeReference is ArrayType arrayType && arrayType.Rank == 1 && indexType == allTypes.Int)
            {
                var    elementType = new TypeRef(arrayType.ElementType);
                OpCode ldelemOpCode;
                if (elementType == allTypes.Bool)
                {
                    ldelemOpCode = OpCodes.Ldelem_U1;
                }
                else if (elementType == allTypes.Int)
                {
                    ldelemOpCode = OpCodes.Ldelem_I4;
                }
                else if (elementType.CanBeNull)
                {
                    ldelemOpCode = OpCodes.Ldelem_Ref;
                }
                else
                {
                    var typeName = allTypes.GetTypeName(elementType);
                    throw MakeError(indexAccess, $"Неподдерживаемый тип элементов массива {typeName}");
                }
                cil.Emit(ldelemOpCode);
                return(elementType);
            }
            {
                var methods = allTypes.GetCallableMethods(objType, "get_Item", new[] { indexType });
                if (methods.Count > 1)
                {
                    throw MakeError(indexAccess, $"Не удалось вызвать индексатор для чтения с аргументом ({allTypes.GetTypeName(indexType)}), так как подошло несколько ({methods.Count})");
                }
                if (methods.Count != 0)
                {
                    cil.Emit(OpCodes.Callvirt, methods[0]);
                    return(methods[0].ReturnType);
                }
                throw MakeError(indexAccess, $"Индексатор для чтения не был найден");
            }
        }
Beispiel #4
0
        public Task Consume(ConsumeContext <JToken> context)
        {
            try
            {
                var message = context.Message.ToObject <Message>();
                if (message == null)
                {
                    return(Task.CompletedTask);
                }

                IResponse response = null;
                if (message.IsDelete)
                {
                    response = IndexAccess.DeleteDocument(message.ContentId);
                }
                else
                {
                    var model = DataAccess.GetDocumentModel(message.ContentId);
                    if (model != null)
                    {
                        response = IndexAccess.UpdateDocument(model);
                    }
                }

                if (response != null && response.IsValid)
                {
                    DataAccess.SetDocumentMessageIndexed(message.Id);
                }
                else
                {
                    Serilog.Log.Error(response.DebugInformation);
                }
            }
            catch (Exception ex)
            {
                Serilog.Log.Error("DocumentConsumer:Error");
                Serilog.Log.Error(ex.Message);
            }


            return(Task.CompletedTask);
        }
Beispiel #5
0
        public Word Visit(IndexAccess indexAccess)
        {
            var result = indexAccess.Expr.Accept(this);

            if (IsError(result))
            {
                return(result);
            }
            PyObj pyObj;

            if (IsMemoryBlock(result))//comentar ese if else si se hace la desereferencia en atomic expr.
            {
                pyObj = ((MemoryBlock)result).Value;
            }
            else
            {
                pyObj = (PyObj)result;
            }
            //rPyObj = (PyObj)rValue;//Descomentar esta linea si se hace la desereferencia en atomic expr.
            return(new IndexSegment(pyObj));
        }
Beispiel #6
0
        public override bool Visit(IndexAccess access)
        {
            if (access.BaseExpression.ToString().Equals("msg.data"))
            {
                return(false);
            }

            DeclarationFinder   declFinder = new DeclarationFinder(access, solidityAST);
            VariableDeclaration decl       = declFinder.getDecl();

            if (results.Contains(decl))
            {
                int numAccesses  = declFinder.getNumAccesses();
                int numIndexDims = getIndexDimSize(decl.TypeName);

                if (numIndexDims != numAccesses)
                {
                    results.Remove(decl);
                }
            }

            return(false);
        }
Beispiel #7
0
        public FunctionDefinition GenerateGetter(VariableDeclaration varDecl)
        {
            FunctionDefinition fnDef = new FunctionDefinition();
            Block body = new Block();

            body.Scope      = 0;
            body.Statements = new List <Statement>();
            ParameterList fnParams = new ParameterList();

            fnParams.Parameters = new List <VariableDeclaration>();
            ParameterList rets = new ParameterList();

            rets.Parameters = new List <VariableDeclaration>();

            fnDef.Visibility       = EnumVisibility.PUBLIC;
            fnDef.Implemented      = true;
            fnDef.Name             = varDecl.Name;
            fnDef.StateMutability  = EnumStateMutability.VIEW;
            fnDef.Body             = body;
            fnDef.Parameters       = fnParams;
            fnDef.ReturnParameters = rets;
            fnDef.Modifiers        = new List <ModifierInvocation>();

            TypeName curType = varDecl.TypeName;

            Identifier ident = new Identifier();

            ident.Name = varDecl.Name;
            ident.ReferencedDeclaration  = varDecl.Id;
            ident.OverloadedDeclarations = new List <int>();
            ident.TypeDescriptions       = varDecl.TypeDescriptions;

            int id = context.IdToNodeMap.Keys.Max() + 1;

            Expression curExpr  = ident;
            List <int> localIds = new List <int>();

            while (curType is Mapping || curType is ArrayTypeName)
            {
                VariableDeclaration paramDecl = new VariableDeclaration();
                paramDecl.Name            = "arg" + id;
                paramDecl.Visibility      = EnumVisibility.DEFAULT;
                paramDecl.StorageLocation = EnumLocation.DEFAULT;
                paramDecl.Id = id;
                context.IdToNodeMap.Add(id, paramDecl);
                fnParams.Parameters.Add(paramDecl);

                if (curType is Mapping map)
                {
                    paramDecl.TypeName         = map.KeyType;
                    paramDecl.TypeDescriptions = map.KeyType.TypeDescriptions;
                    curType = map.ValueType;
                }
                else if (curType is ArrayTypeName arr)
                {
                    TypeDescription intDescription = new TypeDescription();
                    intDescription.TypeString = "uint256";
                    ElementaryTypeName intTypeName = new ElementaryTypeName();
                    intTypeName.TypeDescriptions = intDescription;
                    paramDecl.TypeName           = intTypeName;
                    paramDecl.TypeDescriptions   = intDescription;
                    curType = arr.BaseType;
                }

                Identifier paramIdent = new Identifier();
                paramIdent.Name = paramDecl.Name;
                paramIdent.OverloadedDeclarations = new List <int>();
                paramIdent.TypeDescriptions       = paramDecl.TypeDescriptions;
                paramIdent.ReferencedDeclaration  = paramDecl.Id;

                IndexAccess access = new IndexAccess();
                access.BaseExpression   = curExpr;
                access.IndexExpression  = paramIdent;
                access.TypeDescriptions = TransUtils.TypeNameToTypeDescription(curType);

                curExpr = access;
                id++;
            }

            VariableDeclaration retVar = new VariableDeclaration();

            retVar                  = new VariableDeclaration();
            retVar.Name             = null;
            retVar.TypeDescriptions = TransUtils.TypeNameToTypeDescription(curType);
            retVar.TypeName         = curType;
            rets.Parameters.Add(retVar);

            Return ret = new Return();

            ret.Expression = curExpr;

            context.ASTNodeToSourcePathMap[ret]        = context.ASTNodeToSourcePathMap[varDecl];
            context.ASTNodeToSourceLineNumberMap[ret]  = context.ASTNodeToSourceLineNumberMap[varDecl];
            context.ASTNodeToSourcePathMap[body]       = context.ASTNodeToSourcePathMap[varDecl];
            context.ASTNodeToSourceLineNumberMap[body] = context.ASTNodeToSourceLineNumberMap[varDecl];

            body.Statements.Add(ret);

            return(fnDef);
        }
 public override bool Visit(IndexAccess node)
 {
     node.BaseExpression.Accept(this);
     return(false);
 }
Beispiel #9
0
 public virtual void EndVisit(IndexAccess node)
 {
     CommonEndVisit(node);
 }
Beispiel #10
0
 public virtual bool Visit(IndexAccess node)
 {
     return(CommonVisit(node));
 }
Beispiel #11
0
 public override bool Visit(IndexAccess access)
 {
     numAccesses++;
     access.BaseExpression.Accept(this);
     return(false);
 }