Skip to content

Commit

Permalink
Use TVec for all template arg vectors
Browse files Browse the repository at this point in the history
  • Loading branch information
VonTum committed Jan 29, 2025
1 parent 5da1def commit 9ac82ae
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 47 deletions.
4 changes: 2 additions & 2 deletions src/codegen/system_verilog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::flattening::{DeclarationKind, Instruction, Module, Port};
use crate::instantiation::{
InstantiatedModule, RealWire, RealWireDataSource, RealWirePathElem, CALCULATE_LATENCY_LATER,
};
use crate::typing::template::{ConcreteTemplateArg, ConcreteTemplateArgs};
use crate::typing::template::{ConcreteTemplateArg, TVec};
use crate::{typing::concrete_type::ConcreteType, value::Value};

use super::shared::*;
Expand Down Expand Up @@ -333,7 +333,7 @@ impl<'g> CodeGenerationContext<'g> {
fn write_template_args(
&mut self,
link_info: &LinkInfo,
concrete_template_args: &ConcreteTemplateArgs,
concrete_template_args: &TVec<ConcreteTemplateArg>,
) {
self.program_text.write_str(&link_info.name).unwrap();
self.program_text.write_str(" #(").unwrap();
Expand Down
6 changes: 3 additions & 3 deletions src/flattening/flatten.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use super::parser::Cursor;
use super::*;

use crate::typing::template::{
GenerativeParameterKind, TemplateArg, TemplateArgKind, TemplateArgs, ParameterKind
GenerativeParameterKind, ParameterKind, TVec, TemplateArg, TemplateArgKind
};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
Expand Down Expand Up @@ -282,7 +282,7 @@ impl<'l, 'errs> FlatteningContext<'l, 'errs> {
}
}

fn flatten_template_args(&mut self, found_global: GlobalUUID, has_template_args: bool, cursor: &mut Cursor) -> TemplateArgs {
fn flatten_template_args(&mut self, found_global: GlobalUUID, has_template_args: bool, cursor: &mut Cursor) -> TVec<Option<TemplateArg>> {
let link_info = self.globals.get_link_info(found_global);
let full_object_name = link_info.get_full_name();

Expand Down Expand Up @@ -1593,7 +1593,7 @@ fn flatten_global(linker: &mut Linker, global_obj : GlobalUUID, cursor: &mut Cur
let typ = &globals[type_uuid];
(UUIDRange::empty().into_iter(), typ.fields.id_range().into_iter(), DeclarationContext::StructField, &FlatAlloc::EMPTY_FLAT_ALLOC)
}
GlobalUUID::Constant(const_uuid) => {
GlobalUUID::Constant(_const_uuid) => {
(UUIDRange::empty().into_iter(), UUIDRange::empty().into_iter(), DeclarationContext::Generative(GenerativeKind::PlainGenerative), &FlatAlloc::EMPTY_FLAT_ALLOC)
}
};
Expand Down
5 changes: 2 additions & 3 deletions src/flattening/initialization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ use crate::linker::{FileBuilder, LinkInfo, ResolvedGlobals};
use crate::{file_position::FileText, flattening::Module, instantiation::InstantiationCache};

use crate::typing::template::{
GenerativeParameterKind, Parameter, ParameterKind, Parameters,
TypeParameterKind,
GenerativeParameterKind, Parameter, ParameterKind, TVec, TypeParameterKind
};

use super::parser::Cursor;
use super::*;

struct InitializationContext<'linker> {
parameters: Parameters,
parameters: TVec<Parameter>,

// module-only stuff
ports: FlatAlloc<Port, PortIDMarker>,
Expand Down
2 changes: 2 additions & 0 deletions src/flattening/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,10 @@ pub struct NamedConstant {
/// TODO: Structs #8
#[derive(Debug)]
pub struct StructField {
#[allow(unused)]
pub name: String,
pub name_span: Span,
#[allow(unused)]
pub decl_span: Span,
/// This is only set after flattening is done. Initially just [UUID::PLACEHOLDER]
pub declaration_instruction: FlatID,
Expand Down
4 changes: 2 additions & 2 deletions src/flattening/walk.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::typing::template::{TemplateArgKind, TemplateArgs};
use crate::typing::template::{TVec, TemplateArg, TemplateArgKind};

use crate::prelude::*;

Expand Down Expand Up @@ -50,7 +50,7 @@ impl WireReferencePathElement {
}

pub fn for_each_generative_input_in_template_args(
template_args: &TemplateArgs,
template_args: &TVec<Option<TemplateArg>>,
f: &mut impl FnMut(FlatID),
) {
for (_id, t_arg) in template_args.iter_valids() {
Expand Down
4 changes: 2 additions & 2 deletions src/instantiation/concrete_typecheck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ fn can_expression_be_value_inferred(link_info: &LinkInfo, expr_id: FlatID) -> Op
Some(*template_id)
}

fn try_to_attach_value_to_template_arg(template_wire_referernce: FlatID, found_value: &ConcreteType, template_args: &mut ConcreteTemplateArgs, submodule_link_info: &LinkInfo) {
fn try_to_attach_value_to_template_arg(template_wire_referernce: FlatID, found_value: &ConcreteType, template_args: &mut TVec<ConcreteTemplateArg>, submodule_link_info: &LinkInfo) {
let ConcreteType::Value(v) = found_value else {return}; // We don't have a value to assign
if let Some(template_id) = can_expression_be_value_inferred(submodule_link_info, template_wire_referernce) {
if let ConcreteTemplateArg::NotProvided = &template_args[template_id] {
Expand All @@ -182,7 +182,7 @@ fn try_to_attach_value_to_template_arg(template_wire_referernce: FlatID, found_v
}
}

fn infer_parameters_by_walking_type(port_wr_typ: &WrittenType, connected_typ: &ConcreteType, template_args: &mut ConcreteTemplateArgs, submodule_link_info: &LinkInfo) {
fn infer_parameters_by_walking_type(port_wr_typ: &WrittenType, connected_typ: &ConcreteType, template_args: &mut TVec<ConcreteTemplateArg>, submodule_link_info: &LinkInfo) {
match port_wr_typ {
WrittenType::Error(_) => {} // Can't continue, bad written type
WrittenType::Named(_) => {} // Seems we've run out of type to check
Expand Down
18 changes: 8 additions & 10 deletions src/instantiation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod unique_names;
use unique_names::UniqueNames;

use crate::prelude::*;
use crate::typing::template::{ConcreteTemplateArg, TVec};
use crate::typing::type_inference::{ConcreteTypeVariableIDMarker, TypeSubstitutor};

use std::cell::OnceCell;
Expand All @@ -21,10 +22,7 @@ use crate::{
value::Value,
};

use crate::typing::{
concrete_type::ConcreteType,
template::ConcreteTemplateArgs,
};
use crate::typing::concrete_type::ConcreteType;

use self::latency_algorithm::SpecifiedLatency;

Expand Down Expand Up @@ -134,7 +132,7 @@ pub struct SubModule {
pub interface_call_sites: FlatAlloc<Vec<Span>, InterfaceIDMarker>,
pub name: String,
pub module_uuid: ModuleUUID,
pub template_args: ConcreteTemplateArgs,
pub template_args: TVec<ConcreteTemplateArg>,
}

/// Generated from [Module::ports]
Expand Down Expand Up @@ -209,7 +207,7 @@ impl SubModuleOrWire {
/// Also, with incremental builds (#49) this will be a prime area for investigation
#[derive(Debug)]
pub struct InstantiationCache {
cache: RefCell<HashMap<ConcreteTemplateArgs, Rc<InstantiatedModule>>>,
cache: RefCell<HashMap<TVec<ConcreteTemplateArg>, Rc<InstantiatedModule>>>,
}

impl InstantiationCache {
Expand All @@ -223,7 +221,7 @@ impl InstantiationCache {
&self,
md: &Module,
linker: &Linker,
template_args: ConcreteTemplateArgs,
template_args: TVec<ConcreteTemplateArg>,
) -> Option<Rc<InstantiatedModule>> {
let cache_borrow = self.cache.borrow();

Expand Down Expand Up @@ -276,7 +274,7 @@ impl InstantiationCache {

// Also passes over invalid instances. Instance validity should not be assumed!
// Only used for things like syntax highlighting
pub fn for_each_instance<F: FnMut(&ConcreteTemplateArgs, &Rc<InstantiatedModule>)>(
pub fn for_each_instance<F: FnMut(&TVec<ConcreteTemplateArg>, &Rc<InstantiatedModule>)>(
&self,
mut f: F,
) {
Expand Down Expand Up @@ -329,7 +327,7 @@ struct InstantiationContext<'fl, 'l> {
interface_ports: FlatAlloc<Option<InstantiatedPort>, PortIDMarker>,
errors: ErrorCollector<'l>,

template_args: &'fl ConcreteTemplateArgs,
template_args: &'fl TVec<ConcreteTemplateArg>,
md: &'fl Module,
linker: &'l Linker,
}
Expand Down Expand Up @@ -363,7 +361,7 @@ impl<'fl, 'l> InstantiationContext<'fl, 'l> {
fn perform_instantiation(
md: &Module,
linker: &Linker,
template_args: &ConcreteTemplateArgs,
template_args: &TVec<ConcreteTemplateArg>,
) -> InstantiatedModule {
let mut context = InstantiationContext {
name: pretty_print_concrete_instance(&md.link_info, template_args, &linker.types),
Expand Down
6 changes: 3 additions & 3 deletions src/linker/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{flattening::{Instruction, NamedConstant}, prelude::*, typing::template::{GenerativeParameterKind, ParameterKind, TypeParameterKind}};
use crate::{flattening::{Instruction, NamedConstant}, prelude::*, typing::template::{GenerativeParameterKind, Parameter, ParameterKind, TVec, TypeParameterKind}};

pub mod checkpoint;
mod resolver;
Expand All @@ -23,7 +23,7 @@ use crate::errors::{CompileError, ErrorInfo, ErrorLevel, ErrorStore};

use crate::flattening::{StructType, TypingAllocator};

use crate::typing::template::Parameters;


use self::checkpoint::CheckPoint;

Expand Down Expand Up @@ -114,7 +114,7 @@ pub struct LinkInfo {
/// Is only temporary. It's used during typechecking to allocate the type unification block
pub type_variable_alloc: TypingAllocator,

pub template_parameters: Parameters,
pub template_parameters: TVec<Parameter>,

/// Created in Stage 2: Flattening. type data is filled out during Typechecking
pub instructions: FlatAlloc<Instruction, FlatIDMarker>,
Expand Down
9 changes: 5 additions & 4 deletions src/to_string.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::prelude::*;

use crate::typing::template::{Parameter, TVec};
use crate::{file_position::FileText, pretty_print_many_spans, value::Value};

use crate::flattening::{
Expand All @@ -9,7 +10,7 @@ use crate::linker::{FileData, LinkInfo};
use crate::typing::{
abstract_type::{AbstractType, DomainType},
concrete_type::ConcreteType,
template::{ConcreteTemplateArg, ConcreteTemplateArgs, Parameters},
template::ConcreteTemplateArg,
};

use std::{
Expand All @@ -20,7 +21,7 @@ use std::{
use std::fmt::Write;
use std::ops::Deref;

pub fn map_to_type_names(parameters: &Parameters) -> FlatAlloc<String, TemplateIDMarker> {
pub fn map_to_type_names(parameters: &TVec<Parameter>) -> FlatAlloc<String, TemplateIDMarker> {
parameters.map(|(_id, v)| v.name.clone())
}

Expand All @@ -34,7 +35,7 @@ impl TemplateNameGetter for FlatAlloc<String, TemplateIDMarker> {
&self[id]
}
}
impl TemplateNameGetter for Parameters {
impl TemplateNameGetter for TVec<Parameter> {
fn get_template_name(&self, id: TemplateID) -> &str {
&self[id].name
}
Expand Down Expand Up @@ -306,7 +307,7 @@ impl Module {

pub fn pretty_print_concrete_instance<TypVec>(
target_link_info: &LinkInfo,
given_template_args: &ConcreteTemplateArgs,
given_template_args: &TVec<ConcreteTemplateArg>,
linker_types: &TypVec,
) -> String
where
Expand Down
10 changes: 7 additions & 3 deletions src/typing/abstract_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::prelude::*;
use crate::value::Value;
use std::ops::Deref;

use super::template::{GlobalReference, TemplateAbstractTypes, Parameters};
use super::template::{GlobalReference, Parameter, TVec};
use super::type_inference::{DomainVariableID, DomainVariableIDMarker, TypeSubstitutor, TypeVariableID, TypeVariableIDMarker, UnifyErrorReport};
use crate::flattening::{BinaryOperator, StructType, TypingAllocator, UnaryOperator, WrittenType};
use crate::linker::get_builtin_type;
Expand Down Expand Up @@ -105,7 +105,7 @@ pub struct TypeUnifier {

impl TypeUnifier {
pub fn new(
parameters: &Parameters,
parameters: &TVec<Parameter>,
typing_alloc: TypingAllocator
) -> Self {
Self {
Expand Down Expand Up @@ -151,7 +151,11 @@ impl TypeUnifier {
/// This should always be what happens first to a given variable.
///
/// Therefore it should be impossible that one of the internal unifications ever fails
pub fn unify_with_written_type_substitute_templates_must_succeed(&self, wr_typ: &WrittenType, typ: &AbstractType, template_type_args: &TemplateAbstractTypes) {
///
/// template_type_args applies to both Template Type args and Template Value args.
///
/// For Types this is the Type, for Values this is unified with the parameter declaration type
pub fn unify_with_written_type_substitute_templates_must_succeed(&self, wr_typ: &WrittenType, typ: &AbstractType, template_type_args: &TVec<AbstractType>) {
match wr_typ {
WrittenType::Error(_span) => {} // Already an error, don't unify
WrittenType::TemplateVariable(_span, argument_id) => {
Expand Down
6 changes: 3 additions & 3 deletions src/typing/concrete_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ use std::ops::Deref;
use crate::linker::get_builtin_type;
use crate::value::Value;

use super::template::ConcreteTemplateArg;
use super::template::ConcreteTemplateArgs;
use super::template::{ConcreteTemplateArg, TVec};

use super::type_inference::ConcreteTypeVariableID;

pub const BOOL_CONCRETE_TYPE: ConcreteType = ConcreteType::Named(ConcreteGlobalReference {
Expand All @@ -23,7 +23,7 @@ pub const INT_CONCRETE_TYPE: ConcreteType = ConcreteType::Named(ConcreteGlobalRe
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ConcreteGlobalReference<ID> {
pub id: ID,
pub template_args: ConcreteTemplateArgs,
pub template_args: TVec<ConcreteTemplateArg>,
}

/// A post-instantiation type. These fully define what wires should be generated for a given object.
Expand Down
16 changes: 4 additions & 12 deletions src/typing/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use std::fmt::Display;
pub struct GlobalReference<ID> {
pub name_span: Span,
pub id: ID,
pub template_args: TemplateArgs,
pub template_arg_types: TemplateAbstractTypes,
pub template_args: TVec<Option<TemplateArg>>,
pub template_arg_types: TVec<AbstractType>,
pub template_span: Option<BracketSpan>,
}

Expand Down Expand Up @@ -178,13 +178,5 @@ impl ConcreteTemplateArg {
}
}

/// See [TemplateArg]
pub type TemplateArgs = FlatAlloc<Option<TemplateArg>, TemplateIDMarker>;
/// Applies to both Template Type args and Template Value args.
///
/// For Types this is the Type, for Values this is unified with the parameter declaration type
pub type TemplateAbstractTypes = FlatAlloc<AbstractType, TemplateIDMarker>;
/// See [Parameter]
pub type Parameters = FlatAlloc<Parameter, TemplateIDMarker>;
/// See [ConcreteTemplateArg]
pub type ConcreteTemplateArgs = FlatAlloc<ConcreteTemplateArg, TemplateIDMarker>;
/// A convienent type alias for all places where lists of template args are needed
pub type TVec<T> = FlatAlloc<T, TemplateIDMarker>;
1 change: 1 addition & 0 deletions src/typing/type_inference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,7 @@ pub enum DelayedConstraintStatus {
/// The constraint can be removed
Resolved,
/// Progress was made, (potentially enabling other parts to continue), but the constraint cannot be removed
#[allow(unused)]
Progress,
/// No progress was made, if all constraints return [DelayedConstraintStatus::NoProgress] then type resolution deadlocked and cannot finish.
NoProgress
Expand Down

0 comments on commit 9ac82ae

Please sign in to comment.