Skip to content

Commit

Permalink
Break up query module
Browse files Browse the repository at this point in the history
  • Loading branch information
udoprog committed May 13, 2023
1 parent 921051a commit a7283ad
Show file tree
Hide file tree
Showing 14 changed files with 1,726 additions and 1,740 deletions.
34 changes: 22 additions & 12 deletions crates/rune-macros/src/instrument.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,30 @@ use quote::quote;

/// An internal call to the macro.
pub struct Expander {
f: syn::ItemFn,
attrs: Vec<syn::Attribute>,
vis: syn::Visibility,
sig: syn::Signature,
remaining: TokenStream,
}

impl syn::parse::Parse for Expander {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
let f: syn::ItemFn = input.parse()?;

Ok(Self { f })
let attrs = input.call(syn::Attribute::parse_outer)?;
let vis: syn::Visibility = input.parse()?;
let sig: syn::Signature = input.parse()?;
let remaining = input.parse()?;
Ok(Self {
attrs,
vis,
sig,
remaining,
})
}
}

impl Expander {
pub fn expand(self) -> Result<TokenStream, Vec<syn::Error>> {
let f = self.f;

let mut it = f.sig.inputs.iter();
let mut it = self.sig.inputs.iter();

let first = match it.next() {
Some(syn::FnArg::Typed(ty)) => match &*ty.pat {
Expand All @@ -36,7 +44,7 @@ impl Expander {
_ => None,
};

let ident = &f.sig.ident;
let ident = &self.sig.ident;

let log = match (first, second) {
(Some(a), Some(b)) => {
Expand All @@ -54,14 +62,16 @@ impl Expander {
_ => None,
};

let vis = &f.vis;
let stmts = &f.block.stmts;
let sig = &f.sig;
let attrs = &self.attrs;
let vis = &self.vis;
let sig = &self.sig;
let remaining = &self.remaining;

Ok(quote! {
#(#attrs)*
#vis #sig {
#log
{ #(#stmts)* }
#remaining
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion crates/rune-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ pub fn __internal_impl_any(input: proc_macro::TokenStream) -> proc_macro::TokenS

#[proc_macro_attribute]
#[doc(hidden)]
pub fn __instrument_ast(
pub fn instrument(
_attr: proc_macro::TokenStream,
item: proc_macro::TokenStream,
) -> proc_macro::TokenStream {
Expand Down
33 changes: 14 additions & 19 deletions crates/rune/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub(crate) use self::assembly::{Assembly, AssemblyInst};

pub(crate) mod attrs;

mod error;
pub(crate) mod error;
pub(crate) use self::error::{
CompileErrorKind, HirErrorKind, IrErrorKind, ParseErrorKind, QueryErrorKind, ResolveErrorKind,
};
Expand Down Expand Up @@ -106,23 +106,21 @@ pub(crate) fn compile(
let mut storage = Storage::default();
let mut inner = Default::default();

// The worker queue.
let mut worker = Worker::new(
context,
let q = Query::new(
unit,
prelude,
&mut consts,
&mut storage,
sources,
pool,
options,
unit,
prelude,
diagnostics,
visitor,
source_loader,
&gen,
&mut inner,
);

// The worker queue.
let mut worker = Worker::new(context, options, diagnostics, source_loader, q);

// Queue up the initial sources to be loaded.
for source_id in worker.q.sources.source_ids() {
let mod_item = match worker.q.insert_root_mod(source_id, Span::empty()) {
Expand Down Expand Up @@ -270,14 +268,11 @@ impl CompileBuildEntry<'_> {

use self::v1::assemble;

let args = format_fn_args(
self.q.sources,
location,
f.function.ast.args.iter().map(|(a, _)| a),
)?;
let args =
format_fn_args(self.q.sources, location, f.ast.args.iter().map(|(a, _)| a))?;

let span = f.function.ast.span();
let count = f.function.ast.args.len();
let span = f.ast.span();
let count = f.ast.args.len();

let mut c = self.compiler1(location, span, &mut asm);
let meta = c.lookup_meta(
Expand All @@ -292,13 +287,13 @@ impl CompileBuildEntry<'_> {

let arena = hir::Arena::new();
let ctx = hir::lowering::Ctx::new(&arena, c.q.borrow());
let hir = hir::lowering::item_fn(&ctx, &f.function.ast)?;
let hir = hir::lowering::item_fn(&ctx, &f.ast)?;
assemble::fn_from_item_fn(&hir, &mut c, true)?;

if used.is_unused() {
c.diagnostics.not_used(location.source_id, span, None);
} else {
let name = f.function.ast.name.resolve(resolve_context!(self.q))?;
let name = f.ast.name.resolve(resolve_context!(self.q))?;

self.q.unit.new_instance_function(
location,
Expand All @@ -307,7 +302,7 @@ impl CompileBuildEntry<'_> {
name,
count,
asm,
f.function.call,
f.call,
args,
)?;
}
Expand Down
27 changes: 17 additions & 10 deletions crates/rune/src/compile/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use crate::parse::{Expectation, Id, IntoExpectation, LexerMode};
use crate::runtime::debug::DebugSignature;
use crate::runtime::{AccessError, Label, TypeInfo, TypeOf};
use crate::shared::scopes::MissingLocal;
use crate::shared::MissingLastId;
use crate::{Hash, SourceId};

/// An error raised by the compiler.
Expand Down Expand Up @@ -205,13 +206,17 @@ pub(crate) enum CompileErrorKind {
#[error("{0}")]
QueryError(#[from] QueryErrorKind),
#[error("{0}")]
MetaConflict(#[from] MetaConflict),
#[error("{0}")]
ResolveError(#[from] ResolveErrorKind),
#[error("{0}")]
ParseError(#[from] ParseErrorKind),
#[error("{0}")]
AccessError(#[from] AccessError),
#[error("{0}")]
HirError(#[from] HirErrorKind),
#[error("{0}")]
MissingLastId(#[from] MissingLastId),
#[error("Failed to load `{path}`: {error}")]
FileError {
path: PathBuf,
Expand Down Expand Up @@ -442,16 +447,6 @@ pub(crate) enum QueryErrorKind {
ImportRecursionLimit { count: usize, path: Vec<ImportStep> },
#[error("Missing last use component")]
LastUseComponent,
/// Tried to add an item that already exists.
#[error("Can't insert item `{current}` ({parameters}) because conflicting meta `{existing}` already exists")]
MetaConflict {
/// The meta we tried to insert.
current: MetaInfo,
/// The existing item.
existing: MetaInfo,
/// Parameters hash.
parameters: Hash,
},
#[error("Tried to insert variant runtime type information, but conflicted with hash `{hash}`")]
VariantRttiConflict { hash: Hash },
#[error("Tried to insert runtime type information, but conflicted with hash `{hash}`")]
Expand Down Expand Up @@ -631,3 +626,15 @@ pub struct ImportStep {
/// The item being imported.
pub item: ItemBuf,
}

#[derive(Debug, Error)]
/// Tried to add an item that already exists.
#[error("Can't insert item `{current}` ({parameters}) because conflicting meta `{existing}` already exists")]
pub(crate) struct MetaConflict {
/// The meta we tried to insert.
pub(crate) current: MetaInfo,
/// The existing item.
pub(crate) existing: MetaInfo,
/// Parameters hash.
pub(crate) parameters: Hash,
}
2 changes: 1 addition & 1 deletion crates/rune/src/compile/ir/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::query::Query;
use crate::runtime::{Bytes, Shared};
use crate::SourceId;

use rune_macros::__instrument_ast as instrument;
use rune_macros::instrument;

/// A c that compiles AST into Rune IR.
pub(crate) struct IrCompiler<'a> {
Expand Down
28 changes: 25 additions & 3 deletions crates/rune/src/compile/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::compile::{
Location, Options, QueryErrorKind, WithSpan,
};
use crate::hir;
use crate::query::{Named, Query, QueryConstFn, Used};
use crate::query::{ConstFn, Named, Query, Used};
use crate::runtime::{ConstValue, Inst};
use crate::{Context, Diagnostics, Hash, SourceId};

Expand Down Expand Up @@ -219,7 +219,29 @@ impl<'a> Assembler<'a> {
ContextMatch::Context(meta, parameters) => (meta, parameters),
};

let meta = self.q.insert_context_meta(span, meta, parameters)?;
let Some(item) = &meta.item else {
return Err(compile::Error::new(span,
QueryErrorKind::MissingItem {
hash: meta.hash,
}));
};

let meta = meta::Meta {
context: true,
hash: meta.hash,
item_meta: ItemMeta {
id: Default::default(),
location: Default::default(),
item: self.q.pool.alloc_item(item),
visibility: Default::default(),
module: Default::default(),
},
kind: meta.kind.clone(),
source: None,
parameters,
};

self.q.insert_meta(meta.clone()).with_span(span)?;

tracing::trace!("Found in context: {:?}", meta);

Expand Down Expand Up @@ -322,7 +344,7 @@ impl<'a> Assembler<'a> {
span: Span,
meta: &meta::Meta,
from: &ItemMeta,
query_const_fn: &QueryConstFn,
query_const_fn: &ConstFn,
args: &[hir::Expr<'_>],
) -> compile::Result<ConstValue> {
if query_const_fn.ir_fn.args.len() != args.len() {
Expand Down
2 changes: 1 addition & 1 deletion crates/rune/src/compile/v1/assemble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use crate::runtime::{
};
use crate::Hash;

use rune_macros::__instrument_ast as instrument;
use rune_macros::instrument;

/// `self` variable.
const SELF: &str = "self";
Expand Down
Loading

0 comments on commit a7283ad

Please sign in to comment.