Skip to content

Commit

Permalink
metadata is now actually attached to things
Browse files Browse the repository at this point in the history
  • Loading branch information
EclecticGriffin committed Jan 17, 2025
1 parent f1833fb commit 85017b5
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 10 deletions.
6 changes: 4 additions & 2 deletions calyx-frontend/src/ast.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Abstract Syntax Tree for Calyx
use super::parser;
use crate::{Attributes, PortDef, Primitive};
use crate::{metadata::MetadataTable, Attributes, PortDef, Primitive};
use atty::Stream;
use calyx_utils::{CalyxResult, Error, GPosIdx, Id, PosString};
use std::{num::NonZeroU64, path::PathBuf};
Expand All @@ -14,8 +14,10 @@ pub struct NamespaceDef {
pub components: Vec<ComponentDef>,
/// Extern statements and any primitive declarations in them.
pub externs: Vec<(Option<PosString>, Vec<Primitive>)>,
/// Optional opaque metadata
/// Optional legacy opaque metadata. Newer metadata is in [NamespaceDef::file_info_table]
pub metadata: Option<String>,
/// Optional metadata mapping table
pub file_info_table: Option<MetadataTable>,
}

impl NamespaceDef {
Expand Down
27 changes: 24 additions & 3 deletions calyx-frontend/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1426,18 +1426,38 @@ impl CalyxParser {

// end new metadata

fn extra_info(
input: Node,
) -> ParseResult<(Option<String>, Option<MetadataTable>)> {
Ok(match_nodes!(input.into_children();
[metadata_legacy(l)] => (Some(l), None),
[metadata_table(t)] => (None, Some(t)),
[metadata_legacy(l), metadata_table(t)] => (Some(l), Some(t)),
[metadata_table(t), metadata_legacy(l)] => (Some(l), Some(t))
))
}

fn file(input: Node) -> ParseResult<ast::NamespaceDef> {
Ok(match_nodes!(
input.into_children();
// There really seems to be no straightforward way to resolve this
// duplication
[imports(imports), externs_and_comps(mixed), metadata_legacy(m), EOI(_)] => {
[imports(imports), externs_and_comps(mixed), extra_info(info), EOI(_)] => {
let (mut legacy_metadata, metadata) = info;
// remove empty metadata strings
if let Some(m) = &legacy_metadata {
if m.is_empty() {
legacy_metadata = None;
}
}

let mut namespace =
ast::NamespaceDef {
imports,
components: Vec::new(),
externs: Vec::new(),
metadata: if m != *"" { Some(m) } else { None }
metadata: legacy_metadata,
file_info_table: metadata
};
for m in mixed {
match m {
Expand All @@ -1461,7 +1481,8 @@ impl CalyxParser {
imports,
components: Vec::new(),
externs: Vec::new(),
metadata: None
metadata: None,
file_info_table: None
};
for m in mixed {
match m {
Expand Down
10 changes: 9 additions & 1 deletion calyx-frontend/src/syntax.pest
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ file = {
SOI
~ imports
~ externs_and_comps
~ metadata_legacy?
~ extra_info?
~ EOI
}

Expand Down Expand Up @@ -418,3 +418,11 @@ position_table = {
metadata_table = {
^"fileinfo" ~ "#{" ~ file_table ~ position_table ~ "}#"
}


extra_info = {
metadata_legacy
| metadata_table
| (metadata_legacy ~ metadata_table)
| (metadata_table ~ metadata_legacy)
}
7 changes: 5 additions & 2 deletions calyx-frontend/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::{
ast::{ComponentDef, NamespaceDef},
parser,
};
use crate::LibrarySignatures;
use crate::{metadata::MetadataTable, LibrarySignatures};
use calyx_utils::{CalyxResult, Error, WithPos};
use std::{
collections::HashSet,
Expand Down Expand Up @@ -48,8 +48,10 @@ pub struct Workspace {
pub lib: LibrarySignatures,
/// Original import statements present in the top-level file.
pub original_imports: Vec<String>,
/// Optional opaque metadata attached to the top-level file
/// Optional legacy opaque metadata attached to the top-level file. Newer metadata is stored in [Workspace::file_info_table]
pub metadata: Option<String>,
/// Optional metadata mapping table attached to the top-level file
pub file_info_table: Option<MetadataTable>,
}

impl Workspace {
Expand Down Expand Up @@ -281,6 +283,7 @@ impl Workspace {
// TODO (griffin): Probably not a great idea to clone the metadata
// string but it works for now
ws.metadata = ns.metadata.clone();
ws.file_info_table = ns.file_info_table.clone();

// Merge the initial namespace
let parent_canonical = parent_path.canonicalize().map_err(|err| {
Expand Down
6 changes: 4 additions & 2 deletions calyx-ir/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//! need to transform, lower, an emit a program.
//! Passes usually have transform/analyze the components in the IR.
use super::{Component, Id};
use calyx_frontend::LibrarySignatures;
use calyx_frontend::{metadata::MetadataTable, LibrarySignatures};

/// Configuration information for the backends.
#[derive(Default)]
Expand Down Expand Up @@ -32,8 +32,10 @@ pub struct Context {
/// Extra options provided to the command line.
/// Interpreted by individual passes
pub extra_opts: Vec<String>,
/// An optional opaque metadata string which is used by Cider
/// An optional legacy opaque metadata string
pub metadata: Option<String>,
/// An optional metadata mapping table
pub file_info_table: Option<MetadataTable>,
}

impl Context {
Expand Down
1 change: 1 addition & 0 deletions calyx-ir/src/from_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ pub fn ast_to_ir(mut workspace: Workspace) -> CalyxResult<Context> {
entrypoint,
extra_opts: vec![],
metadata: workspace.metadata,
file_info_table: workspace.file_info_table,
})
}

Expand Down

0 comments on commit 85017b5

Please sign in to comment.