From a66cc3afdaa574e7d5308caa01017deef77f1bfb Mon Sep 17 00:00:00 2001 From: collin Date: Sun, 11 Oct 2020 12:56:01 -0700 Subject: [PATCH] deprecate resolved node trait --- types/src/lib.rs | 22 ------------------- types/src/types/circuits/circuit.rs | 2 +- types/src/types/functions/function.rs | 19 ++++++---------- types/src/types/functions/function_input.rs | 15 +++++-------- .../functions/function_input_variable.rs | 13 ++++------- types/src/types/functions/function_output.rs | 21 +++++++----------- types/src/types/type_.rs | 6 ++--- 7 files changed, 28 insertions(+), 70 deletions(-) diff --git a/types/src/lib.rs b/types/src/lib.rs index 28ee6ddf03..2166af9a94 100644 --- a/types/src/lib.rs +++ b/types/src/lib.rs @@ -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 - where - Self: std::marker::Sized; -} diff --git a/types/src/types/circuits/circuit.rs b/types/src/types/circuits/circuit.rs index 8d1df94a91..f20f9dc053 100644 --- a/types/src/types/circuits/circuit.rs +++ b/types/src/types/circuits/circuit.rs @@ -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(), diff --git a/types/src/types/functions/function.rs b/types/src/types/functions/function.rs index 3a3d98475a..4f2657baf8 100644 --- a/types/src/types/functions/function.rs +++ b/types/src/types/functions/function.rs @@ -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 { + pub fn new(table: &mut SymbolTable, unresolved: Function) -> Result { 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, diff --git a/types/src/types/functions/function_input.rs b/types/src/types/functions/function_input.rs index 74df8ede4c..08657d6f06 100644 --- a/types/src/types/functions/function_input.rs +++ b/types/src/types/functions/function_input.rs @@ -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 { + pub fn new(table: &mut SymbolTable, unresolved: FunctionInput) -> Result { 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) } diff --git a/types/src/types/functions/function_input_variable.rs b/types/src/types/functions/function_input_variable.rs index f2f0e9062a..0d02e8b329 100644 --- a/types/src/types/functions/function_input_variable.rs +++ b/types/src/types/functions/function_input_variable.rs @@ -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 { + pub fn new(table: &mut SymbolTable, unresolved: FunctionInputVariable) -> Result { 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 { - let type_ = Type::from_circuit( + let type_ = Type::new_from_circuit( table, unresolved_function_input.type_, circuit_name, diff --git a/types/src/types/functions/function_output.rs b/types/src/types/functions/function_output.rs index 4ed18f9b45..3d06cf69ed 100644 --- a/types/src/types/functions/function_output.rs +++ b/types/src/types/functions/function_output.rs @@ -26,20 +26,17 @@ pub struct FunctionOutputType { pub type_: Type, } -impl ResolvedNode for FunctionOutputType { - type Error = TypeError; - /// (optional function output, span) - type UnresolvedNode = (Option, 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 { - let function_output = unresolved.0; - let span = unresolved.1; - + pub(crate) fn new( + table: &mut SymbolTable, + function_output: Option, + span: Span, + ) -> Result { 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, @@ -66,7 +61,7 @@ impl FunctionOutputType { ) -> Result { 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 }) diff --git a/types/src/types/type_.rs b/types/src/types/type_.rs index 335a1eea0a..788713644b 100644 --- a/types/src/types/type_.rs +++ b/types/src/types/type_.rs @@ -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 { 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::, _>>()?; Type::Tuple(tuple_types)