deprecate resolved node trait

This commit is contained in:
collin 2020-10-11 12:56:01 -07:00
parent a5975eb5fd
commit a66cc3afda
7 changed files with 28 additions and 70 deletions

View File

@ -22,25 +22,3 @@ pub use self::errors::*;
pub mod types;
pub use self::types::*;
/// A resolved node in an abstract syntax tree (AST).
///
/// Resolved nodes can be any function, statement, expression, type, etc. in an AST.
/// Resolved nodes should not contain any illegal types.
/// Resolved nodes should not contain any implicit types.
pub trait ResolvedNode {
/// The expected error type if the type resolution fails.
type Error;
/// The unresolved AST node that is being resolved.
type UnresolvedNode;
///
/// Returns a resolved AST representation given an unresolved AST representation.
///
/// User-defined types are looked up using the given symbol table.
///
fn resolve(table: &mut SymbolTable, unresolved: Self::UnresolvedNode) -> Result<Self, Self::Error>
where
Self: std::marker::Sized;
}

View File

@ -63,7 +63,7 @@ impl ResolvedNode for CircuitType {
match member {
CircuitMember::CircuitVariable(is_mutable, variable_identifier, type_) => {
// Resolve the type of the circuit member variable.
let type_ = Type::from_circuit(
let type_ = Type::new_from_circuit(
table,
type_,
circuit_identifier.clone(),

View File

@ -40,27 +40,24 @@ pub struct FunctionType {
pub output: FunctionOutputType,
}
impl ResolvedNode for FunctionType {
type Error = TypeError;
type UnresolvedNode = Function;
impl FunctionType {
///
/// Return a new `FunctionType` from a given `Function` definition.
///
/// Performs a lookup in the given symbol table if the function definition contains
/// user-defined types.
///
fn resolve(table: &mut SymbolTable, unresolved: Self::UnresolvedNode) -> Result<Self, Self::Error> {
pub fn new(table: &mut SymbolTable, unresolved: Function) -> Result<Self, TypeError> {
let mut inputs_resolved = vec![];
// Type check function inputs
for input in unresolved.input {
let input = FunctionInputType::resolve(table, input)?;
let input = FunctionInputType::new(table, input)?;
inputs_resolved.push(input);
}
// Type check function output
let output = FunctionOutputType::resolve(table, (unresolved.output, unresolved.span))?;
let output = FunctionOutputType::new(table, unresolved.output, unresolved.span)?;
Ok(FunctionType {
identifier: unresolved.identifier,
@ -68,9 +65,7 @@ impl ResolvedNode for FunctionType {
output,
})
}
}
impl FunctionType {
///
/// Resolve a function definition and insert it into the given symbol table.
///
@ -79,7 +74,7 @@ impl FunctionType {
let function_identifier = unresolved_function.identifier.clone();
// Resolve the function definition into a function type.
let function = Self::resolve(table, unresolved_function)?;
let function = Self::new(table, unresolved_function)?;
// Insert (function_identifier -> function_type) as a (key -> value) pair in the symbol table.
table.insert_function(function_identifier, function);
@ -106,12 +101,12 @@ impl FunctionType {
// Type check function inputs.
for unresolved_input in unresolved_function.input {
let input = FunctionInputType::from_circuit(table, unresolved_input, circuit_name.clone())?;
let input = FunctionInputType::new_from_circuit(table, unresolved_input, circuit_name.clone())?;
inputs.push(input);
}
// Type check function output.
let output = FunctionOutputType::from_circuit(
let output = FunctionOutputType::new_from_circuit(
table,
circuit_name.clone(),
unresolved_function.output,

View File

@ -25,29 +25,24 @@ pub enum FunctionInputType {
Variable(FunctionInputVariableType),
}
impl ResolvedNode for FunctionInputType {
type Error = TypeError;
type UnresolvedNode = FunctionInput;
impl FunctionInputType {
///
/// Return a new `FunctionInputType` from a given `FunctionInput`.
///
/// Performs a lookup in the given symbol table if the function input contains
/// user-defined types.
///
fn resolve(table: &mut SymbolTable, unresolved: Self::UnresolvedNode) -> Result<Self, Self::Error> {
pub fn new(table: &mut SymbolTable, unresolved: FunctionInput) -> Result<Self, TypeError> {
Ok(match unresolved {
FunctionInput::InputKeyword(identifier) => FunctionInputType::InputKeyword(identifier),
FunctionInput::Variable(variable) => {
let variable_resolved = FunctionInputVariableType::resolve(table, variable)?;
let variable_resolved = FunctionInputVariableType::new(table, variable)?;
FunctionInputType::Variable(variable_resolved)
}
})
}
}
impl FunctionInputType {
///
/// Return the `Identifier` containing name and span information about the current function input.
///
@ -77,7 +72,7 @@ impl FunctionInputType {
/// If the type of the function input is the `Self` keyword, then the given circuit identifier
/// is used as the type.
///
pub fn from_circuit(
pub fn new_from_circuit(
table: &mut SymbolTable,
unresolved: FunctionInput,
circuit_name: Identifier,
@ -86,7 +81,7 @@ impl FunctionInputType {
FunctionInput::InputKeyword(identifier) => FunctionInputType::InputKeyword(identifier),
FunctionInput::Variable(unresolved_function_input) => {
let function_input =
FunctionInputVariableType::from_circuit(table, unresolved_function_input, circuit_name)?;
FunctionInputVariableType::new_from_circuit(table, unresolved_function_input, circuit_name)?;
FunctionInputType::Variable(function_input)
}

View File

@ -34,16 +34,13 @@ pub struct FunctionInputVariableType {
pub span: Span,
}
impl ResolvedNode for FunctionInputVariableType {
type Error = TypeError;
type UnresolvedNode = FunctionInputVariable;
impl FunctionInputVariableType {
///
/// Return a new `FunctionInputVariableType` from a given `FunctionInputVariable`.
///
/// Performs a lookup in the given symbol table if the type is user-defined.
///
fn resolve(table: &mut SymbolTable, unresolved: Self::UnresolvedNode) -> Result<Self, Self::Error> {
pub fn new(table: &mut SymbolTable, unresolved: FunctionInputVariable) -> Result<Self, TypeError> {
let type_ = Type::new(table, unresolved.type_, unresolved.span.clone())?;
let attributes = if unresolved.mutable {
vec![Attribute::Mutable]
@ -58,9 +55,7 @@ impl ResolvedNode for FunctionInputVariableType {
span: unresolved.span,
})
}
}
impl FunctionInputVariableType {
///
/// Return a new `FunctionInputVariableType` from a given `FunctionInputVariable`.
///
@ -69,12 +64,12 @@ impl FunctionInputVariableType {
/// If the type of the function return type is the `Self` keyword, then the given circuit
/// identifier is used as the type.
///
pub fn from_circuit(
pub fn new_from_circuit(
table: &mut SymbolTable,
unresolved_function_input: FunctionInputVariable,
circuit_name: Identifier,
) -> Result<Self, TypeError> {
let type_ = Type::from_circuit(
let type_ = Type::new_from_circuit(
table,
unresolved_function_input.type_,
circuit_name,

View File

@ -26,20 +26,17 @@ pub struct FunctionOutputType {
pub type_: Type,
}
impl ResolvedNode for FunctionOutputType {
type Error = TypeError;
/// (optional function output, span)
type UnresolvedNode = (Option<UnresolvedType>, Span);
impl FunctionOutputType {
///
/// Return a new `FunctionOutputType` from a given optional function return type and span.
///
/// Performs a lookup in the given symbol table if the return type is user-defined.
///
fn resolve(table: &mut SymbolTable, unresolved: Self::UnresolvedNode) -> Result<Self, TypeError> {
let function_output = unresolved.0;
let span = unresolved.1;
pub(crate) fn new(
table: &mut SymbolTable,
function_output: Option<UnresolvedType>,
span: Span,
) -> Result<Self, TypeError> {
let type_ = match function_output {
None => Type::Tuple(vec![]), // functions with no return value return an empty tuple
Some(type_) => Type::new(table, type_, span)?,
@ -47,9 +44,7 @@ impl ResolvedNode for FunctionOutputType {
Ok(FunctionOutputType { type_ })
}
}
impl FunctionOutputType {
///
/// Return a new `FunctionOutputType` from a given optional function return type and span.
///
@ -58,7 +53,7 @@ impl FunctionOutputType {
/// If the type of the function return type is the `Self` keyword, then the given circuit
/// identifier is used as the type.
///
pub fn from_circuit(
pub fn new_from_circuit(
table: &mut SymbolTable,
circuit_name: Identifier,
unresolved: Option<UnresolvedType>,
@ -66,7 +61,7 @@ impl FunctionOutputType {
) -> Result<Self, TypeError> {
let output_type = match unresolved {
None => Type::Tuple(vec![]),
Some(type_) => Type::from_circuit(table, type_, circuit_name, span)?,
Some(type_) => Type::new_from_circuit(table, type_, circuit_name, span)?,
};
Ok(FunctionOutputType { type_: output_type })

View File

@ -85,7 +85,7 @@ impl Type {
///
/// If this type is SelfType, return the circuit's type.
///
pub fn from_circuit(
pub fn new_from_circuit(
table: &mut SymbolTable,
type_: UnresolvedType,
circuit_name: Identifier,
@ -93,13 +93,13 @@ impl Type {
) -> Result<Self, TypeError> {
Ok(match type_ {
UnresolvedType::Array(type_, dimensions) => {
let array_type = Type::from_circuit(table, *type_, circuit_name, span)?;
let array_type = Type::new_from_circuit(table, *type_, circuit_name, span)?;
Type::Array(Box::new(array_type), dimensions)
}
UnresolvedType::Tuple(types) => {
let tuple_types = types
.into_iter()
.map(|type_| Type::from_circuit(table, type_, circuit_name.clone(), span.clone()))
.map(|type_| Type::new_from_circuit(table, type_, circuit_name.clone(), span.clone()))
.collect::<Result<Vec<_>, _>>()?;
Type::Tuple(tuple_types)