fn zero_initializer(&self, decl_id: CDeclId, type_id: CTypeId, is_static: bool) -> Result<P<Expr>, String> {

        // Look up the decl in the cache and return what we find (if we find anything)
        if let Some(init) = self.zero_inits.borrow().get(&decl_id) {
            return init.clone()
        }

        // Otherwise, construct the initializer
        let init = match self.ast_context.index(decl_id).kind {

            // Zero initialize all of the fields
            CDeclKind::Struct { ref fields, .. } => {
                let name = self.type_converter.borrow().resolve_decl_name(decl_id).unwrap();

                let fields = match *fields {
                    Some(ref fields) => fields,
                    None => return Err(format!("Attempted to zero-initialize forward-declared struct")),
                };

                let fields: Result<Vec<Field>, String> = fields
                    .into_iter()
                    .map(|field_id: &CFieldId| -> Result<Field, String> {
                        let name = self.type_converter.borrow().resolve_field_name(Some(decl_id), *field_id).unwrap();

                        match self.ast_context.index(*field_id).kind {
                            CDeclKind::Field { typ, .. } => {
                                let field_init = self.implicit_default_expr(typ.ctype, is_static)?;
                                Ok(mk().field(name, field_init))
                            }
                            _ => Err(format!("Found non-field in record field list"))
                        }
                    })
                    .collect();

                Ok(mk().struct_expr(vec![name], fields?))
            },

            // Zero initialize the first field
            CDeclKind::Union { ref fields, .. } => {
                let name = self.type_converter.borrow().resolve_decl_name(decl_id).unwrap();

                let fields = match *fields {
                    Some(ref fields) => fields,
                    None => return Err(format!("Attempted to zero-initialize forward-declared struct")),
                };

                let &field_id = fields.first().ok_or(format!("A union should have a field"))?;

                let field = match self.ast_context.index(field_id).kind {
                    CDeclKind::Field { typ, .. } => {
                        let field_init = self.implicit_default_expr(typ.ctype, is_static)?;
                        let name = self.type_converter.borrow().resolve_field_name(Some(decl_id), field_id).unwrap();

                        Ok(mk().field(name, field_init))
                    }
                    _ => Err(format!("Found non-field in record field list"))
                }?;

                Ok(mk().struct_expr(vec![name], vec![field]))
            },

            // Transmute the number `0` into the enum type
            CDeclKind::Enum { .. } => Ok(self.enum_for_i64(type_id, 0)),

            _ => Err(format!("Declaration is not associated with a type"))
        };

        // Insert the initializer into the cache, then return it
        self.zero_inits.borrow_mut().insert(decl_id, init.clone());
        init
    }
/// Produce zero-initializers for structs/unions/enums, looking them up when possible.
Produce zero-initializers for structs/unions/enums, looking them up when possible.
Produce zero - initializers for structs / unions / enums looking them up when possible .
fn zero_initializer(&self, decl_id: CDeclId, type_id: CTypeId, is_static: bool) -> Result<P<Expr>, String> {
        // Look up the decl in the cache and return what we find (if we find anything)
        if let Some(init) = self.zero_inits.borrow().get(&decl_id) {
            return init.clone()
        }
        // Otherwise, construct the initializer
        let init = match self.ast_context.index(decl_id).kind {
            // Zero initialize all of the fields
            CDeclKind::Struct { ref fields, .. } => {
                let name = self.type_converter.borrow().resolve_decl_name(decl_id).unwrap();
                let fields = match *fields {
                    Some(ref fields) => fields,
                    None => return Err(format!("Attempted to zero-initialize forward-declared struct")),
                };
                let fields: Result<Vec<Field>, String> = fields
                    .into_iter()
                    .map(|field_id: &CFieldId| -> Result<Field, String> {
                        let name = self.type_converter.borrow().resolve_field_name(Some(decl_id), *field_id).unwrap();
                        match self.ast_context.index(*field_id).kind {
                            CDeclKind::Field { typ, .. } => {
                                let field_init = self.implicit_default_expr(typ.ctype, is_static)?;
                                Ok(mk().field(name, field_init))
                            }
                            _ => Err(format!("Found non-field in record field list"))
                        }
                    })
                    .collect();
                Ok(mk().struct_expr(vec![name], fields?))
            },
            // Zero initialize the first field
            CDeclKind::Union { ref fields, .. } => {
                let name = self.type_converter.borrow().resolve_decl_name(decl_id).unwrap();
                let fields = match *fields {
                    Some(ref fields) => fields,
                    None => return Err(format!("Attempted to zero-initialize forward-declared struct")),
                };
                let &field_id = fields.first().ok_or(format!("A union should have a field"))?;
                let field = match self.ast_context.index(field_id).kind {
                    CDeclKind::Field { typ, .. } => {
                        let field_init = self.implicit_default_expr(typ.ctype, is_static)?;
                        let name = self.type_converter.borrow().resolve_field_name(Some(decl_id), field_id).unwrap();
                        Ok(mk().field(name, field_init))
                    }
                    _ => Err(format!("Found non-field in record field list"))
                }?;
                Ok(mk().struct_expr(vec![name], vec![field]))
            },
            // Transmute the number `0` into the enum type
            CDeclKind::Enum { .. } => Ok(self.enum_for_i64(type_id, 0)),
            _ => Err(format!("Declaration is not associated with a type"))
        };
        // Insert the initializer into the cache, then return it
        self.zero_inits.borrow_mut().insert(decl_id, init.clone());
        init
    }
