Browse Source

Remove Expression::DynamicArraySubscript

We can use Expressin::Subscript for this.

Signed-off-by: Sean Young <sean@mess.org>
Sean Young 3 years ago
parent
commit
e1d9e8c5d7

+ 0 - 4
src/bin/languageserver/mod.rs

@@ -604,10 +604,6 @@ impl SolangServer {
             ast::Expression::DynamicArrayLength(_locs, expr1) => {
                 SolangServer::construct_expr(expr1, lookup_tbl, symtab, fnc_map, ns);
             }
-            ast::Expression::DynamicArraySubscript(_locs, _typ, expr1, expr2) => {
-                SolangServer::construct_expr(expr1, lookup_tbl, symtab, fnc_map, ns);
-                SolangServer::construct_expr(expr2, lookup_tbl, symtab, fnc_map, ns);
-            }
             ast::Expression::StorageBytesSubscript(_locs, expr1, expr2) => {
                 SolangServer::construct_expr(expr1, lookup_tbl, symtab, fnc_map, ns);
                 SolangServer::construct_expr(expr2, lookup_tbl, symtab, fnc_map, ns);

+ 0 - 5
src/codegen/cfg.rs

@@ -565,11 +565,6 @@ impl ControlFlowGraph {
                 self.expr_to_string(contract, ns, a),
                 self.expr_to_string(contract, ns, i)
             ),
-            Expression::DynamicArraySubscript(_, _, a, i) => format!(
-                "(darray index {}[{}])",
-                self.expr_to_string(contract, ns, a),
-                self.expr_to_string(contract, ns, i)
-            ),
             Expression::StorageBytesSubscript(_, a, i) => format!(
                 "(storage bytes index {}[{}])",
                 self.expr_to_string(contract, ns, a),

+ 0 - 14
src/codegen/constant_folding.rs

@@ -944,20 +944,6 @@ fn expression(
                 false,
             )
         }
-        Expression::DynamicArraySubscript(loc, ty, array, index) => {
-            let array = expression(array, vars, pos, cfg, ns);
-            let index = expression(index, vars, pos, cfg, ns);
-
-            (
-                Expression::DynamicArraySubscript(
-                    *loc,
-                    ty.clone(),
-                    Box::new(array.0),
-                    Box::new(index.0),
-                ),
-                false,
-            )
-        }
         Expression::StorageBytesSubscript(loc, array, index) => {
             let array = expression(array, vars, pos, cfg, ns);
             let index = expression(index, vars, pos, cfg, ns);

+ 3 - 3
src/codegen/dead_storage.rs

@@ -273,9 +273,9 @@ fn instr_transfers(block_no: usize, block: &BasicBlock) -> Vec<Vec<Transfer>> {
 fn array_var(expr: &Expression) -> Option<usize> {
     match expr {
         Expression::Variable(_, _, var_no) => Some(*var_no),
-        Expression::DynamicArraySubscript(_, _, expr, _)
-        | Expression::Subscript(_, _, expr, _)
-        | Expression::StructMember(_, _, expr, _) => array_var(expr),
+        Expression::Subscript(_, _, expr, _) | Expression::StructMember(_, _, expr, _) => {
+            array_var(expr)
+        }
         _ => None,
     }
 }

+ 2 - 2
src/codegen/expression.rs

@@ -2311,9 +2311,9 @@ fn array_subscript(
                 Box::new(array),
                 Box::new(Expression::Variable(index_loc, coerced_ty, pos)),
             ),
-            Type::DynamicBytes | Type::Array(_, _) => Expression::DynamicArraySubscript(
+            Type::DynamicBytes | Type::Array(_, _) => Expression::Subscript(
                 *loc,
-                array_ty.array_deref(),
+                array_ty.clone(),
                 Box::new(array),
                 Box::new(Expression::Variable(index_loc, coerced_ty, pos)),
             ),

+ 3 - 3
src/codegen/reaching_definitions.rs

@@ -178,9 +178,9 @@ fn instr_transfers(block_no: usize, block: &BasicBlock) -> Vec<Vec<Transfer>> {
 fn array_var(expr: &Expression) -> Option<usize> {
     match expr {
         Expression::Variable(_, _, var_no) => Some(*var_no),
-        Expression::DynamicArraySubscript(_, _, expr, _)
-        | Expression::Subscript(_, _, expr, _)
-        | Expression::StructMember(_, _, expr, _) => array_var(expr),
+        Expression::Subscript(_, _, expr, _) | Expression::StructMember(_, _, expr, _) => {
+            array_var(expr)
+        }
         _ => None,
     }
 }

+ 1 - 3
src/codegen/unused_variable.rs

@@ -36,9 +36,7 @@ pub fn should_remove_assignment(
 
         Expression::StructMember(_, _, str, _) => should_remove_assignment(ns, str, func, opt),
 
-        Expression::Subscript(_, _, array, _)
-        | Expression::DynamicArraySubscript(_, _, array, _)
-        | Expression::StorageBytesSubscript(_, array, _) => {
+        Expression::Subscript(_, _, array, _) | Expression::StorageBytesSubscript(_, array, _) => {
             should_remove_assignment(ns, array, func, opt)
         }
 

+ 38 - 37
src/emit/mod.rs

@@ -2327,6 +2327,44 @@ pub trait TargetRuntime<'a> {
 
                     self.storage_subscript(bin, function, ty, array, index, ns)
                         .into()
+                } else if ty.is_dynamic_memory() {
+                    let array = self.expression(bin, a, vartab, function, ns);
+
+                    let elem_ty = ty.array_deref();
+
+                    let ty = bin.llvm_var(&elem_ty, ns);
+
+                    let mut array_index = self
+                        .expression(bin, i, vartab, function, ns)
+                        .into_int_value();
+
+                    // bounds checking already done; we can down-cast if necessary
+                    if array_index.get_type().get_bit_width() > 32 {
+                        array_index = bin.builder.build_int_truncate(
+                            array_index,
+                            bin.context.i32_type(),
+                            "index",
+                        );
+                    }
+
+                    let index = bin.builder.build_int_mul(
+                        array_index,
+                        ty.into_pointer_type()
+                            .get_element_type()
+                            .size_of()
+                            .unwrap()
+                            .const_cast(bin.context.i32_type(), false),
+                        "",
+                    );
+
+                    let elem = unsafe {
+                        bin.builder
+                            .build_gep(bin.vector_bytes(array), &[index], "index_access")
+                    };
+
+                    bin.builder
+                        .build_pointer_cast(elem, ty.into_pointer_type(), "elem")
+                        .into()
                 } else {
                     let array = self
                         .expression(bin, a, vartab, function, ns)
@@ -2356,43 +2394,6 @@ pub trait TargetRuntime<'a> {
                 self.get_storage_bytes_subscript(bin, function, slot, index)
                     .into()
             }
-            Expression::DynamicArraySubscript(_, elem_ty, a, i) => {
-                let array = self.expression(bin, a, vartab, function, ns);
-
-                let ty = bin.llvm_var(elem_ty, ns);
-
-                let mut array_index = self
-                    .expression(bin, i, vartab, function, ns)
-                    .into_int_value();
-
-                // bounds checking already done; we can down-cast if necessary
-                if array_index.get_type().get_bit_width() > 32 {
-                    array_index = bin.builder.build_int_truncate(
-                        array_index,
-                        bin.context.i32_type(),
-                        "index",
-                    );
-                }
-
-                let index = bin.builder.build_int_mul(
-                    array_index,
-                    ty.into_pointer_type()
-                        .get_element_type()
-                        .size_of()
-                        .unwrap()
-                        .const_cast(bin.context.i32_type(), false),
-                    "",
-                );
-
-                let elem = unsafe {
-                    bin.builder
-                        .build_gep(bin.vector_bytes(array), &[index], "index_access")
-                };
-
-                bin.builder
-                    .build_pointer_cast(elem, ty.into_pointer_type(), "elem")
-                    .into()
-            }
             Expression::StructMember(_, _, a, i) => {
                 let struct_ptr = self
                     .expression(bin, a, vartab, function, ns)

+ 1 - 12
src/sema/ast.rs

@@ -548,7 +548,6 @@ pub enum Expression {
 
     AllocDynamicArray(pt::Loc, Type, Box<Expression>, Option<Vec<u8>>),
     DynamicArrayLength(pt::Loc, Box<Expression>),
-    DynamicArraySubscript(pt::Loc, Type, Box<Expression>, Box<Expression>),
     StorageBytesSubscript(pt::Loc, Box<Expression>, Box<Expression>),
     StorageArrayLength {
         loc: pt::Loc,
@@ -836,14 +835,6 @@ impl Expression {
                 Expression::DynamicArrayLength(loc, expr) => {
                     Expression::DynamicArrayLength(*loc, Box::new(filter(expr, ctx)))
                 }
-                Expression::DynamicArraySubscript(loc, ty, left, right) => {
-                    Expression::DynamicArraySubscript(
-                        *loc,
-                        ty.clone(),
-                        Box::new(filter(left, ctx)),
-                        Box::new(filter(right, ctx)),
-                    )
-                }
                 Expression::StorageBytesSubscript(loc, storage, index) => {
                     Expression::StorageBytesSubscript(
                         *loc,
@@ -1075,8 +1066,7 @@ impl Expression {
 
                 Expression::AllocDynamicArray(_, _, expr, _)
                 | Expression::DynamicArrayLength(_, expr) => expr.recurse(cx, f),
-                Expression::DynamicArraySubscript(_, _, left, right)
-                | Expression::StorageBytesSubscript(_, left, right) => {
+                Expression::StorageBytesSubscript(_, left, right) => {
                     left.recurse(cx, f);
                     right.recurse(cx, f);
                 }
@@ -1226,7 +1216,6 @@ impl Expression {
             | Expression::Or(loc, _, _)
             | Expression::AllocDynamicArray(loc, _, _, _)
             | Expression::DynamicArrayLength(loc, _)
-            | Expression::DynamicArraySubscript(loc, _, _, _)
             | Expression::StorageBytesSubscript(loc, _, _)
             | Expression::StorageArrayLength { loc, .. }
             | Expression::StringCompare(loc, _, _)

+ 0 - 15
src/sema/dotgraphviz.rs

@@ -937,22 +937,7 @@ impl Dot {
 
                 self.add_expression(expr, func, ns, node, String::from("expr"));
             }
-            Expression::DynamicArraySubscript(loc, ty, array, index) => {
-                let node = self.add_node(
-                    Node::new(
-                        "subscript",
-                        vec![
-                            format!("array subscript {}", ty.to_string(ns)),
-                            ns.files[loc.0].loc_to_string(loc),
-                        ],
-                    ),
-                    Some(parent),
-                    Some(parent_rel),
-                );
 
-                self.add_expression(array, func, ns, node, String::from("array"));
-                self.add_expression(index, func, ns, node, String::from("index"));
-            }
             Expression::StorageBytesSubscript(loc, array, index) => {
                 let node = self.add_node(
                     Node::new(

+ 0 - 1
src/sema/expression.rs

@@ -82,7 +82,6 @@ impl Expression {
             | Expression::Ternary(_, ty, _, _, _)
             | Expression::StructMember(_, ty, _, _)
             | Expression::AllocDynamicArray(_, ty, _, _)
-            | Expression::DynamicArraySubscript(_, ty, _, _)
             | Expression::PreIncrement(_, ty, ..)
             | Expression::PreDecrement(_, ty, ..)
             | Expression::PostIncrement(_, ty, ..)

+ 0 - 7
src/sema/printer.rs

@@ -460,13 +460,6 @@ fn print_expr(e: &Expression, func: Option<&Function>, ns: &Namespace) -> Tree {
             String::from("dynamic array length"),
             vec![print_expr(array, func, ns)],
         ),
-        Expression::DynamicArraySubscript(_, ty, array, index) => Tree::Branch(
-            format!("dynamic array subscript {}", ty.to_string(ns)),
-            vec![
-                Tree::Branch(String::from("array"), vec![print_expr(array, func, ns)]),
-                Tree::Branch(String::from("index"), vec![print_expr(index, func, ns)]),
-            ],
-        ),
         Expression::StorageBytesSubscript(_, array, index) => Tree::Branch(
             String::from("storage bytes subscript"),
             vec![

+ 10 - 0
src/sema/types.rs

@@ -1076,6 +1076,16 @@ impl Type {
         matches!(self, Type::StorageRef(_, _))
     }
 
+    /// Is this a reference to contract storage?
+    pub fn is_dynamic_memory(&self) -> bool {
+        match self {
+            Type::DynamicBytes => true,
+            Type::Array(_, dim) if dim.len() == 1 && dim[0].is_none() => true,
+            Type::Ref(ty) => ty.is_dynamic_memory(),
+            _ => false,
+        }
+    }
+
     /// Is this a storage bytes string
     pub fn is_storage_bytes(&self) -> bool {
         if let Type::StorageRef(_, ty) = self {

+ 0 - 2
src/sema/unused_variable.rs

@@ -21,7 +21,6 @@ pub fn assigned_variable(ns: &mut Namespace, exp: &Expression, symtable: &mut Sy
         }
 
         Expression::Subscript(_, _, array, index)
-        | Expression::DynamicArraySubscript(_, _, array, index)
         | Expression::StorageBytesSubscript(_, array, index) => {
             assigned_variable(ns, array, symtable);
             used_variable(ns, index, symtable);
@@ -67,7 +66,6 @@ pub fn used_variable(ns: &mut Namespace, exp: &Expression, symtable: &mut Symtab
         }
 
         Expression::Subscript(_, _, array, index)
-        | Expression::DynamicArraySubscript(_, _, array, index)
         | Expression::StorageBytesSubscript(_, array, index) => {
             used_variable(ns, array, symtable);
             used_variable(ns, index, symtable);