Skip to content

Commit

Permalink
merge main
Browse files Browse the repository at this point in the history
  • Loading branch information
StunxFS committed Dec 3, 2023
2 parents a144993 + 05ee473 commit c1bbab0
Show file tree
Hide file tree
Showing 18 changed files with 86 additions and 35 deletions.
File renamed without changes.
File renamed without changes.
12 changes: 0 additions & 12 deletions lib/core/src/errors.c.ri

This file was deleted.

10 changes: 9 additions & 1 deletion lib/core/src/errors.ri
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@
// Use of this source code is governed by an MIT license that can
// be found in the LICENSE file.

import c;

pub alias ErrnoError := c.ErrnoError;

// FIXME: pub alias last_errno_error := c.last_errno_error;
pub func last_errno_error() -> c.ErrnoError {
return c.last_errno_error();
}

var returnTrace := ReturnTrace();

/// This trait is used for errors throwed with result types (!T).
Expand Down Expand Up @@ -117,4 +126,3 @@ pub struct ValueOutOfRangeError < Throwable {
return self.msg;
}
}

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
14 changes: 14 additions & 0 deletions lib/rivet/src/ast/SourceFile.ri
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright (C) 2023 The Rivet Developers. All rights reserved.
// Use of this source code is governed by an MIT license that can
// be found in the LICENSE file.

import ../token;

#[boxed]
pub struct SourceFile {
pub path: string;
pub decls: []Decl;
pub mut mod: Module;
pub mut imported_symbols: ImportedSymbols;
pub mut pos: token.Pos;
}
9 changes: 9 additions & 0 deletions lib/rivet/src/ast/Sym.ri
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ pub enum ABI as uint8 < traits.Stringable {
};
}

// TODO: remove, use `.to_lower()` instead
#[inline]
pub func lower(&self) -> string {
return match self.* {
.C -> "c",
.Rivet -> "rivet"
};
}

#[inline]
pub func to_string(&self) -> string {
return match self.* {
Expand Down
10 changes: 1 addition & 9 deletions lib/rivet/src/ast/mod.ri
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,6 @@ pub enum Node {
}
}

#[boxed]
pub struct SourceFile {
pub file: string;
pub decls: []Decl;
pub mut mod: Module;
pub mut imported_symbols: ImportedSymbols;
}

#[boxed]
pub struct Comment {
pub mut is_doc: bool;
Expand Down Expand Up @@ -87,4 +79,4 @@ pub struct ObjectData {
pub mut sym: Var;
pub is_extern: bool;
pub is_global: bool;
}
}
10 changes: 10 additions & 0 deletions lib/rivet/src/checker/mod.ri
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ pub struct Checker {
self.source_file = source_file;
self.expected_type = .Void();
self.check_decls(source_file.decls);
// check unused imports
for imported_sym in source_file.imported_symbols.syms {
if !imported_sym.is_used and imported_sym.name != "_" {
report.warn(
"{} `{}` is imported but never used".fmt(
imported_sym.sym.type_of(), imported_sym.sym.name
), imported_sym.pos
);
}
}
}
// check global mutable variables
for sym in self.table.universe.scope.syms {
Expand Down
2 changes: 1 addition & 1 deletion lib/rivet/src/lib.ri
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ pub struct Compiler {
continue;
}
import_decl.info = self.load_module(
import_decl.path, import_decl.alias_name, sf.file,
import_decl.path, import_decl.alias_name, sf.path,
import_decl.path_pos
)!;
import_decl.alias_name = import_decl.info.alias_name;
Expand Down
3 changes: 2 additions & 1 deletion lib/rivet/src/parser/mod.ri
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ pub struct Parser {
return ast.SourceFile(file, [], self.mod_sym);
}
self.advance(2);
return ast.SourceFile(file, self.parse_decls(), self.mod_sym);
pos := self.tok.pos;
return ast.SourceFile(file, self.parse_decls(), self.mod_sym, pos: pos + self.prev_tok.pos);
}

func next(mut self) {
Expand Down
2 changes: 1 addition & 1 deletion lib/rivet/src/prefs/mod.ri
Original file line number Diff line number Diff line change
Expand Up @@ -323,4 +323,4 @@ pub struct Prefs {
};
self.library_path.push(self.mod_dir);
}
}
}
2 changes: 1 addition & 1 deletion lib/rivet/src/resolver/Register.ri
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ pub struct Register {
},
.Func as func_decl -> func_decl.sym = self.add_sym(ast.Func(
parent: self.sym,
abi: self.abi,
abi: func_decl.abi,
is_public: func_decl.is_public,
is_extern: func_decl.is_extern,
is_unsafe: func_decl.is_unsafe,
Expand Down
19 changes: 19 additions & 0 deletions lib/rivet/src/resolver/exprs.ri
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,7 @@ extend Resolver {
if ident.is_sym and ident.sym is ast.SymRef as mut sym_ref {
ident.sym = self.clean_symbol_reference(sym_ref);
}
self.check_symbol_abi(ident.sym, ident.pos);
} else {
report.error("cannot find `{}` in this scope".fmt(ident.name), ident.pos);
}
Expand Down Expand Up @@ -297,6 +298,7 @@ extend Resolver {
) {
selector.found = true;
selector.sym = sym;
self.check_symbol_abi(sym, selector.pos);
}
}
}
Expand Down Expand Up @@ -353,4 +355,21 @@ extend Resolver {
}
return sym_ref.ref;
}

