Skip to content
This repository has been archived by the owner on Jul 3, 2024. It is now read-only.

Commit

Permalink
merge: PR #83 for #71
Browse files Browse the repository at this point in the history
71 - Create references structure
  • Loading branch information
0xmemorygrinder authored Sep 3, 2023
2 parents 3d88d21 + f3d0754 commit 044340c
Show file tree
Hide file tree
Showing 14 changed files with 1,613 additions and 0 deletions.
10 changes: 10 additions & 0 deletions libs/ast-references/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Generated by Cargo
# will have compiled files and executables
debug/
target/

# These are backup files generated by rustfmt
**/*.rs.bk

# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb
64 changes: 64 additions & 0 deletions libs/ast-references/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions libs/ast-references/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "ast-references"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
proc-macro2 = { version = "1.0.66", features = ["span-locations"]}
syn-solidity = { version = "0.3.1", features = ["visit"] }
1 change: 1 addition & 0 deletions libs/ast-references/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod types;
9 changes: 9 additions & 0 deletions libs/ast-references/src/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
pub mod contract_reference;
pub mod enum_reference;
pub mod error_reference;
pub mod event_reference;
pub mod file_reference;
pub mod function_reference;
pub mod location;
pub mod variable_reference;
pub mod struct_reference;
199 changes: 199 additions & 0 deletions libs/ast-references/src/types/contract_reference.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
/**
* ContractReference.rs
* Definition of ContractReference struct
* author: 0xMemoryGrinder
*/

use std::fmt;
use std::hash::{Hash, Hasher};
use std::rc::Rc;
use std::cell::RefCell;
use crate::types::location::Location;
use crate::types::file_reference::FileReference;
use crate::types::struct_reference::StructReference;
use crate::types::function_reference::FunctionReference;
use crate::types::variable_reference::VariableReference;

use super::error_reference::ErrorReference;
use super::event_reference::EventReference;

/******************************************************************************
* Types *
*****************************************************************************/

pub struct ContractReference {
pub name: String,
pub location: Location,
pub file: Rc<RefCell<FileReference>>,
pub structs: Vec<Rc<RefCell<StructReference>>>,
pub functions: Vec<Rc<RefCell<FunctionReference>>>,
pub properties: Vec<Rc<RefCell<VariableReference>>>,
pub errors: Vec<Rc<RefCell<ErrorReference>>>,
pub events: Vec<Rc<RefCell<EventReference>>>,
}

/******************************************************************************
* Methods / Trait implementation *
*****************************************************************************/

impl ContractReference {
pub fn new(name: String, location: Location, file: &Rc<RefCell<FileReference>>) -> ContractReference {
ContractReference {
name: name,
location: location,
file: file.clone(),
structs: Vec::new(),
functions: Vec::new(),
properties: Vec::new(),
errors: Vec::new(),
events: Vec::new(),
}
}

pub fn add_struct(&mut self, strct: &Rc<RefCell<StructReference>>) {
self.structs.push(strct.clone());
}

pub fn add_function(&mut self, function: &Rc<RefCell<FunctionReference>>) {
self.functions.push(function.clone());
}

pub fn add_property(&mut self, property: &Rc<RefCell<VariableReference>>) {
self.properties.push(property.clone());
}

pub fn add_error(&mut self, error: &Rc<RefCell<ErrorReference>>) {
self.errors.push(error.clone());
}

pub fn add_event(&mut self, event: &Rc<RefCell<EventReference>>) {
self.events.push(event.clone());
}
}

impl fmt::Display for ContractReference {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Contract {} at {}", self.name, self.location)
}
}

impl fmt::Debug for ContractReference {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Contract {} at {}", self.name, self.location)
}
}

impl PartialEq for ContractReference {
fn eq(&self, other: &ContractReference) -> bool {
self.name == other.name && self.location == other.location && self.file == other.file
}
}

impl Eq for ContractReference {}

impl Hash for ContractReference {
fn hash<H: Hasher>(&self, state: &mut H) {
self.name.hash(state);
self.location.hash(state);
}
}

/******************************************************************************
* Tests *
*****************************************************************************/

