Skip to content

Commit

Permalink
Remove another type parameter from assembler::session::CompositeSession
Browse files Browse the repository at this point in the history
  • Loading branch information
ytausky committed Oct 8, 2020
1 parent ea762a4 commit 72429a5
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 108 deletions.
25 changes: 15 additions & 10 deletions src/assembler/session/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,23 @@ impl<S> ObjectBuilder<S> {
}
}

impl<C, R, I, D, L> Backend<R::Span> for CompositeSession<C, R, I, D, L>
impl<C, R, I, D> Backend<R::Span> for CompositeSession<C, R, I, D>
where
R: SpanSystem<BufId>,
I: StringSource,
Self: MacroSource + Diagnostics<R::Span>,
Self: Log<Self::SymbolId, <Self as MacroSource>::MacroId, I::StringRef, R::Span, R::Stripped>,
for<'a> DiagnosticsContext<'a, C, R, D>: Diagnostics<R::Span>,
{
fn define_symbol(
&mut self,
name: Self::SymbolId,
span: R::Span,
_span: R::Span,
expr: Expr<Self::SymbolId, R::Span>,
) {
self.log(|| Event::DefineSymbol {
#[cfg(test)]
self.log_event(Event::DefineSymbol {
name,
span,
span: _span,
expr: expr.clone(),
});

Expand All @@ -80,9 +80,11 @@ where
}

fn emit_fragment(&mut self, fragment: Fragment<Expr<Self::SymbolId, R::Span>>) {
self.log(|| Event::EmitFragment {
#[cfg(test)]
self.log_event(Event::EmitFragment {
fragment: fragment.clone(),
});

self.builder.push(fragment)
}

Expand All @@ -104,7 +106,9 @@ where
}

fn set_origin(&mut self, addr: Expr<Self::SymbolId, R::Span>) {
self.log(|| Event::SetOrigin { addr: addr.clone() });
#[cfg(test)]
self.log_event(Event::SetOrigin { addr: addr.clone() });

match self.builder.state.take().unwrap() {
BuilderState::SectionPrelude(index) => {
self.builder.object.content.sections[index].constraints.addr = Some(addr);
Expand All @@ -114,16 +118,17 @@ where
}
}

fn start_section(&mut self, name: SymbolId, span: R::Span) {
self.log(|| Event::StartSection { name, span });
fn start_section(&mut self, name: SymbolId, _span: R::Span) {
#[cfg(test)]
self.log_event(Event::StartSection { name, span: _span });

let index = self.builder.object.content.sections.len();
self.builder.state = Some(BuilderState::SectionPrelude(index));
self.builder.add_section(Some(name.content().unwrap()))
}
}

impl<C, R, I, D, L> AllocSymbol<R::Span> for CompositeSession<C, R, I, D, L>
impl<C, R, I, D> AllocSymbol<R::Span> for CompositeSession<C, R, I, D>
where
R: SpanSystem<BufId>,
I: StringSource,
Expand Down
2 changes: 1 addition & 1 deletion src/assembler/session/lex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ where
) -> Result<Self::TokenIter, CodebaseError>;
}

impl<'a, C, R, I, D, L> Lex<R, I> for CompositeSession<C, R, I, D, L>
impl<'a, C, R, I, D> Lex<R, I> for CompositeSession<C, R, I, D>
where
C: Codebase,
I: Interner,
Expand Down
15 changes: 7 additions & 8 deletions src/assembler/session/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,12 @@ impl<D, R> MacroSource for VecMacroTable<D, R> {
type MacroId = MacroId;
}

impl<'a, C, R: SpanSystem<BufId>, I: StringSource, D, L> MacroSource
for CompositeSession<C, R, I, D, L>
{
impl<'a, C, R: SpanSystem<BufId>, I: StringSource, D> MacroSource for CompositeSession<C, R, I, D> {
type MacroId = MacroId;
}

impl<'a, C, R, I, D, L> MacroTable<I::StringRef, Literal<I::StringRef>, <Self as SpanSource>::Span>
for CompositeSession<C, R, I, D, L>
impl<'a, C, R, I, D> MacroTable<I::StringRef, Literal<I::StringRef>, <Self as SpanSource>::Span>
for CompositeSession<C, R, I, D>
where
Self: Lex<R, I, Span = R::Span, StringRef = I::StringRef>,
C: Codebase,
Expand All @@ -52,7 +50,6 @@ where
Self: StartScope + NameTable<I::StringRef>,
Self: Backend<R::Span>,
Self: MacroSource<MacroId = MacroId>,
Self: Log<<Self as SymbolSource>::SymbolId, MacroId, I::StringRef, R::Span, R::Stripped>,
<Self as StringSource>::StringRef: 'static,
<Self as SpanSource>::Span: 'static,
<Self as Lex<R, I>>::TokenIter: 'static,
Expand All @@ -66,7 +63,8 @@ where
Box<[R::Span]>,
),
) -> Self::MacroId {
self.log(|| Event::DefineMacro {
#[cfg(test)]
self.log_event(Event::DefineMacro {
name_span: name_span.clone(),
params: (params.clone(), param_spans.clone()),
body: (body.clone(), body_spans.clone()),
Expand All @@ -91,7 +89,8 @@ where
(MacroId(id), name_span): (Self::MacroId, R::Span),
(args, arg_spans): MacroArgs<Token<I::StringRef, Literal<I::StringRef>>, R::Span>,
) {
self.log(|| Event::ExpandMacro {
#[cfg(test)]
self.log_event(Event::ExpandMacro {
name: (MacroId(id), name_span.clone()),
args: (args.clone(), arg_spans.clone()),
});
Expand Down
95 changes: 34 additions & 61 deletions src/assembler/session/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
use self::builder::ObjectBuilder;
use self::macros::{MacroArgs, MacroTable, VecMacroTable};
#[cfg(test)]
use self::macros::MacroArgs;
use self::macros::{MacroTable, VecMacroTable};
use self::resolve::*;

use super::keywords::KEYWORDS;
use super::semantics::Keyword;
use super::syntax::{LexItem, Literal, SemanticToken, Sigil, Token};
#[cfg(test)]
use super::syntax::SemanticToken;
use super::syntax::{LexItem, Literal, Sigil, Token};

use crate::codebase::{BufId, CodebaseError, FileCodebase, FileSystem};
use crate::diagnostics::{CompactDiag, Diagnostics, DiagnosticsContext, EmitDiag, OutputForwarder};
Expand Down Expand Up @@ -128,7 +132,6 @@ pub(super) type Session<'a> = CompositeSession<
RcContextFactory<BufId>,
HashInterner,
OutputForwarder<'a>,
(),
>;

impl<'a> Session<'a> {
Expand All @@ -145,7 +148,8 @@ impl<'a> Session<'a> {
names: BiLevelNameTable::new(),
builder: ObjectBuilder::new(),
diagnostics,
log: (),
#[cfg(test)]
log: Vec::new(),
};
for (string, name) in crate::eval::BUILTIN_SYMBOLS {
let string = session.interner.intern(string);
Expand Down Expand Up @@ -175,7 +179,7 @@ pub(crate) trait TokenStream<R: SpanSource, I: StringSource> {
) -> Option<LexItem<I::StringRef, R::Span>>;
}

pub(super) struct CompositeSession<C, R: SpanSystem<BufId>, I: StringSource, D, L> {
pub(super) struct CompositeSession<C, R: SpanSystem<BufId>, I: StringSource, D> {
pub codebase: C,
pub registry: R,
interner: I,
Expand All @@ -184,29 +188,23 @@ pub(super) struct CompositeSession<C, R: SpanSystem<BufId>, I: StringSource, D,
names: BiLevelNameTable<MacroId, SymbolId, I::StringRef>,
pub builder: ObjectBuilder<R::Span>,
pub diagnostics: D,
#[allow(dead_code)]
log: L,
#[cfg(test)]
log: Vec<Event<SymbolId, MacroId, I::StringRef, R::Span, R::Stripped>>,
}

impl<C, R: SpanSystem<BufId>, I: StringSource, D, L> SpanSource
for CompositeSession<C, R, I, D, L>
{
impl<C, R: SpanSystem<BufId>, I: StringSource, D> SpanSource for CompositeSession<C, R, I, D> {
type Span = R::Span;
}

impl<C, R: SpanSystem<BufId>, I: StringSource, D, L> StringSource
for CompositeSession<C, R, I, D, L>
{
impl<C, R: SpanSystem<BufId>, I: StringSource, D> StringSource for CompositeSession<C, R, I, D> {
type StringRef = I::StringRef;
}

impl<C, R: SpanSystem<BufId>, I: StringSource, D, L> SymbolSource
for CompositeSession<C, R, I, D, L>
{
impl<C, R: SpanSystem<BufId>, I: StringSource, D> SymbolSource for CompositeSession<C, R, I, D> {
type SymbolId = SymbolId;
}

impl<C, R: SpanSystem<BufId>, I: StringSource, D, L> NextToken for CompositeSession<C, R, I, D, L> {
impl<C, R: SpanSystem<BufId>, I: StringSource, D> NextToken for CompositeSession<C, R, I, D> {
fn next_token(&mut self) -> Option<LexItem<Self::StringRef, Self::Span>> {
let token = self
.tokens
Expand All @@ -221,28 +219,24 @@ impl<C, R: SpanSystem<BufId>, I: StringSource, D, L> NextToken for CompositeSess
}
}

impl<C, R: SpanSystem<BufId>, I: StringSource, D, L> EmitDiag<R::Span, R::Stripped>
for CompositeSession<C, R, I, D, L>
impl<C, R: SpanSystem<BufId>, I: StringSource, D> EmitDiag<R::Span, R::Stripped>
for CompositeSession<C, R, I, D>
where
Self: SymbolSource + MacroSource,
Self: Log<
<Self as SymbolSource>::SymbolId,
<Self as MacroSource>::MacroId,
I::StringRef,
R::Span,
R::Stripped,
>,
for<'a> DiagnosticsContext<'a, C, R, D>: EmitDiag<R::Span, R::Stripped>,
R::Stripped: Clone,
{
fn emit_diag(&mut self, diag: impl Into<CompactDiag<R::Span, R::Stripped>>) {
let diag = diag.into();
self.log(|| Event::EmitDiag { diag: diag.clone() });

#[cfg(test)]
self.log_event(Event::EmitDiag { diag: diag.clone() });

self.diagnostics().emit_diag(diag)
}
}

impl<C, R: SpanSystem<BufId>, I: StringSource, D, L> CompositeSession<C, R, I, D, L> {
impl<C, R: SpanSystem<BufId>, I: StringSource, D> CompositeSession<C, R, I, D> {
fn diagnostics(&mut self) -> DiagnosticsContext<C, R, D> {
DiagnosticsContext {
codebase: &mut self.codebase,
Expand All @@ -252,16 +246,16 @@ impl<C, R: SpanSystem<BufId>, I: StringSource, D, L> CompositeSession<C, R, I, D
}
}

impl<C, R: SpanSystem<BufId>, I: StringSource, D, L> MergeSpans<R::Span>
for CompositeSession<C, R, I, D, L>
impl<C, R: SpanSystem<BufId>, I: StringSource, D> MergeSpans<R::Span>
for CompositeSession<C, R, I, D>
{
fn merge_spans(&mut self, left: &R::Span, right: &R::Span) -> R::Span {
self.registry.merge_spans(left, right)
}
}

impl<C, R: SpanSystem<BufId>, I: StringSource, D, L> StripSpan<R::Span>
for CompositeSession<C, R, I, D, L>
impl<C, R: SpanSystem<BufId>, I: StringSource, D> StripSpan<R::Span>
for CompositeSession<C, R, I, D>
{
type Stripped = R::Stripped;

Expand All @@ -270,7 +264,7 @@ impl<C, R: SpanSystem<BufId>, I: StringSource, D, L> StripSpan<R::Span>
}
}

impl<C, R: SpanSystem<BufId>, I: Interner, D, L> Interner for CompositeSession<C, R, I, D, L> {
impl<C, R: SpanSystem<BufId>, I: Interner, D> Interner for CompositeSession<C, R, I, D> {
fn intern(&mut self, string: &str) -> Self::StringRef {
self.interner.intern(string)
}
Expand Down Expand Up @@ -341,33 +335,17 @@ impl Interner for HashInterner {
}

#[cfg(test)]
impl<C, R: SpanSystem<BufId>, I: Interner, D, BB, MM, S, T>
CompositeSession<C, R, I, D, Vec<Event<BB, MM, I::StringRef, S, T>>>
{
pub fn log(&self) -> &[Event<BB, MM, I::StringRef, S, T>] {
impl<C, R: SpanSystem<BufId>, I: StringSource, D> CompositeSession<C, R, I, D> {
pub fn log(&self) -> &[Event<SymbolId, MacroId, I::StringRef, R::Span, R::Stripped>] {
&self.log
}
}

pub(super) trait Log<B, M, R, S, T> {
fn log<F: FnOnce() -> Event<B, M, R, S, T>>(&mut self, f: F);
}

impl<C, R: SpanSystem<BufId>, I: Interner, D, BB, MM, S, T> Log<BB, MM, I::StringRef, S, T>
for CompositeSession<C, R, I, D, ()>
{
fn log<F: FnOnce() -> Event<BB, MM, I::StringRef, S, T>>(&mut self, _: F) {}
}

#[cfg(test)]
impl<C, R: SpanSystem<BufId>, I: Interner, D, BB, MM, S, T> Log<BB, MM, I::StringRef, S, T>
for CompositeSession<C, R, I, D, Vec<Event<BB, MM, I::StringRef, S, T>>>
{
fn log<F: FnOnce() -> Event<BB, MM, I::StringRef, S, T>>(&mut self, f: F) {
self.log.push(f())
fn log_event(&mut self, event: Event<SymbolId, MacroId, I::StringRef, R::Span, R::Stripped>) {
self.log.push(event)
}
}

#[cfg(test)]
#[derive(Clone, Debug, PartialEq)]
pub(super) enum Event<B, M, R, S, T> {
AnalyzeFile {
Expand Down Expand Up @@ -420,13 +398,8 @@ pub mod mock {

pub type Expr<S> = crate::expr::Expr<SymbolId, S>;

pub(in crate::assembler) type MockSession<S> = CompositeSession<
FakeCodebase,
FakeSpanSystem<BufId, S>,
MockInterner,
IgnoreDiagnostics,
Vec<Event<SymbolId, MacroId, String, S, S>>,
>;
pub(in crate::assembler) type MockSession<S> =
CompositeSession<FakeCodebase, FakeSpanSystem<BufId, S>, MockInterner, IgnoreDiagnostics>;

impl<S: Clone + Default + Merge> MockSession<S> {
pub fn new() -> Self {
Expand Down
14 changes: 4 additions & 10 deletions src/assembler/session/reentrancy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use crate::assembler::syntax::{LexError, Literal, ParseTokenStream};
use crate::codebase::{BufId, Codebase, CodebaseError};
use crate::span::{SpanSource, SpanSystem};

impl<C, R, I, D, L> ReentrancyActions<<Self as StringSource>::StringRef, R::Span>
for CompositeSession<C, R, I, D, L>
impl<C, R, I, D> ReentrancyActions<<Self as StringSource>::StringRef, R::Span>
for CompositeSession<C, R, I, D>
where
C: Codebase,
Self: Lex<R, I, Span = R::Span>,
Expand All @@ -26,13 +26,6 @@ where
Self: EmitDiag<R::Span, R::Stripped>,
Self: StartScope + NameTable<<Self as StringSource>::StringRef>,
Self: Backend<R::Span>,
Self: Log<
<Self as SymbolSource>::SymbolId,
<Self as MacroSource>::MacroId,
I::StringRef,
R::Span,
R::Stripped,
>,
<Self as StringSource>::StringRef: 'static,
<Self as SpanSource>::Span: 'static,
<Self as Lex<R, I>>::TokenIter: 'static,
Expand All @@ -42,7 +35,8 @@ where
path: <Self as StringSource>::StringRef,
from: Option<R::Span>,
) -> Result<(), CodebaseError> {
self.log(|| Event::AnalyzeFile {
#[cfg(test)]
self.log_event(Event::AnalyzeFile {
path: path.clone(),
from: from.clone(),
});
Expand Down
Loading

0 comments on commit 72429a5

Please sign in to comment.