Skip to content

Commit

Permalink
Merge pull request ProvableHQ#27902 from AleoHQ/fix/external-struct
Browse files Browse the repository at this point in the history
[Fix] External struct equality
  • Loading branch information
d0cd authored Apr 18, 2024
2 parents 57b612b + 88b57be commit 95ee4a4
Show file tree
Hide file tree
Showing 733 changed files with 7,910 additions and 7,852 deletions.
50 changes: 0 additions & 50 deletions compiler/ast/src/functions/external.rs

This file was deleted.

4 changes: 2 additions & 2 deletions compiler/ast/src/functions/finalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ impl Finalize {
) -> Self {
let output_type = match output.len() {
0 => Type::Unit,
1 => output[0].type_(),
_ => Type::Tuple(TupleType::new(output.iter().map(|output| output.type_()).collect())),
1 => output[0].type_.clone(),
_ => Type::Tuple(TupleType::new(output.iter().map(|output| output.type_.clone()).collect())),
};

Self { identifier, input, output, output_type, block, span, id }
Expand Down
98 changes: 17 additions & 81 deletions compiler/ast/src/functions/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,91 +14,15 @@
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.

use crate::{External, Identifier, Mode, Node, NodeID, Type};
use crate::{Identifier, Mode, Node, NodeID, Type};
use leo_span::Span;

use serde::{Deserialize, Serialize};
use std::fmt;

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum Input {
Internal(FunctionInput),
External(External),
}

impl fmt::Display for Input {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use Input::*;
match self {
Internal(input) => input.fmt(f),
External(input) => input.fmt(f),
}
}
}

impl Input {
pub fn type_(&self) -> Type {
use Input::*;
match self {
Internal(input) => input.type_.clone(),
External(input) => input.type_(),
}
}

pub fn identifier(&self) -> Identifier {
use Input::*;
match self {
Internal(input) => input.identifier,
External(input) => input.identifier,
}
}

pub fn mode(&self) -> Mode {
use Input::*;
match self {
Internal(input) => input.mode,
External(_) => Mode::None,
}
}
}

impl Node for Input {
fn span(&self) -> Span {
use Input::*;
match self {
Internal(input) => input.span(),
External(input) => input.span(),
}
}

fn set_span(&mut self, span: Span) {
use Input::*;
match self {
Internal(input) => input.set_span(span),
External(input) => input.set_span(span),
}
}

fn id(&self) -> usize {
use Input::*;
match self {
Internal(input) => input.id(),
External(input) => input.id(),
}
}

fn set_id(&mut self, id: usize) {
use Input::*;
match self {
Internal(input) => input.set_id(id),
External(input) => input.set_id(id),
}
}
}