#[cfg(test)]
mod tests {
use std::cell::RefCell;

use proc_macro2::Span;
use syn_solidity::{kw::function, Type};

use crate::types::location::Bound;

use super::*;

#[test]
fn new_good_construct() {
let file = Rc::new(RefCell::new(FileReference::new("File.test".to_string())));
let result = ContractReference::new("Test".to_string(), Location::new("File.test".to_string(), Bound {line: 0, column: 0}, Bound { line: 0, column: 0}), &file);

assert_eq!(result.file, file);
assert_eq!(result.name, "Test");
assert_eq!(result.location.file, "File.test");
assert_eq!(result.location.start.line, 0);
assert_eq!(result.location.start.column, 0);
assert_eq!(result.location.end.line, 0);
assert_eq!(result.location.end.column, 0);
}

#[test]
fn add_struct() {
let file = Rc::new(RefCell::new(FileReference::new("File.test".to_string())));
let result = Rc::new(RefCell::new(ContractReference::new("Test".to_string(), Location::new("File.test".to_string(), Bound {line: 0, column: 0}, Bound { line: 0, column: 0}), &file)));
let strct = Rc::new(RefCell::new(StructReference::new("TestStruct".to_string(), Location::new("File.test".to_string(), Bound {line: 0, column: 0}, Bound { line: 0, column: 0}), Some(&result), Some(&file))));

(*result).borrow_mut().add_struct(&strct);

assert_eq!(result.borrow().structs.len(), 1);
assert_eq!(result.borrow().structs[0], strct);
}

#[test]
fn add_function() {
let file = Rc::new(RefCell::new(FileReference::new("File.test".to_string())));
let result = Rc::new(RefCell::new(ContractReference::new("Test".to_string(), Location::new("File.test".to_string(), Bound {line: 0, column: 0}, Bound { line: 0, column: 0}), &file)));
let function = Rc::new(
RefCell::new(
FunctionReference::new(
"TestFunction".to_string(),
syn_solidity::FunctionKind::Function(function(proc_macro2::Span::call_site())),
Location::new(
"File.test".to_string(),
Bound {line: 0, column: 0},
Bound { line: 0, column: 0}
),
&result)));

(*result).borrow_mut().add_function(&function);

assert_eq!(result.borrow().functions.len(), 1);
assert_eq!(result.borrow().functions[0], function);
}

#[test]
fn add_property() {
let file = Rc::new(RefCell::new(FileReference::new("File.test".to_string())));
let result = Rc::new(RefCell::new(ContractReference::new("Test".to_string(), Location::new("File.test".to_string(), Bound {line: 0, column: 0}, Bound { line: 0, column: 0}), &file)));
let property = Rc::new(RefCell::new(VariableReference::new("TestProperty".to_string(), Type::Bool(Span::call_site()), Location::new("File.test".to_string(), Bound {line: 0, column: 0}, Bound { line: 0, column: 0}), Some(&result), None)));

(*result).borrow_mut().add_property(&property);

assert_eq!(result.borrow().properties.len(), 1);
assert_eq!(result.borrow().properties[0], property);
}

#[test]
fn add_error() {
let file = Rc::new(RefCell::new(FileReference::new("File.test".to_string())));
let result = Rc::new(RefCell::new(ContractReference::new("Test".to_string(), Location::new("File.test".to_string(), Bound {line: 0, column: 0}, Bound { line: 0, column: 0}), &file)));
let error = Rc::new(RefCell::new(ErrorReference::new("TestError".to_string(), Location::new("File.test".to_string(), Bound {line: 0, column: 0}, Bound { line: 0, column: 0}), Some(&result), None)));

(*result).borrow_mut().add_error(&error);

assert_eq!(result.borrow().errors.len(), 1);
assert_eq!(result.borrow().errors[0], error);
}

#[test]
fn add_event() {
let file = Rc::new(RefCell::new(FileReference::new("File.test".to_string())));
let result = Rc::new(RefCell::new(ContractReference::new("Test".to_string(), Location::new("File.test".to_string(), Bound {line: 0, column: 0}, Bound { line: 0, column: 0}), &file)));
let event = Rc::new(RefCell::new(EventReference::new("TestEvent".to_string(), Location::new("File.test".to_string(), Bound {line: 0, column: 0}, Bound { line: 0, column: 0}), Some(&result), None)));

(*result).borrow_mut().add_event(&event);

assert_eq!(result.borrow().events.len(), 1);
assert_eq!(result.borrow().events[0], event);
}
}
Loading

0 comments on commit 044340c

Please sign in to comment.