Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
10 changes: 10 additions & 0 deletions crates/ast/src/common/location.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ impl Location {
pub fn is_dynamic(&self) -> bool {
self.program == sym::__dynamic__
}

/// The portion of the path *above* the item, i.e. the module path the item lives in.
/// For program-block items and library top-level items the path has length 1 and
/// this returns an empty slice; for module items it returns the module segments.
pub fn module_path(&self) -> &[Symbol] {
match self.path.split_last() {
Some((_, module)) => module,
None => &[],
}
}
}

impl Display for Location {
Expand Down
9 changes: 9 additions & 0 deletions crates/ast/src/composite/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ use snarkvm::{
/// The fields are named so `struct Foo(u8, u16)` is not allowed.
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Composite {
/// Whether the `export` keyword was written on this composite. `None` when the
/// concept doesn't apply (records, program-block composites, and structs
/// imported from another unit, all always reachable).
pub is_exported: Option<bool>,
/// The name of the type in the type system in this module.
pub identifier: Identifier,
/// The composite's const parameters.
Expand Down Expand Up @@ -89,6 +93,7 @@ impl Composite {
id: Default::default(),
}));
Self {
is_exported: None,
identifier: Identifier::from(input.name()),
const_parameters: Vec::new(),
members,
Expand All @@ -100,6 +105,7 @@ impl Composite {

pub fn from_snarkvm<N: Network>(input: &StructType<N>, program: ProgramId) -> Self {
Self {
is_exported: None,
identifier: Identifier::from(input.name()),
const_parameters: Vec::new(),
members: input
Expand All @@ -122,6 +128,9 @@ impl Composite {

impl fmt::Display for Composite {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.is_exported == Some(true) {
f.write_str("export ")?;
}
f.write_str(if self.is_record { "record" } else { "struct" })?;
write!(f, " {}", self.identifier)?;
if !self.const_parameters.is_empty() {
Expand Down
25 changes: 24 additions & 1 deletion crates/ast/src/functions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ use std::fmt;
/// A function definition.
#[derive(Clone, Default, Serialize, Deserialize)]
pub struct Function {
/// Whether the `export` keyword was written on this function. `None` means the
/// visibility concept does not apply (e.g., program-block functions and
/// stubs, which are always reachable as part of the program's public
/// interface, and compiler-synthesized helpers).
pub is_exported: Option<bool>,
/// Annotations on the function.
pub annotations: Vec<Annotation>,
/// Is this function a transition, inlined, or a regular function?.
Expand Down Expand Up @@ -76,6 +81,7 @@ impl Function {
/// Initialize a new function.
#[allow(clippy::too_many_arguments)]
pub fn new(
is_exported: Option<bool>,
annotations: Vec<Annotation>,
variant: Variant,
identifier: Identifier,
Expand All @@ -92,7 +98,19 @@ impl Function {
_ => Type::Tuple(TupleType::new(output.iter().map(|o| o.type_.clone()).collect())),
};

Function { annotations, variant, identifier, const_parameters, input, output, output_type, block, span, id }
Function {
is_exported,
annotations,
variant,
identifier,
const_parameters,
input,
output,
output_type,
block,
span,
id,
}
}

/// Returns function name.
Expand All @@ -109,6 +127,7 @@ impl Function {
impl From<FunctionStub> for Function {
fn from(function: FunctionStub) -> Self {
Self {
is_exported: None,
annotations: function.annotations,
variant: function.variant,
identifier: function.identifier,
Expand All @@ -135,6 +154,10 @@ impl fmt::Display for Function {
writeln!(f, "{annotation}")?;
}

if self.is_exported == Some(true) {
write!(f, "export ")?;
}

match self.variant {
Variant::FinalFn => write!(f, "final fn ")?,
Variant::Fn => write!(f, "fn ")?,
Expand Down
7 changes: 7 additions & 0 deletions crates/ast/src/interface/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ mod prototypes;
/// An interface definition.
#[derive(Clone, Default, Serialize, Deserialize)]
pub struct Interface {
/// Whether the `export` keyword was written on this interface. `None` when
/// visibility doesn't apply (program-block interfaces, which are always
/// reachable).
pub is_exported: Option<bool>,
/// The interface identifier, e.g., `Foo` in `interface Foo { ... }`.
pub identifier: Identifier,
/// The interfaces this interface inherits from (supports multiple inheritance)
Expand Down Expand Up @@ -77,6 +81,9 @@ impl fmt::Debug for Interface {

impl fmt::Display for Interface {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.is_exported == Some(true) {
write!(f, "export ")?;
}
writeln!(
f,
"interface {}{} {{",
Expand Down
2 changes: 2 additions & 0 deletions crates/ast/src/passes/reconstructor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,7 @@ pub trait UnitReconstructor: AstReconstructor {

fn reconstruct_interface(&mut self, input: Interface) -> Interface {
Interface {
is_exported: input.is_exported,
identifier: input.identifier,
parents: input.parents.into_iter().map(|(s, t)| (s, self.reconstruct_type(t).0)).collect(),
span: input.span,
Expand Down Expand Up @@ -769,6 +770,7 @@ pub trait UnitReconstructor: AstReconstructor {

fn reconstruct_function(&mut self, input: Function) -> Function {
Function {
is_exported: input.is_exported,
annotations: input.annotations,
variant: input.variant,
identifier: input.identifier,
Expand Down
6 changes: 6 additions & 0 deletions crates/ast/src/statement/const_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ use std::fmt;
/// A constant declaration statement.
#[derive(Clone, Default, PartialEq, Eq, Serialize, Deserialize, Debug)]
pub struct ConstDeclaration {
/// Whether the `export` keyword was written on this const. `None` when
/// visibility doesn't apply (statement-level consts).
pub is_exported: Option<bool>,
/// The place to assign to. As opposed to `DefinitionStatement`, this can only be an identifier
pub place: Identifier,
/// The type of the binding, if specified, or inferred otherwise.
Expand All @@ -37,6 +40,9 @@ pub struct ConstDeclaration {

impl fmt::Display for ConstDeclaration {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if self.is_exported == Some(true) {
write!(f, "export ")?;
}
write!(f, "const {}: {} = {}", self.place, self.type_, self.value)
}
}
Expand Down
12 changes: 10 additions & 2 deletions crates/fmt/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ fn format_function(node: &SyntaxNode, out: &mut Output) {
SyntaxElement::Token(tok) => {
let k = tok.kind();
match k {
KW_FINAL | KW_VIEW | KW_FN | KW_SCRIPT => {
KW_EXPORT | KW_FINAL | KW_VIEW | KW_FN | KW_SCRIPT => {
out.write(tok.text());
out.space();
}
Expand Down Expand Up @@ -607,7 +607,7 @@ fn format_composite(node: &SyntaxNode, out: &mut Output) {
SyntaxElement::Token(tok) => {
let k = tok.kind();
match k {
KW_STRUCT | KW_RECORD => {
KW_EXPORT | KW_STRUCT | KW_RECORD => {
out.write(tok.text());
out.space();
}
Expand Down Expand Up @@ -749,6 +749,10 @@ fn format_interface(node: &SyntaxNode, out: &mut Output) {
SyntaxElement::Token(tok) => {
let k = tok.kind();
match k {
KW_EXPORT => {
out.write("export");
out.space();
}
KW_INTERFACE => {
out.write("interface");
out.space();
Expand Down Expand Up @@ -1208,6 +1212,10 @@ fn format_global_const(node: &SyntaxNode, out: &mut Output) {
SyntaxElement::Token(tok) => {
let k = tok.kind();
match k {
KW_EXPORT => {
out.write("export");
out.space();
}
KW_CONST => {
out.write("const");
out.space();
Expand Down
Loading
Loading