Ver código fonte

Improve type deref functions

Rename deref() to array_deref() so that reftype() can be called deref()

Signed-off-by: Sean Young <sean@mess.org>
Sean Young 5 anos atrás
pai
commit
f2e1978c8b

+ 3 - 3
src/emit/ethabiencoder.rs

@@ -51,7 +51,7 @@ impl EthAbiEncoder {
                             )
                         };
 
-                        let ty = ty.deref();
+                        let ty = ty.array_deref();
 
                         if ty.is_reference_type() {
                             elem = contract.builder.build_load(elem, "").into_pointer_value();
@@ -423,11 +423,11 @@ impl EthAbiEncoder {
                             )
                         };
 
-                        let ty = ty.deref();
+                        let ty = ty.array_deref();
 
                         if ty.is_reference_type() {
                             let val = contract.builder.build_alloca(
-                                ty.ref_type().llvm_type(contract.ns, contract.context),
+                                ty.deref().llvm_type(contract.ns, contract.context),
                                 "",
                             );
                             self.decode_ty(contract, function, &ty, Some(val), data);

+ 5 - 5
src/emit/mod.rs

@@ -992,7 +992,7 @@ impl<'a> Contract<'a> {
     ) {
         match ty {
             resolver::Type::FixedArray(_, dim) => {
-                let ty = ty.deref();
+                let ty = ty.array_deref();
 
                 self.emit_static_loop_with_int(
                     function,
@@ -1009,7 +1009,7 @@ impl<'a> Contract<'a> {
                         };
 
                         if ty.is_reference_type() {
-                            let ty = ty.ref_type();
+                            let ty = ty.deref();
                             let val = self
                                 .builder
                                 .build_alloca(ty.llvm_type(self.ns, self.context), "");
@@ -1046,7 +1046,7 @@ impl<'a> Contract<'a> {
 
                     if field.ty.is_reference_type() {
                         let val = self.builder.build_alloca(
-                            field.ty.ref_type().llvm_type(self.ns, self.context),
+                            field.ty.deref().llvm_type(self.ns, self.context),
                             &field.name,
                         );
 
@@ -1087,9 +1087,9 @@ impl<'a> Contract<'a> {
         function: FunctionValue<'b>,
         runtime: &dyn TargetRuntime,
     ) {
-        match ty {
+        match ty.deref() {
             resolver::Type::FixedArray(_, dim) => {
-                let ty = ty.deref();
+                let ty = ty.array_deref();
 
                 self.emit_static_loop_with_int(
                     function,

+ 3 - 3
src/emit/substrate.rs

@@ -459,11 +459,11 @@ impl SubstrateTarget {
                             )
                         };
 
-                        let ty = ty.deref();
+                        let ty = ty.array_deref();
 
                         if ty.is_reference_type() {
                             let val = contract.builder.build_alloca(
-                                ty.ref_type().llvm_type(contract.ns, contract.context),
+                                ty.deref().llvm_type(contract.ns, contract.context),
                                 "",
                             );
                             self.decode_ty(contract, function, &ty, Some(val), data);
@@ -634,7 +634,7 @@ impl SubstrateTarget {
                             )
                         };
 
-                        let ty = ty.deref();
+                        let ty = ty.array_deref();
 
                         if ty.is_reference_type() {
                             elem = contract.builder.build_load(elem, "").into_pointer_value()

+ 4 - 4
src/resolver/expression.rs

@@ -2562,7 +2562,7 @@ fn array_subscript(
 ) -> Result<(Expression, resolver::Type), ()> {
     let (array_expr, array_ty) = expression(array, cfg, ns, vartab, errors)?;
 
-    let array_length = match array_ty.ref_type() {
+    let array_length = match array_ty.deref() {
         resolver::Type::Primitive(ast::PrimitiveType::Bytes(n)) => BigInt::from(*n),
         resolver::Type::FixedArray(_, _) => array_ty.array_length().clone(),
         _ => {
@@ -2740,7 +2740,7 @@ fn array_subscript(
             ))
         }
     } else {
-        match array_ty.ref_type() {
+        match array_ty.deref() {
             resolver::Type::Primitive(ast::PrimitiveType::Bytes(array_length)) => {
                 let res_ty = resolver::Type::Primitive(ast::PrimitiveType::Bytes(1));
 
@@ -2787,14 +2787,14 @@ fn array_subscript(
                         &array.loc(),
                         array_expr,
                         &array_ty,
-                        &array_ty.ref_type(),
+                        &array_ty.deref(),
                         true,
                         ns,
                         errors,
                     )?),
                     Box::new(Expression::Variable(index.loc(), pos)),
                 ),
-                array_ty.deref(),
+                array_ty.array_deref(),
             )),
             _ => {
                 // should not happen as type-checking already done

+ 3 - 3
src/resolver/mod.rs

@@ -72,9 +72,9 @@ impl Type {
 
     /// Give the type of an array after dereference. This can only be used on
     /// array types and will cause a panic otherwise.
-    pub fn deref(&self) -> Self {
+    pub fn array_deref(&self) -> Self {
         match self {
-            Type::Ref(t) => t.deref(),
+            Type::Ref(t) => t.array_deref(),
             Type::FixedArray(ty, dim) if dim.len() > 1 => {
                 Type::FixedArray(ty.clone(), dim[..dim.len() - 1].to_vec())
             }
@@ -196,7 +196,7 @@ impl Type {
     }
 
     /// If the type is Ref or StorageRef, get the underlying type
-    pub fn ref_type(&self) -> &Self {
+    pub fn deref(&self) -> &Self {
         match self {
             Type::StorageRef(r) => r,
             Type::Ref(r) => r,

+ 6 - 0
tests/substrate_arrays/mod.rs

@@ -652,6 +652,8 @@ fn struct_array_struct_abi() {
             struct bar  {
                 foo[10] bars;
             }
+
+            bar s;
     
             function get_bar() public returns (bar) {
                 bar a = bar({ bars: [
@@ -667,6 +669,8 @@ fn struct_array_struct_abi() {
                     foo({ f1: 10, f2: true})
                 ]});
 
+                s = a;
+                
                 return a;
             }
 
@@ -675,6 +679,8 @@ fn struct_array_struct_abi() {
                     assert(a.bars[i].f1 == i + 1);
                     assert(a.bars[i].f2 == (i != 6));
                 }
+
+                a = s;
             }
         }"##,
     );