Skip to content

Commit

Permalink
feat: Add pub modifier (#2754)
Browse files Browse the repository at this point in the history
Co-authored-by: guipublic <guipublic@gmail.com>
  • Loading branch information
jfecher and guipublic authored Sep 21, 2023
1 parent 2107ebc commit dda964e
Show file tree
Hide file tree
Showing 51 changed files with 471 additions and 352 deletions.
8 changes: 4 additions & 4 deletions compiler/noirc_driver/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,17 +260,17 @@ fn compile_contract_inner(
continue;
}
};
let func_meta = context.def_interner.function_meta(&function_id);
let func_type = func_meta
let modifiers = context.def_interner.function_modifiers(&function_id);
let func_type = modifiers
.contract_function_type
.expect("Expected contract function to have a contract visibility");

let function_type = ContractFunctionType::new(func_type, func_meta.is_unconstrained);
let function_type = ContractFunctionType::new(func_type, modifiers.is_unconstrained);

functions.push(ContractFunction {
name,
function_type,
is_internal: func_meta.is_internal.unwrap_or(false),
is_internal: modifiers.is_internal.unwrap_or(false),
abi: function.abi,
bytecode: function.circuit,
debug: function.debug,
Expand Down
4 changes: 4 additions & 0 deletions compiler/noirc_frontend/src/ast/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,9 @@ pub struct FunctionDefinition {
/// True if this function was defined with the 'unconstrained' keyword
pub is_unconstrained: bool,

/// True if this function was defined with the 'pub' keyword
pub is_public: bool,

pub generics: UnresolvedGenerics,
pub parameters: Vec<(Pattern, UnresolvedType, Visibility)>,
pub body: BlockExpression,
Expand Down Expand Up @@ -649,6 +652,7 @@ impl FunctionDefinition {
is_open: false,
is_internal: false,
is_unconstrained: false,
is_public: false,
generics: generics.clone(),
parameters: p,
body: body.clone(),
Expand Down
5 changes: 2 additions & 3 deletions compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -850,8 +850,7 @@ fn resolve_function_set(

vecmap(unresolved_functions.functions, |(mod_id, func_id, func)| {
let module_id = ModuleId { krate: crate_id, local_id: mod_id };
let path_resolver =
StandardPathResolver::new(ModuleId { local_id: mod_id, krate: crate_id });
let path_resolver = StandardPathResolver::new(module_id);

let mut resolver = Resolver::new(interner, &path_resolver, def_maps, file_id);
// Must use set_generics here to ensure we re-use the same generics from when
Expand All @@ -860,7 +859,7 @@ fn resolve_function_set(
resolver.set_generics(impl_generics.clone());
resolver.set_self_type(self_type.clone());

let (hir_func, func_meta, errs) = resolver.resolve_function(func, func_id, module_id);
let (hir_func, func_meta, errs) = resolver.resolve_function(func, func_id);
interner.push_fn_meta(func_meta, func_id);
interner.update_fn(func_id, hir_func);
extend_errors(errors, file_id, errs);
Expand Down
54 changes: 39 additions & 15 deletions compiler/noirc_frontend/src/hir/def_collector/dc_mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ use crate::{
def_collector::dc_crate::{UnresolvedStruct, UnresolvedTrait},
def_map::ScopeResolveError,
},
node_interner::TraitId,
node_interner::{FunctionModifiers, TraitId},
parser::SubModule,
token::Attributes,
FunctionDefinition, Ident, LetStatement, NoirFunction, NoirStruct, NoirTrait, NoirTraitImpl,
NoirTypeAlias, ParsedModule, TraitImplItem, TraitItem, TypeImpl,
};
Expand Down Expand Up @@ -71,11 +72,11 @@ pub fn collect_defs(

collector.collect_type_aliases(context, ast.type_aliases, errors);

collector.collect_functions(context, ast.functions, errors);
collector.collect_functions(context, ast.functions, crate_id, errors);

collector.collect_trait_impls(context, ast.trait_impls, errors);
collector.collect_trait_impls(context, ast.trait_impls, crate_id, errors);

collector.collect_impls(context, ast.impls);
collector.collect_impls(context, ast.impls, crate_id);
}

impl<'a> ModCollector<'a> {
Expand Down Expand Up @@ -114,14 +115,16 @@ impl<'a> ModCollector<'a> {
}
}

fn collect_impls(&mut self, context: &mut Context, impls: Vec<TypeImpl>) {
fn collect_impls(&mut self, context: &mut Context, impls: Vec<TypeImpl>, krate: CrateId) {
let module_id = ModuleId { krate, local_id: self.module_id };

for r#impl in impls {
let mut unresolved_functions =
UnresolvedFunctions { file_id: self.file_id, functions: Vec::new() };

for method in r#impl.methods {
let func_id = context.def_interner.push_empty_fn();
context.def_interner.push_function_definition(method.name().to_owned(), func_id);
context.def_interner.push_function(func_id, &method.def, module_id);
unresolved_functions.push_fn(self.module_id, func_id, method);
}

Expand All @@ -135,8 +138,11 @@ impl<'a> ModCollector<'a> {
&mut self,
context: &mut Context,
impls: Vec<NoirTraitImpl>,
krate: CrateId,
errors: &mut Vec<FileDiagnostic>,
) {
let module_id = ModuleId { krate, local_id: self.module_id };

for trait_impl in impls {
let trait_name = &trait_impl.trait_name;
let module = &self.def_collector.def_map.modules[self.module_id.0];
Expand All @@ -149,13 +155,13 @@ impl<'a> ModCollector<'a> {
context,
&trait_impl,
&collected_trait.trait_def,
krate,
errors,
);

for (_, func_id, noir_function) in &unresolved_functions.functions {
let name = noir_function.name().to_owned();

context.def_interner.push_function_definition(name, *func_id);
let function = &noir_function.def;
context.def_interner.push_function(*func_id, function, module_id);
}

let unresolved_trait_impl = UnresolvedTraitImpl {
Expand Down Expand Up @@ -200,17 +206,18 @@ impl<'a> ModCollector<'a> {
context: &mut Context,
trait_impl: &NoirTraitImpl,
trait_def: &NoirTrait,
krate: CrateId,
errors: &mut Vec<FileDiagnostic>,
) -> UnresolvedFunctions {
let mut unresolved_functions =
UnresolvedFunctions { file_id: self.file_id, functions: Vec::new() };

let module = ModuleId { krate, local_id: self.module_id };

for item in &trait_impl.items {
if let TraitImplItem::Function(impl_method) = item {
let func_id = context.def_interner.push_empty_fn();
context
.def_interner
.push_function_definition(impl_method.name().to_owned(), func_id);
context.def_interner.push_function(func_id, &impl_method.def, module);
unresolved_functions.push_fn(self.module_id, func_id, impl_method.clone());
}
}
Expand Down Expand Up @@ -244,7 +251,21 @@ impl<'a> ModCollector<'a> {
// if there's a default implementation for the method, use it
let method_name = name.0.contents.clone();
let func_id = context.def_interner.push_empty_fn();
context.def_interner.push_function_definition(method_name, func_id);
let modifiers = FunctionModifiers {
// trait functions are always public
visibility: crate::Visibility::Public,
attributes: Attributes::empty(),
is_unconstrained: false,
contract_function_type: None,
is_internal: None,
};

context.def_interner.push_function_definition(
method_name,
func_id,
modifiers,
module,
);
let impl_method = NoirFunction::normal(FunctionDefinition::normal(
name,
generics,
Expand Down Expand Up @@ -292,18 +313,21 @@ impl<'a> ModCollector<'a> {
&mut self,
context: &mut Context,
functions: Vec<NoirFunction>,
krate: CrateId,
errors: &mut Vec<FileDiagnostic>,
) {
let mut unresolved_functions =
UnresolvedFunctions { file_id: self.file_id, functions: Vec::new() };

let module = ModuleId { krate, local_id: self.module_id };

for mut function in functions {
let name = function.name_ident().clone();
let func_id = context.def_interner.push_empty_fn();

// First create dummy function in the DefInterner
// So that we can get a FuncId
let func_id = context.def_interner.push_empty_fn();
context.def_interner.push_function_definition(name.0.contents.clone(), func_id);
context.def_interner.push_function(func_id, &function.def, module);

// Then go over the where clause and assign trait_ids to the constraints
for constraint in &mut function.def.where_clause {
Expand Down
7 changes: 4 additions & 3 deletions compiler/noirc_frontend/src/hir/def_map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,11 @@ impl CrateDefMap {
self.modules.iter().flat_map(|(_, module)| {
module.value_definitions().filter_map(|id| {
if let Some(func_id) = id.as_function() {
let func_meta = interner.function_meta(&func_id);
match func_meta.attributes.function {
let attributes = interner.function_attributes(&func_id);
match &attributes.function {
Some(FunctionAttribute::Test(scope)) => {
Some(TestFunction::new(func_id, scope, func_meta.name.location))
let location = interner.function_meta(&func_id).name.location;
Some(TestFunction::new(func_id, scope.clone(), location))
}
_ => None,
}
Expand Down
6 changes: 3 additions & 3 deletions compiler/noirc_frontend/src/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ impl Context {

let name = self.def_interner.function_name(id);

let meta = self.def_interner.function_meta(id);
let module = self.module(meta.module_id);
let module_id = self.def_interner.function_module(*id);
let module = self.module(module_id);

let parent =
def_map.get_module_path_with_separator(meta.module_id.local_id.0, module.parent, "::");
def_map.get_module_path_with_separator(module_id.local_id.0, module.parent, "::");

if parent.is_empty() {
name.into()
Expand Down
6 changes: 6 additions & 0 deletions compiler/noirc_frontend/src/hir/resolution/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ pub enum ResolverError {
NumericConstantInFormatString { name: String, span: Span },
#[error("Closure environment must be a tuple or unit type")]
InvalidClosureEnvironment { typ: Type, span: Span },
#[error("{name} is private and not visible from the current module")]
PrivateFunctionCalled { name: String, span: Span },
}

impl ResolverError {
Expand Down Expand Up @@ -288,6 +290,10 @@ impl From<ResolverError> for Diagnostic {
ResolverError::InvalidClosureEnvironment { span, typ } => Diagnostic::simple_error(
format!("{typ} is not a valid closure environment type"),
"Closure environment must be a tuple or unit type".to_string(), span),
// This will be upgraded to an error in future versions
ResolverError::PrivateFunctionCalled { span, name } => Diagnostic::simple_warning(
format!("{name} is private and not visible from the current module"),
format!("{name} is private"), span),
}
}
}
Loading

0 comments on commit dda964e

Please sign in to comment.