func check_symbol_abi(mut self, sym: ast.Sym, pos: token.Pos) {
if sym.abi != .Rivet {
if self.source_file_abi == sym.abi {
self.different_abi_usage_count += 1;
} else {
mut warn := report.warn_builder(
"using a symbol whose ABI is different from that of the current file", pos
);
warn.add_note("`{}` is declared as `extern ({})`", sym.name, sym.abi);
warn.add_help(
"consider adding `.{}.ri` extension to current filename", sym.abi.lower()
);
warn.emit();
}
}
}
}
28 changes: 19 additions & 9 deletions lib/rivet/src/resolver/mod.ri
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import ../ast;
import ../prefs;
import ../token;
import ../report;
import std/console;
import { Path } from std/fs;

struct Prelude {
name: string;
Expand All @@ -24,6 +26,8 @@ pub struct Resolver {
mut self_sym_is_set: bool;

mut source_file: ast.SourceFile;
mut source_file_abi: ast.ABI;
mut different_abi_usage_count: uint;

pub func resolve_files(mut self, source_files: []ast.SourceFile) {
Register(self.table, self.prefs).walk_files(source_files);
Expand All @@ -36,20 +40,26 @@ pub struct Resolver {
for sf in source_files {
self.sym = sf.mod;
self.source_file = sf;
self.detect_source_file_abi();
self.different_abi_usage_count = 0;
self.resolve_decls(self.source_file.decls);
// check unused imports
for imported_sym in sf.imported_symbols.syms {
if !imported_sym.is_used and imported_sym.name != "_" {
report.warn(
"{} `{}` is imported but never used".fmt(
imported_sym.sym.type_of(), imported_sym.sym.name
), imported_sym.pos
);
}
if self.source_file_abi == .Rivet and self.different_abi_usage_count > 0 {
report.warn("filename requires ABI specification", sf.pos);
} else if self.source_file_abi != .Rivet and self.different_abi_usage_count == 0 {
report.warn("filename does not require ABI specification", sf.pos);
}
}
}

func detect_source_file_abi(mut self) {
filename := Path.file_name(self.source_file.path);
if filename.ends_with(".c.ri") {
self.source_file_abi = .C;
} else {
self.source_file_abi = .Rivet;
}
}

func check_vis(self, sym: ast.Sym, pos: token.Pos) {
if !sym.is_public and !self.source_file.mod.has_access_to(sym) {
report.error("{} `{}` is private".fmt(sym.type_of(), sym.name), pos);
Expand Down

0 comments on commit c1bbab0

Please sign in to comment.