Skip to content

Commit

Permalink
Spanned -> Node with ID
Browse files Browse the repository at this point in the history
  • Loading branch information
g-r-a-n-t committed Mar 18, 2021
1 parent bd79e01 commit 90111c3
Show file tree
Hide file tree
Showing 39 changed files with 775 additions and 915 deletions.
30 changes: 27 additions & 3 deletions Cargo.lock

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

33 changes: 13 additions & 20 deletions analyzer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ use crate::namespace::types::{
use builtins::GlobalMethod;
use fe_parser::ast as fe;
use fe_parser::span::{
Node,
Span,
Spanned,
};
use std::cell::RefCell;
use std::collections::{
Expand Down Expand Up @@ -326,7 +326,7 @@ impl Context {
}

/// Attribute contextual information to an emit statement node.
pub fn add_emit(&mut self, spanned: &Spanned<fe::FuncStmt>, event: EventDef) {
pub fn add_emit(&mut self, spanned: &Node<fe::FuncStmt>, event: EventDef) {
self.emits.insert(spanned.span, event);
}

Expand All @@ -338,7 +338,7 @@ impl Context {
/// Attribute contextual information to a function definition node.
pub fn add_function(
&mut self,
spanned: &Spanned<fe::ContractStmt>,
spanned: &Node<fe::ContractStmt>,
attributes: FunctionAttributes,
) {
self.functions.insert(spanned.span, attributes);
Expand All @@ -350,7 +350,7 @@ impl Context {
}

/// Attribute contextual information to a declaration node.
pub fn add_declaration(&mut self, spanned: &Spanned<fe::FuncStmt>, typ: FixedSize) {
pub fn add_declaration(&mut self, spanned: &Node<fe::FuncStmt>, typ: FixedSize) {
self.declarations.insert(spanned.span, typ);
}

Expand All @@ -360,11 +360,7 @@ impl Context {
}

/// Attribute contextual information to a contract definition node.
pub fn add_contract(
&mut self,
spanned: &Spanned<fe::ModuleStmt>,
attributes: ContractAttributes,
) {
pub fn add_contract(&mut self, spanned: &Node<fe::ModuleStmt>, attributes: ContractAttributes) {
self.contracts.insert(spanned.span, attributes);
}

Expand All @@ -374,7 +370,7 @@ impl Context {
}

/// Attribute contextual information to a call expression node.
pub fn add_call(&mut self, spanned: &Spanned<fe::Expr>, call_type: CallType) {
pub fn add_call(&mut self, spanned: &Node<fe::Expr>, call_type: CallType) {
self.calls.insert(spanned.span, call_type);
}

Expand All @@ -384,7 +380,7 @@ impl Context {
}

/// Attribute contextual information to an event definition node.
pub fn add_event(&mut self, spanned: &Spanned<fe::ContractStmt>, event: EventDef) {
pub fn add_event(&mut self, spanned: &Node<fe::ContractStmt>, event: EventDef) {
self.events.insert(spanned.span, event);
}

Expand Down Expand Up @@ -414,8 +410,8 @@ pub mod test_utils {
};
use fe_parser::ast as fe;
use fe_parser::span::{
Node,
Span,
Spanned,
};

pub struct ContextHarness {
Expand Down Expand Up @@ -445,10 +441,7 @@ pub mod test_utils {

pub fn add_expression(&mut self, substr: &str, attributes: ExpressionAttributes) {
let span = self.find_span(substr);
let mock_spanned = Spanned {
span,
node: fe::Expr::Name("foo"),
};
let mock_spanned = Node::new(fe::Expr::Name("foo"), span);
self.context.add_expression(&mock_spanned, attributes)
}

Expand All @@ -460,12 +453,12 @@ pub mod test_utils {

pub fn add_declaration(&mut self, substr: &str, typ: FixedSize) {
let span = self.find_span(substr);
let mock_spanned = Spanned {
span,
node: fe::FuncStmt::Expr {
let mock_spanned = Node::new(
fe::FuncStmt::Expr {
value: fe::Expr::Name("foo"),
},
};
span,
);
self.context.add_declaration(&mock_spanned, typ)
}
}
Expand Down
8 changes: 4 additions & 4 deletions analyzer/src/namespace/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -817,17 +817,17 @@ pub fn type_desc(defs: &HashMap<String, Type>, typ: &fe::TypeDesc) -> Result<Typ
Err(SemanticError::undefined_value())
}
fe::TypeDesc::Array { typ, dimension } => Ok(Type::Array(Array {
inner: type_desc_base(defs, &typ.node)?,
inner: type_desc_base(defs, &typ.kind)?,
size: *dimension,
})),
fe::TypeDesc::Map { from, to } => Ok(Type::Map(Map {
key: type_desc_base(defs, &from.node)?,
value: Box::new(type_desc(defs, &to.node)?),
key: type_desc_base(defs, &from.kind)?,
value: Box::new(type_desc(defs, &to.kind)?),
})),
fe::TypeDesc::Tuple { items } => Ok(Type::Tuple(Tuple {
items: items
.iter()
.map(|typ| type_desc_base(defs, &typ.node))
.map(|typ| type_desc_base(defs, &typ.kind))
.collect::<Result<_, _>>()?,
})),
}
Expand Down
9 changes: 3 additions & 6 deletions analyzer/src/traversal/_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,14 @@ use crate::{
};
use fe_parser::ast as fe;
use fe_parser::span::{
Node,
Span,
Spanned,
};

/// Creates a new spanned expression. Useful in cases where an `Expr` is nested
/// within the node of a `Spanned` object.
pub fn spanned_expression<'a>(span: &Span, exp: &fe::Expr<'a>) -> Spanned<fe::Expr<'a>> {
Spanned {
node: (*exp).clone(),
span: (*span).to_owned(),
}
pub fn spanned_expression<'a>(span: &Span, exp: &fe::Expr<'a>) -> Node<fe::Expr<'a>> {
Node::new(exp.to_owned(), span.to_owned())
}

pub fn expression_attributes_to_types(attributes: Vec<ExpressionAttributes>) -> Vec<Type> {
Expand Down
6 changes: 3 additions & 3 deletions analyzer/src/traversal/assignments.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::traversal::expressions;
use crate::Context;
use crate::Location;
use fe_parser::ast as fe;
use fe_parser::span::Spanned;
use fe_parser::span::Node;
use std::rc::Rc;

/// Gather context information for assignments and check for type errors.
Expand All @@ -16,9 +16,9 @@ use std::rc::Rc;
pub fn assign(
scope: Shared<BlockScope>,
context: Shared<Context>,
stmt: &Spanned<fe::FuncStmt>,
stmt: &Node<fe::FuncStmt>,
) -> Result<(), SemanticError> {
if let fe::FuncStmt::Assign { targets, value } = &stmt.node {
if let fe::FuncStmt::Assign { targets, value } = &stmt.kind {
if targets.len() > 1 {
unimplemented!()
}
Expand Down
42 changes: 21 additions & 21 deletions analyzer/src/traversal/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,21 @@ use crate::{
ContractAttributes,
};
use fe_parser::ast as fe;
use fe_parser::span::Spanned;
use fe_parser::span::Node;
use std::rc::Rc;

/// Gather context information for contract definitions and check for type
/// errors.
pub fn contract_def(
module_scope: Shared<ModuleScope>,
context: Shared<Context>,
stmt: &Spanned<fe::ModuleStmt>,
stmt: &Node<fe::ModuleStmt>,
) -> Result<Shared<ContractScope>, SemanticError> {
if let fe::ModuleStmt::ContractDef { name, body } = &stmt.node {
let contract_scope = ContractScope::new(name.node, Rc::clone(&module_scope));
if let fe::ModuleStmt::ContractDef { name, body } = &stmt.kind {
let contract_scope = ContractScope::new(name.kind, Rc::clone(&module_scope));

for stmt in body.iter() {
match &stmt.node {
match &stmt.kind {
fe::ContractStmt::ContractField { .. } => {
// Contract fields are evaluated in the next pass together with function bodies
// so that they can use other contract types that may only be defined after the
Expand All @@ -58,9 +58,9 @@ pub fn contract_def(
.module_scope()
.borrow_mut()
.add_type_def(
name.node,
name.kind,
Type::Contract(Contract {
name: name.node.to_owned(),
name: name.kind.to_owned(),
functions: contract_attributes.public_functions,
}),
);
Expand All @@ -77,15 +77,15 @@ pub fn contract_def(
pub fn contract_body(
contract_scope: Shared<ContractScope>,
context: Shared<Context>,
stmt: &Spanned<fe::ModuleStmt>,
stmt: &Node<fe::ModuleStmt>,
) -> Result<(), SemanticError> {
if let fe::ModuleStmt::ContractDef { body, .. } = &stmt.node {
if let fe::ModuleStmt::ContractDef { body, .. } = &stmt.kind {
for stmt in body.iter() {
if let fe::ContractStmt::ContractField { .. } = &stmt.node {
if let fe::ContractStmt::ContractField { .. } = &stmt.kind {
contract_field(Rc::clone(&contract_scope), stmt)?;
};

if let fe::ContractStmt::FuncDef { .. } = &stmt.node {
if let fe::ContractStmt::FuncDef { .. } = &stmt.kind {
functions::func_body(Rc::clone(&contract_scope), Rc::clone(&context), stmt)
.map_err(|error| error.with_context(stmt.span))?;
};
Expand All @@ -103,11 +103,11 @@ pub fn contract_body(

fn contract_field(
scope: Shared<ContractScope>,
stmt: &Spanned<fe::ContractStmt>,
stmt: &Node<fe::ContractStmt>,
) -> Result<(), SemanticError> {
if let fe::ContractStmt::ContractField { qual: _, name, typ } = &stmt.node {
if let fe::ContractStmt::ContractField { qual: _, name, typ } = &stmt.kind {
let typ = types::type_desc(Scope::Contract(Rc::clone(&scope)), typ)?;
return scope.borrow_mut().add_field(name.node, typ);
return scope.borrow_mut().add_field(name.kind, typ);
}

unreachable!()
Expand All @@ -116,10 +116,10 @@ fn contract_field(
fn event_def(
scope: Shared<ContractScope>,
context: Shared<Context>,
stmt: &Spanned<fe::ContractStmt>,
stmt: &Node<fe::ContractStmt>,
) -> Result<(), SemanticError> {
if let fe::ContractStmt::EventDef { name, fields } = &stmt.node {
let name = name.node;
if let fe::ContractStmt::EventDef { name, fields } = &stmt.kind {
let name = name.kind;

let (is_indexed_bools, fields): (Vec<bool>, Vec<(String, FixedSize)>) = fields
.iter()
Expand Down Expand Up @@ -159,13 +159,13 @@ fn event_def(

fn event_field(
scope: Shared<ContractScope>,
field: &Spanned<fe::EventField>,
field: &Node<fe::EventField>,
) -> Result<(bool, (String, FixedSize)), SemanticError> {
Ok((
field.node.qual.is_some(),
field.kind.qual.is_some(),
(
field.node.name.node.to_string(),
types::type_desc_fixed_size(Scope::Contract(scope), &field.node.typ)?,
field.kind.name.kind.to_string(),
types::type_desc_fixed_size(Scope::Contract(scope), &field.kind.typ)?,
),
))
}
6 changes: 3 additions & 3 deletions analyzer/src/traversal/declarations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ use crate::traversal::{
};
use crate::Context;
use fe_parser::ast as fe;
use fe_parser::span::Spanned;
use fe_parser::span::Node;
use std::rc::Rc;

/// Gather context information for var declarations and check for type errors.
pub fn var_decl(
scope: Shared<BlockScope>,
context: Shared<Context>,
stmt: &Spanned<fe::FuncStmt>,
stmt: &Node<fe::FuncStmt>,
) -> Result<(), SemanticError> {
if let fe::FuncStmt::VarDecl { target, typ, value } = &stmt.node {
if let fe::FuncStmt::VarDecl { target, typ, value } = &stmt.kind {
let name = expressions::expr_name_str(target)?;
let declared_type = types::type_desc_fixed_size(Scope::Block(Rc::clone(&scope)), typ)?;
if let Some(value) = value {
Expand Down
Loading

0 comments on commit 90111c3

Please sign in to comment.