adding in fn def

This commit is contained in:
Edward Chen
2021-09-15 20:54:57 -04:00
parent af4107afea
commit 72b44eec00
5 changed files with 94 additions and 29 deletions

72
src/front/c/ast_utils.rs Normal file
View File

@@ -0,0 +1,72 @@
use lang_c::ast::{
BlockItem,
DeclaratorKind,
DerivedDeclarator,
FunctionDefinition,
ParameterDeclaration,
Statement
};
use lang_c::span::Node;
use std::fmt::{self, Display, Formatter};
pub struct FnInfo {
name: String,
args: Vec<ParameterDeclaration>,
body: Vec<Node<BlockItem>>,
}
impl FnInfo {
fn new(name: String, args: Vec<ParameterDeclaration>, body: Vec<Node<BlockItem>>) -> Self {
Self { name, args, body }
}
}
impl Display for FnInfo {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "name: {},\nargs: {:#?},\nbody: {:#?}", self.name, self.args, self.body)
}
}
pub fn get_fn_info(fn_def: &FunctionDefinition) -> FnInfo {
let name = name_from_func(fn_def);
let args = args_from_func(fn_def).unwrap();
let body = body_from_func(fn_def);
FnInfo {
name,
args: args.to_vec(),
body: body.to_vec(),
}
}
fn name_from_func(fn_def: &FunctionDefinition) -> String {
let dec = &fn_def.declarator.node;
match dec.kind.node {
DeclaratorKind::Identifier(ref id) => id.node.name.clone(),
_ => panic!("Function name not found: {:?}", dec),
}
}
fn args_from_func(fn_def: &FunctionDefinition) -> Option<Vec<ParameterDeclaration>> {
let dec = &fn_def.declarator.node;
dec.derived.iter().find_map(|d| match d.node {
DerivedDeclarator::Function(ref fn_dec) => {
let args = fn_dec.node.parameters.iter().map(|a| {
a.node.clone()
}).collect::<Vec<ParameterDeclaration>>();
Some(args)
}
_ => None
})
}
fn body_from_func(fn_def: &FunctionDefinition) -> &Vec<Node<BlockItem>> {
let stmt = &fn_def.statement.node;
match stmt {
Statement::Compound(nodes) => nodes,
_ => panic!("Function body not found: {:?}", stmt),
}
}

View File

@@ -1,25 +1,20 @@
//! The C front-end
mod ast_utils;
mod parser;
mod term;
mod types;
use super::FrontEnd;
use crate::circify::{Circify, Loc, Val};
use crate::ir::proof::{self, ConstraintMetadata};
use crate::ir::proof;
use crate::ir::term::*;
use lang_c::ast::{
Declarator, DeclaratorKind,
ExternalDeclaration, TranslationUnit,
};
use lang_c::span::Node;
use lang_c::visit;
use lang_c::visit::Visit;
use log::debug;
// use std::collections::HashMap;
use std::fmt::{self, Display, Formatter};
use std::path::{Path, PathBuf};
use std::path::PathBuf;
use term::*;
const PROVER_VIS: Option<PartyId> = Some(proof::PROVER_ID);
const PUBLIC_VIS: Option<PartyId> = None;
@@ -65,6 +60,7 @@ impl Display for Mode {
}
}
/// The C front-end. Implements [FrontEnd].
pub struct C;
impl FrontEnd for C {
@@ -96,11 +92,8 @@ impl CGen {
Self { source, tu, mode }
}
fn get_name(&self, dec: &Declarator) -> String {
match dec.kind.node {
DeclaratorKind::Identifier(ref id) => id.node.name.clone(),
_ => panic!("Declarator Identified not found: {:?}", id.node),
}
fn gen_stmt(&mut self, ) {
}
fn gen(&mut self) {
@@ -111,8 +104,8 @@ impl CGen {
println!("{:#?}", decl);
}
ExternalDeclaration::FunctionDefinition(ref fn_def) => {
let fn_name = self.get_name(&fn_def.node.declarator.node);
let ret_ty =
let fn_info = ast_utils::get_fn_info(&fn_def.node);
println!("{}", fn_info);
}
_ => panic!("Haven't implemented node: {:?}", n.node),
}

View File

@@ -1,15 +1,14 @@
//! C Terms
use crate::ir::term::*;
// use crate::ir::term::*;
// use crate::front::c::types::*;
pub enum CTermData {
CBool,
CInt(Bool, usize)
}
// pub enum CTermData {
// CBool,
// CInt(Ty::Bool, usize)
// }
#[derive(Eq)]
pub struct CTerm {
term: CTermData,
udef: Bool,
}
// pub struct CTerm {
// term: CTermData,
// udef: Ty::Bool,
// }

View File

@@ -1,6 +1,7 @@
//! C Types
pub enum Type {
#[derive(Clone, PartialEq, Eq)]
pub enum Ty {
Uint(usize),
Bool,
}

View File

@@ -510,7 +510,7 @@ impl ToABY {
_ => panic!("Invalid bv-op in BvBinOp: {:?}", o),
}
}
Op::BvExtract(start, end) => {
Op::BvExtract(_start, _end) => {
}
_ => panic!("Non-field in embed_bv: {:?}", t),