fn zero_initializer ( & self , decl_id : CDeclId , type_id : CTypeId , is_static : bool ) -> Result < P < Expr > , String > { // Look up the decl in the cache and return what we find (if we find anything) if let Some ( init ) = self . zero_inits . borrow ( ) . get ( & decl_id ) { return init . clone ( ) } // Otherwise, construct the initializer let init = match self . ast_context . index ( decl_id ) . kind { // Zero initialize all of the fields CDeclKind :: Struct { ref fields , .. } => { let name = self . type_converter . borrow ( ) . resolve_decl_name ( decl_id ) . unwrap ( ) ; let fields = match * fields { Some ( ref fields ) => fields , None => return Err ( format ! ( " " ) ) , } ; let fields : Result < Vec < Field > , String > = fields . into_iter ( ) . map ( | field_id : & CFieldId | -> Result < Field , String > { let name = self . type_converter . borrow ( ) . resolve_field_name ( Some ( decl_id ) , * field_id ) . unwrap ( ) ; match self . ast_context . index ( * field_id ) . kind { CDeclKind :: Field { typ , .. } => { let field_init = self . implicit_default_expr ( typ . ctype , is_static ) ? ; Ok ( mk ( ) . field ( name , field_init ) ) } _ => Err ( format ! ( " " ) ) } } ) . collect ( ) ; Ok ( mk ( ) . struct_expr ( vec ! [ name ] , fields ? ) ) } , // Zero initialize the first field CDeclKind :: Union { ref fields , .. } => { let name = self . type_converter . borrow ( ) . resolve_decl_name ( decl_id ) . unwrap ( ) ; let fields = match * fields { Some ( ref fields ) => fields , None => return Err ( format ! ( " " ) ) , } ; let & field_id = fields . first ( ) . ok_or ( format ! ( " " ) ) ? ; let field = match self . ast_context . index ( field_id ) . kind { CDeclKind :: Field { typ , .. } => { let field_init = self . implicit_default_expr ( typ . ctype , is_static ) ? ; let name = self . type_converter . borrow ( ) . resolve_field_name ( Some ( decl_id ) , field_id ) . unwrap ( ) ; Ok ( mk ( ) . field ( name , field_init ) ) } _ => Err ( format ! ( " " ) ) } ? ; Ok ( mk ( ) . struct_expr ( vec ! [ name ] , vec ! [ field ] ) ) } , // Transmute the number `0` into the enum type CDeclKind :: Enum { .. } => Ok ( self . enum_for_i64 ( type_id , 0 ) ) , _ => Err ( format ! ( " " ) ) } ; // Insert the initializer into the cache, then return it self . zero_inits . borrow_mut ( ) . insert ( decl_id , init . clone ( ) ) ; init }
Produce zero-initializers for structs/unions/enums, looking them up when possible.
Produce zero - initializers for structs / unions / enums looking them up when possible .
&self
decl_id
CDeclId
type_id
CTypeId
is_static
bool
&self
decl_id
CDeclId
type_id
CTypeId
is_static
bool