add an example and get it building

This commit is contained in:
Niko Matsakis 2018-09-28 11:26:53 -04:00
parent cae379fd06
commit 798a8a418c
6 changed files with 73 additions and 9 deletions

View file

@ -0,0 +1,51 @@
use crate::compiler;
use salsa::{query_definition, query_prototype};
use std::sync::Arc;
pub trait ClassTableQueryContext: compiler::CompilerQueryContext {
query_prototype!(
/// Get the fields.
fn fields() for Fields
);
query_prototype!(
/// Get the list of all classes
fn all_classes() for AllClasses
);
query_prototype!(
/// Get the list of all fields
fn all_fields() for AllFields
);
}
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
pub struct DefId;
query_definition! {
pub AllClasses(_: &impl ClassTableQueryContext, (): ()) -> Arc<Vec<DefId>> {
Arc::new(vec![]) // dummy impl
}
}
query_definition! {
pub Fields(_: &impl ClassTableQueryContext, _class: DefId) -> Arc<Vec<DefId>> {
Arc::new(vec![]) // dummy impl
}
}
query_definition! {
pub AllFields(query: &impl ClassTableQueryContext, _class: DefId) -> Arc<Vec<DefId>> {
Arc::new(
query.all_classes()
.of(())
.iter()
.cloned()
.flat_map(|def_id| {
let fields = query.fields().of(def_id);
(0..fields.len()).map(move |i| fields[i].clone())
})
.collect()
)
}
}

View file

@ -0,0 +1,6 @@
pub trait CompilerQueryContext: salsa::BaseQueryContext {
fn interner(&self) -> &Interner;
}
#[derive(Clone)]
pub struct Interner;

View file

@ -0,0 +1,6 @@
mod class_table;
mod compiler;
fn main() {
println!("It builds.");
}

View file

@ -18,13 +18,13 @@ use std::hash::Hash;
/// out cycle stack errors and not for any real recovery; also, not
/// especially efficient.
#[derive(PartialEq, Eq)]
crate struct DynDescriptor {
pub struct DynDescriptor {
type_id: TypeId,
debug_string: String,
}
impl DynDescriptor {
crate fn from_key<QC, Q>(_query: &QC, key: &Q::Key) -> DynDescriptor
pub fn from_key<QC, Q>(_query: &QC, key: &Q::Key) -> DynDescriptor
where
QC: BaseQueryContext,
Q: Query<QC>,

View file

@ -108,7 +108,7 @@ where
///
/// Example:
///
/// ```
/// ```ignore
/// trait TypeckQueryContext {
/// query_prototype!(fn <method>() for <type>);
/// }
@ -120,18 +120,19 @@ macro_rules! query_prototype {
fn $method_name:ident() for $query_type:ty
) => {
$(#[$attr])*
fn $method_name(&self) -> $crate::query::QueryTable<'_, Self, $query_type>;
fn $method_name(&self) -> $crate::QueryTable<'_, Self, $query_type>;
}
}
/// Example:
///
/// ```
/// ```ignore
/// query_definition! {
/// QueryName(query: &impl TypeckQueryContext, key: DefId) -> Arc<Vec<DefId>> {
/// ...
/// ...
/// }
/// }
/// ```
#[macro_export]
macro_rules! query_definition {
(
@ -143,13 +144,13 @@ macro_rules! query_definition {
#[derive(Default, Debug)]
$v struct $name;
impl<QC> $crate::query::Query<QC> for $name
impl<QC> $crate::Query<QC> for $name
where
QC: $query_trait,
{
type Key = $key_ty;
type Value = $value_ty;
type Storage = $crate::query::storage::MemoizedStorage<QC, Self>;
type Storage = $crate::storage::MemoizedStorage<QC, Self>;
fn execute($query: &QC, $key: $key_ty) -> $value_ty {
$($body)*

View file

@ -17,7 +17,7 @@ use std::hash::Hash;
// contains a certain amount of boilerplate. This file aims to
// reduce that.
crate struct MemoizedStorage<QC, Q>
pub struct MemoizedStorage<QC, Q>
where
Q: Query<QC>,
QC: BaseQueryContext,