/// Infer the type of every variable.
Infer the type of every variable.
Infer the type of every variable .
pub fn infer_types<
        Store: store::Store,
        Heap: heap::Heap,
    > (
        &self,
        view: &View<Store, Heap>,
    ) -> Result<Vec<Type>, String>
    {
        let mut types: Vec<Option<Type>> = self.variable_names.iter().map(|_| None).collect();
        for statement in &self.where_statements {
            let entity_type = types[statement.entity.0 as usize];
            let entity_name = &self.variable_names[statement.entity.0 as usize];
            match entity_type {
                None => types[statement.entity.0 as usize] = Some(Type::Ref),
                Some(Type::Ref) => { /* Ok, the types unify. */ }
                Some(t) => return Err(format!(
                    "Type mismatch for variable '{}': used as ref and {:?}.", entity_name, t
                )),
            };
            let aid = match statement.attribute {
                QueryAttribute::Fixed(id) => id,
                QueryAttribute::Named(..) =>
                    panic!("Should have fixed attributes before type inference."),
            };
            // TODO: What if the attribute does not exist?
            let attr_type = view.lookup_attribute_type(aid);
            let value = match statement.value {
                QueryValue::Const(..) => {
                    // TODO: Check that the value base type (uint64 or bytes) is
                    // compatible with the attribute type.
                    continue
                }
                QueryValue::Var(v) => v,
            };
            let value_type = types[value.0 as usize];
            let value_name = &self.variable_names[value.0 as usize];
            match value_type {
                None => types[value.0 as usize] = Some(attr_type),
                Some(t) if t == attr_type => { /* Ok, the types unify. */ },
                Some(t) => return Err(format!(
                    "Type mismatch for variable '{}': used as {:?} and {:?}.", value_name, t, attr_type
                )),
            }
        }
        let result = types
            .iter()
            .map(|opt_t| match opt_t {
                Some(t) => t,
                None => panic!("A variable did not occur in any statement."),
            })
            .cloned()
            .collect();
        Ok(result)
    }
pub fn infer_types < Store : store :: Store , Heap : heap :: Heap , > ( & self , view : & View < Store , Heap > , ) -> Result < Vec < Type > , String > { let mut types : Vec < Option < Type > > = self . variable_names . iter ( ) . map ( | _ | None ) . collect ( ) ; for statement in & self . where_statements { let entity_type = types [ statement . entity . 0 as usize ] ; let entity_name = & self . variable_names [ statement . entity . 0 as usize ] ; match entity_type { None => types [ statement . entity . 0 as usize ] = Some ( Type :: Ref ) , Some ( Type :: Ref ) => { /* Ok, the types unify. */ } Some ( t ) => return Err ( format ! ( " " entity_name t ) ) , } ; let aid = match statement . attribute { QueryAttribute :: Fixed ( id ) => id , QueryAttribute :: Named ( .. ) => panic ! ( " " ) , } ; // TODO: What if the attribute does not exist? let attr_type = view . lookup_attribute_type ( aid ) ; let value = match statement . value { QueryValue :: Const ( .. ) => { // TODO: Check that the value base type (uint64 or bytes) is // compatible with the attribute type. continue } QueryValue :: Var ( v ) => v , } ; let value_type = types [ value . 0 as usize ] ; let value_name = & self . variable_names [ value . 0 as usize ] ; match value_type { None => types [ value . 0 as usize ] = Some ( attr_type ) , Some ( t ) if t == attr_type => { /* Ok, the types unify. */ } , Some ( t ) => return Err ( format ! ( " " value_name t attr_type ) ) , } } let result = types . iter ( ) . map ( | opt_t | match opt_t { Some ( t ) => t , None => panic ! ( " " ) , } ) . cloned ( ) . collect ( ) ; Ok ( result ) }
Infer the type of every variable.
Infer the type of every variable .
&self
view
&View<Store, Heap>