Skip to content
8 changes: 4 additions & 4 deletions crates/mun/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,18 @@ fn start(matches: &ArgMatches) -> Result<(), failure::Error> {
})?;

if let Some(ret_type) = fn_info.signature.return_type() {
let type_guid = ret_type.guid;
if type_guid == bool::type_guid() {
let type_guid = &ret_type.guid;
if *type_guid == bool::type_guid() {
let result: bool =
invoke_fn!(runtime, entry_point).map_err(|e| failure::err_msg(format!("{}", e)))?;

println!("{}", result)
} else if type_guid == f64::type_guid() {
} else if *type_guid == f64::type_guid() {
let result: f64 =
invoke_fn!(runtime, entry_point).map_err(|e| failure::err_msg(format!("{}", e)))?;

println!("{}", result)
} else if type_guid == i64::type_guid() {
} else if *type_guid == i64::type_guid() {
let result: i64 =
invoke_fn!(runtime, entry_point).map_err(|e| failure::err_msg(format!("{}", e)))?;

Expand Down
4 changes: 4 additions & 0 deletions crates/mun_codegen/src/code_gen/symbols.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ fn gen_signature_from_function<D: IrDatabase>(
function: hir::Function,
) -> StructValue {
let name_str = intern_string(&module, &function.name(db).to_string());
let _visibility = match function.visibility(db) {
hir::Visibility::Public => 0,
_ => 1,
};
let ret_type_ir = gen_signature_return_type(db, module, types, function);
let params_type_ir = gen_signature_argument_types(db, module, types, function);

Expand Down
24 changes: 23 additions & 1 deletion crates/mun_hir/src/code_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::resolve::{Resolution, Resolver};
use crate::ty::InferenceResult;
use crate::type_ref::{TypeRefBuilder, TypeRefId, TypeRefMap, TypeRefSourceMap};
use crate::{ids::FunctionId, AsName, DefDatabase, FileId, HirDatabase, Name, Ty};
use mun_syntax::ast::{NameOwner, TypeAscriptionOwner};
use mun_syntax::ast::{NameOwner, TypeAscriptionOwner, VisibilityOwner};
use rustc_hash::FxHashMap;
use std::sync::Arc;

Expand Down Expand Up @@ -127,6 +127,12 @@ pub enum DefWithBody {
Function(Function),
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Visibility {
Public,
Private,
}

impl DefWithBody {
pub fn infer(self, db: &impl HirDatabase) -> Arc<InferenceResult> {
db.infer(self)
Expand Down Expand Up @@ -163,6 +169,7 @@ pub struct Function {
pub struct FnData {
name: Name,
params: Vec<TypeRefId>,
visibility: Visibility,
ret_type: TypeRefId,
type_ref_map: TypeRefMap,
type_ref_source_map: TypeRefSourceMap,
Expand All @@ -178,6 +185,12 @@ impl FnData {
.map(|n| n.as_name())
.unwrap_or_else(Name::missing);

let visibility = src
.ast
.visibility()
.map(|_v| Visibility::Public)
.unwrap_or(Visibility::Private);

let mut params = Vec::new();
if let Some(param_list) = src.ast.param_list() {
for param in param_list.params() {
Expand All @@ -197,6 +210,7 @@ impl FnData {
Arc::new(FnData {
name,
params,
visibility,
ret_type,
type_ref_map,
type_ref_source_map,
Expand All @@ -211,6 +225,10 @@ impl FnData {
&self.params
}

pub fn visibility(&self) -> Visibility {
self.visibility
}

pub fn ret_type(&self) -> &TypeRefId {
&self.ret_type
}
Expand All @@ -235,6 +253,10 @@ impl Function {
self.data(db).name.clone()
}

pub fn visibility(self, db: &impl HirDatabase) -> Visibility {
self.data(db).visibility()
}

pub fn data(self, db: &impl HirDatabase) -> Arc<FnData> {
db.fn_data(self)
}
Expand Down
2 changes: 1 addition & 1 deletion crates/mun_hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,4 @@ use crate::{
source_id::{AstIdMap, FileAstId},
};

pub use self::code_model::{FnData, Function, Module, ModuleDef};
pub use self::code_model::{FnData, Function, Module, ModuleDef, Visibility};
5 changes: 1 addition & 4 deletions crates/mun_syntax/src/grammar.ron
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,8 @@ Grammar(
"let",
"mut",
"class",
"public",
"protected",
"private",
"export",
"never",
"pub"
],
literals: [
"INT_NUMBER",
Expand Down
4 changes: 2 additions & 2 deletions crates/mun_syntax/src/parsing/grammar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,9 @@ fn name_ref(p: &mut Parser) {
}

fn opt_visibility(p: &mut Parser) -> bool {
if p.at(EXPORT_KW) {
if p.at(PUB_KW) {
let m = p.start();
p.bump(EXPORT_KW);
p.bump(PUB_KW);
m.complete(p, VISIBILITY);
true
} else {
Expand Down
2 changes: 1 addition & 1 deletion crates/mun_syntax/src/parsing/grammar/declarations.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::*;
use crate::T;

pub(super) const DECLARATION_RECOVERY_SET: TokenSet = token_set![FN_KW, EXPORT_KW];
pub(super) const DECLARATION_RECOVERY_SET: TokenSet = token_set![FN_KW, PUB_KW];

pub(super) fn mod_contents(p: &mut Parser) {
while !p.at(EOF) {
Expand Down
25 changes: 5 additions & 20 deletions crates/mun_syntax/src/syntax_kind/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,8 @@ pub enum SyntaxKind {
LET_KW,
MUT_KW,
CLASS_KW,
PUBLIC_KW,
PROTECTED_KW,
PRIVATE_KW,
EXPORT_KW,
NEVER_KW,
PUB_KW,
INT_NUMBER,
FLOAT_NUMBER,
STRING,
Expand Down Expand Up @@ -177,11 +174,8 @@ macro_rules! T {
(let) => { $crate::SyntaxKind::LET_KW };
(mut) => { $crate::SyntaxKind::MUT_KW };
(class) => { $crate::SyntaxKind::CLASS_KW };
(public) => { $crate::SyntaxKind::PUBLIC_KW };
(protected) => { $crate::SyntaxKind::PROTECTED_KW };
(private) => { $crate::SyntaxKind::PRIVATE_KW };
(export) => { $crate::SyntaxKind::EXPORT_KW };
(never) => { $crate::SyntaxKind::NEVER_KW };
(pub) => { $crate::SyntaxKind::PUB_KW };
}

impl From<u16> for SyntaxKind {
Expand Down Expand Up @@ -219,11 +213,8 @@ impl SyntaxKind {
| LET_KW
| MUT_KW
| CLASS_KW
| PUBLIC_KW
| PROTECTED_KW
| PRIVATE_KW
| EXPORT_KW
| NEVER_KW
| PUB_KW
=> true,
_ => false
}
Expand Down Expand Up @@ -339,11 +330,8 @@ impl SyntaxKind {
LET_KW => &SyntaxInfo { name: "LET_KW" },
MUT_KW => &SyntaxInfo { name: "MUT_KW" },
CLASS_KW => &SyntaxInfo { name: "CLASS_KW" },
PUBLIC_KW => &SyntaxInfo { name: "PUBLIC_KW" },
PROTECTED_KW => &SyntaxInfo { name: "PROTECTED_KW" },
PRIVATE_KW => &SyntaxInfo { name: "PRIVATE_KW" },
EXPORT_KW => &SyntaxInfo { name: "EXPORT_KW" },
NEVER_KW => &SyntaxInfo { name: "NEVER_KW" },
PUB_KW => &SyntaxInfo { name: "PUB_KW" },
INT_NUMBER => &SyntaxInfo { name: "INT_NUMBER" },
FLOAT_NUMBER => &SyntaxInfo { name: "FLOAT_NUMBER" },
STRING => &SyntaxInfo { name: "STRING" },
Expand Down Expand Up @@ -408,11 +396,8 @@ impl SyntaxKind {
"let" => LET_KW,
"mut" => MUT_KW,
"class" => CLASS_KW,
"public" => PUBLIC_KW,
"protected" => PROTECTED_KW,
"private" => PRIVATE_KW,
"export" => EXPORT_KW,
"never" => NEVER_KW,
"pub" => PUB_KW,
_ => return None,
};
Some(kw)
Expand Down
4 changes: 2 additions & 2 deletions crates/mun_syntax/src/tests/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ fn keywords() {
lex_snapshot(
r#"
and break do else false for fn if in nil
return true while let mut class public protected
private never loop
return true while let mut class
never loop pub
"#,
)
}
Expand Down
3 changes: 2 additions & 1 deletion crates/mun_syntax/src/tests/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ fn function() {
// Comment that belongs to the function
fn a() {}
fn b(value:number) {}
export fn c():never {}
pub fn d() {}
pub fn c():never {}
fn b(value:number):number {}"#,
);
}
Expand Down
12 changes: 4 additions & 8 deletions crates/mun_syntax/src/tests/snapshots/lexer__keywords.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: crates/mun_syntax/src/tests/lexer.rs
expression: "and break do else false for fn if in nil\nreturn true while let mut class public protected\nprivate never loop"
expression: "and break do else false for fn if in nil\nreturn true while let mut class \nnever loop pub"
---
AND_KW 3 "and"
WHITESPACE 1 " "
Expand Down Expand Up @@ -33,14 +33,10 @@ WHITESPACE 1 " "
MUT_KW 3 "mut"
WHITESPACE 1 " "
CLASS_KW 5 "class"
WHITESPACE 1 " "
PUBLIC_KW 6 "public"
WHITESPACE 1 " "
PROTECTED_KW 9 "protected"
WHITESPACE 1 "\n"
PRIVATE_KW 7 "private"
WHITESPACE 1 " "
WHITESPACE 2 " \n"
NEVER_KW 5 "never"
WHITESPACE 1 " "
LOOP_KW 4 "loop"
WHITESPACE 1 " "
PUB_KW 3 "pub"

116 changes: 66 additions & 50 deletions crates/mun_syntax/src/tests/snapshots/parser__function.snap
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
source: crates/mun_syntax/src/tests/parser.rs
expression: "// Source file comment\n\n// Comment that belongs to the function\nfn a() {}\nfn b(value:number) {}\nexport fn c():never {}\nfn b(value:number):number {}"
expression: "// Source file comment\n\n// Comment that belongs to the function\nfn a() {}\nfn b(value:number) {}\npub fn d() {}\npub fn c():never {}\nfn b(value:number):number {}"
---
SOURCE_FILE@[0; 147)
SOURCE_FILE@[0; 158)
COMMENT@[0; 22) "// Source file comment"
WHITESPACE@[22; 24) "\n\n"
FUNCTION_DEF@[24; 73)
Expand Down Expand Up @@ -42,54 +42,70 @@ SOURCE_FILE@[0; 147)
BLOCK_EXPR@[93; 95)
L_CURLY@[93; 94) "{"
R_CURLY@[94; 95) "}"
FUNCTION_DEF@[95; 118)
FUNCTION_DEF@[95; 109)
WHITESPACE@[95; 96) "\n"
VISIBILITY@[96; 102)
EXPORT_KW@[96; 102) "export"
VISIBILITY@[96; 99)
PUB_KW@[96; 99) "pub"
WHITESPACE@[99; 100) " "
FN_KW@[100; 102) "fn"
WHITESPACE@[102; 103) " "
FN_KW@[103; 105) "fn"
WHITESPACE@[105; 106) " "
NAME@[106; 107)
IDENT@[106; 107) "c"
PARAM_LIST@[107; 109)
L_PAREN@[107; 108) "("
R_PAREN@[108; 109) ")"
RET_TYPE@[109; 115)
COLON@[109; 110) ":"
NEVER_TYPE@[110; 115)
NEVER_KW@[110; 115) "never"
WHITESPACE@[115; 116) " "
BLOCK_EXPR@[116; 118)
L_CURLY@[116; 117) "{"
R_CURLY@[117; 118) "}"
FUNCTION_DEF@[118; 147)
WHITESPACE@[118; 119) "\n"
FN_KW@[119; 121) "fn"
WHITESPACE@[121; 122) " "
NAME@[122; 123)
IDENT@[122; 123) "b"
PARAM_LIST@[123; 137)
L_PAREN@[123; 124) "("
PARAM@[124; 136)
BIND_PAT@[124; 129)
NAME@[124; 129)
IDENT@[124; 129) "value"
COLON@[129; 130) ":"
PATH_TYPE@[130; 136)
PATH@[130; 136)
PATH_SEGMENT@[130; 136)
NAME_REF@[130; 136)
IDENT@[130; 136) "number"
R_PAREN@[136; 137) ")"
RET_TYPE@[137; 144)
COLON@[137; 138) ":"
PATH_TYPE@[138; 144)
PATH@[138; 144)
PATH_SEGMENT@[138; 144)
NAME_REF@[138; 144)
IDENT@[138; 144) "number"
WHITESPACE@[144; 145) " "
BLOCK_EXPR@[145; 147)
L_CURLY@[145; 146) "{"
R_CURLY@[146; 147) "}"
NAME@[103; 104)
IDENT@[103; 104) "d"
PARAM_LIST@[104; 106)
L_PAREN@[104; 105) "("
R_PAREN@[105; 106) ")"
WHITESPACE@[106; 107) " "
BLOCK_EXPR@[107; 109)
L_CURLY@[107; 108) "{"
R_CURLY@[108; 109) "}"
FUNCTION_DEF@[109; 129)
WHITESPACE@[109; 110) "\n"
VISIBILITY@[110; 113)
PUB_KW@[110; 113) "pub"
WHITESPACE@[113; 114) " "
FN_KW@[114; 116) "fn"
WHITESPACE@[116; 117) " "
NAME@[117; 118)
IDENT@[117; 118) "c"
PARAM_LIST@[118; 120)
L_PAREN@[118; 119) "("
R_PAREN@[119; 120) ")"
RET_TYPE@[120; 126)
COLON@[120; 121) ":"
NEVER_TYPE@[121; 126)
NEVER_KW@[121; 126) "never"
WHITESPACE@[126; 127) " "
BLOCK_EXPR@[127; 129)
L_CURLY@[127; 128) "{"
R_CURLY@[128; 129) "}"
FUNCTION_DEF@[129; 158)
WHITESPACE@[129; 130) "\n"
FN_KW@[130; 132) "fn"
WHITESPACE@[132; 133) " "
NAME@[133; 134)
IDENT@[133; 134) "b"
PARAM_LIST@[134; 148)
L_PAREN@[134; 135) "("
PARAM@[135; 147)
BIND_PAT@[135; 140)
NAME@[135; 140)
IDENT@[135; 140) "value"
COLON@[140; 141) ":"
PATH_TYPE@[141; 147)
PATH@[141; 147)
PATH_SEGMENT@[141; 147)
NAME_REF@[141; 147)
IDENT@[141; 147) "number"
R_PAREN@[147; 148) ")"
RET_TYPE@[148; 155)
COLON@[148; 149) ":"
PATH_TYPE@[149; 155)
PATH@[149; 155)
PATH_SEGMENT@[149; 155)
NAME_REF@[149; 155)
IDENT@[149; 155) "number"
WHITESPACE@[155; 156) " "
BLOCK_EXPR@[156; 158)
L_CURLY@[156; 157) "{"
R_CURLY@[157; 158) "}"