/// A function parameter.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct FunctionInput {
pub struct Input {
/// The name the parameter is accessible as in the function's body.
pub identifier: Identifier,
/// The mode of the function parameter.
Expand All @@ -111,16 +35,28 @@ pub struct FunctionInput {
pub id: NodeID,
}

impl FunctionInput {
impl Input {
fn format(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} {}: {}", self.mode, self.identifier, self.type_)
}

pub fn identifier(&self) -> &Identifier {
&self.identifier
}

pub fn mode(&self) -> Mode {
self.mode
}

pub fn type_(&self) -> &Type {
&self.type_
}
}

impl fmt::Display for FunctionInput {
impl fmt::Display for Input {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.format(f)
}
}

crate::simple_node_impl!(FunctionInput);
crate::simple_node_impl!(Input);
13 changes: 2 additions & 11 deletions compiler/ast/src/functions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ pub use core_function::*;
pub mod variant;
pub use variant::*;

pub mod external;
pub use external::*;

pub mod finalize;
pub use finalize::*;

Expand Down Expand Up @@ -91,16 +88,10 @@ impl Function {
span: Span,
id: NodeID,
) -> Self {
// Determine the output type of the function
let get_output_type = |output: &Output| match &output {
Output::Internal(output) => output.type_.clone(),
Output::External(output) => output.type_(),
};

let output_type = match output.len() {
0 => Type::Unit,
1 => get_output_type(&output[0]),
_ => Type::Tuple(TupleType::new(output.iter().map(get_output_type).collect())),
1 => output[0].type_.clone(),
_ => Type::Tuple(TupleType::new(output.iter().map(|o| o.type_.clone()).collect())),
};

Function { annotations, variant, identifier, input, output, output_type, block, finalize, span, id }
Expand Down
84 changes: 14 additions & 70 deletions compiler/ast/src/functions/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,81 +14,15 @@
// You should have received a copy of the GNU General Public License
// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.

use crate::{External, Mode, Node, NodeID, Type};
use crate::{Mode, Node, NodeID, Type};
use leo_span::Span;

use serde::{Deserialize, Serialize};
use std::fmt;

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum Output {
Internal(FunctionOutput),
External(External),
}

impl Output {
pub fn type_(&self) -> Type {
match self {
Output::Internal(output) => output.type_.clone(),
Output::External(output) => output.type_(),
}
}

pub fn mode(&self) -> Mode {
match self {
Output::Internal(output) => output.mode,
Output::External(_) => Mode::None,
}
}
}

impl fmt::Display for Output {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
use Output::*;
match self {
Internal(output) => output.fmt(f),
External(output) => output.fmt(f),
}
}
}

impl Node for Output {
fn span(&self) -> Span {
use Output::*;
match self {
Internal(output) => output.span(),
External(output) => output.span(),
}
}

fn set_span(&mut self, span: Span) {
use Output::*;
match self {
Internal(output) => output.set_span(span),
External(output) => output.set_span(span),
}
}

fn id(&self) -> NodeID {
use Output::*;
match self {
Internal(output) => output.id(),
External(output) => output.id(),
}
}

fn set_id(&mut self, id: NodeID) {
use Output::*;
match self {
Internal(output) => output.set_id(id),
External(output) => output.set_id(id),
}
}
}

/// A function output.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct FunctionOutput {
pub struct Output {
/// The mode of the function output.
pub mode: Mode,
/// The type of the function output.
Expand All @@ -99,10 +33,20 @@ pub struct FunctionOutput {
pub id: NodeID,
}

impl fmt::Display for FunctionOutput {
impl Output {
pub fn type_(&self) -> &Type {
&self.type_
}

pub fn mode(&self) -> Mode {
self.mode
}
}

impl fmt::Display for Output {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} {}", self.mode, self.type_)
}
}

crate::simple_node_impl!(FunctionOutput);
crate::simple_node_impl!(Output);
8 changes: 4 additions & 4 deletions compiler/ast/src/mapping/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

use crate::{Identifier, Node, NodeID, Type};

use leo_span::{Span, Symbol};
use leo_span::Span;

use serde::{Deserialize, Serialize};
use snarkvm::prelude::{Mapping as MappingCore, Network};
Expand All @@ -38,11 +38,11 @@ pub struct Mapping {
}

impl Mapping {
pub fn from_snarkvm<N: Network>(mapping: &MappingCore<N>, program: Symbol) -> Self {
pub fn from_snarkvm<N: Network>(mapping: &MappingCore<N>) -> Self {
Self {
identifier: Identifier::from(mapping.name()),
key_type: Type::from_snarkvm(mapping.key().plaintext_type(), program),
value_type: Type::from_snarkvm(mapping.value().plaintext_type(), program),
key_type: Type::from_snarkvm(mapping.key().plaintext_type(), None),
value_type: Type::from_snarkvm(mapping.value().plaintext_type(), None),
span: Default::default(),
id: Default::default(),
}
Expand Down
12 changes: 6 additions & 6 deletions compiler/ast/src/struct/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ impl Composite {
mode: if input.owner().is_public() { Mode::Public } else { Mode::Private },
identifier: Identifier::from(id),
type_: match entry {
Public(t) => Type::from_snarkvm(t, external_program),
Private(t) => Type::from_snarkvm(t, external_program),
Constant(t) => Type::from_snarkvm(t, external_program),
Public(t) => Type::from_snarkvm(t, None),
Private(t) => Type::from_snarkvm(t, None),
Constant(t) => Type::from_snarkvm(t, None),
},
span: Default::default(),
id: Default::default(),
Expand All @@ -104,7 +104,7 @@ impl Composite {
}
}

pub fn from_snarkvm<N: Network>(input: &StructType<N>, program: Symbol) -> Self {
pub fn from_snarkvm<N: Network>(input: &StructType<N>) -> Self {
Self {
identifier: Identifier::from(input.name()),
members: input
Expand All @@ -113,12 +113,12 @@ impl Composite {
.map(|(id, type_)| Member {
mode: Mode::None,
identifier: Identifier::from(id),
type_: Type::from_snarkvm(type_, program),
type_: Type::from_snarkvm(type_, None),
span: Default::default(),
id: Default::default(),
})
.collect(),
external: Some(program),
external: None,
is_record: false,
span: Default::default(),
id: Default::default(),
Expand Down
Loading

0 comments on commit 95ee4a4

Please sign in to comment.