diff --git a/lib/rivet/src/parser/decls.ri b/lib/rivet/src/parser/decls.ri index a1a9f5ffd..5e905aa36 100644 --- a/lib/rivet/src/parser/decls.ri +++ b/lib/rivet/src/parser/decls.ri @@ -90,9 +90,11 @@ extend Parser { func parse_decl(mut self) -> ast.Decl { doc_comment := self.parse_doc_comments(); - attributes := self.parse_attributes( - self.tok.kind == .Hash && self.peek_tok.kind == .Bang - ); + is_mod_attr := self.tok.kind == .Hash && self.peek_tok.kind == .Bang; + attributes := self.parse_attributes(is_mod_attr); + if is_mod_attr { + return .Empty(); + } is_public := self.is_public(); mut pos := self.tok.pos; match { diff --git a/lib/std/src/dynlib/mod.c.ri b/lib/std/src/dynlib/mod.c.ri index d8f6c840d..5bd7ddeb5 100644 --- a/lib/std/src/dynlib/mod.c.ri +++ b/lib/std/src/dynlib/mod.c.ri @@ -46,9 +46,8 @@ pub func load(path: string, global_symbols: bool := false) -> !Library { } if dl_error := dlerror() { throw CannotLoadLibraryError("cannot load '{}': {}".fmt(path, dl_error)); - } else { - throw CannotLoadLibraryError("cannot load '{}'".fmt(path)); } + throw CannotLoadLibraryError("cannot load '{}'".fmt(path)); } #[boxed] diff --git a/rivetc/src/ast.py b/rivetc/src/ast.py index 0a4ba97de..bd02d0e2e 100644 --- a/rivetc/src/ast.py +++ b/rivetc/src/ast.py @@ -75,7 +75,7 @@ def __str__(self): # ---- Declarations ---- class EmptyDecl: def __init__(self): - self.attributes = Annotations() + self.attributes = Attributes() class DocComment: def __init__(self, lines, pos): @@ -95,13 +95,13 @@ def merge(self): res += " " return res -class AnnotationArg: +class AttributeArg: def __init__(self, name, expr): self.name = name self.expr = expr self.is_named = name != "" -class Annotation: +class Attribute: def __init__(self, name, args, pos): self.name = name self.args = args @@ -113,7 +113,7 @@ def find_arg(self, name): return arg return None -class Annotations: +class Attributes: def __init__(self): self.attributes = [] diff --git a/rivetc/src/codegen/__init__.py b/rivetc/src/codegen/__init__.py index ffc8580bb..dc9ee82de 100644 --- a/rivetc/src/codegen/__init__.py +++ b/rivetc/src/codegen/__init__.py @@ -61,21 +61,21 @@ def gen_source_files(self, source_files): # generate 'init_string_lits_fn' function self.init_string_lits_fn = ir.FuncDecl( - False, ast.Annotations(), False, "_R4core16init_string_litsF", [], + False, ast.Attributes(), False, "_R4core16init_string_litsF", [], False, ir.VOID_T, False ) self.out_rir.decls.append(self.init_string_lits_fn) # generate '_R4core12init_globalsF' function self.init_global_vars_fn = ir.FuncDecl( - False, ast.Annotations(), False, "_R4core12init_globalsF", [], + False, ast.Attributes(), False, "_R4core12init_globalsF", [], False, ir.VOID_T, False ) self.out_rir.decls.append(self.init_global_vars_fn) # generate '_R12drop_globalsZ' function g_fn = ir.FuncDecl( - False, ast.Annotations(), False, "_R4core12drop_globalsF", [], + False, ast.Attributes(), False, "_R4core12drop_globalsF", [], False, ir.VOID_T, False ) self.out_rir.decls.append(g_fn) @@ -88,7 +88,7 @@ def gen_source_files(self, source_files): argc = ir.Ident(ir.C_INT_T, "_argc") argv = ir.Ident(ir.CHAR_T.ptr().ptr(), "_argv") main_fn = ir.FuncDecl( - False, ast.Annotations(), False, "main", [argc, argv], False, + False, ast.Attributes(), False, "main", [argc, argv], False, ir.C_INT_T, False ) if self.comp.prefs.build_mode == prefs.BuildMode.Test: @@ -375,7 +375,7 @@ def gen_decl(self, decl): test_func = f"__test{len(self.generated_tests)}__" test_func = f"_R{len(test_func)}{test_func}" test_fn = ir.FuncDecl( - False, ast.Annotations(), False, test_func, + False, ast.Attributes(), False, test_func, [ir.Ident(ir.TEST_T.ptr(), "test")], False, ir.VOID_T, False ) self.cur_func = test_fn @@ -1907,7 +1907,7 @@ def gen_expr(self, expr, custom_tmp = None): self.ir_type(expr_left_typ), "_elem_" ) contains_decl = ir.FuncDecl( - False, ast.Annotations(), False, full_name, + False, ast.Attributes(), False, full_name, [self_idx_, elem_idx_], False, ir.BOOL_T, False ) else: @@ -1918,7 +1918,7 @@ def gen_expr(self, expr, custom_tmp = None): self.ir_type(expr_left_typ), "_elem_" ) contains_decl = ir.FuncDecl( - False, ast.Annotations(), False, full_name, [ + False, ast.Attributes(), False, full_name, [ self_idx_, ir.Ident(ir.UINT_T, "_len_"), elem_idx_ ], False, ir.BOOL_T, False @@ -3112,7 +3112,7 @@ def gen_types(self): ) ) index_of_vtbl_fn = ir.FuncDecl( - False, ast.Annotations(), False, + False, ast.Attributes(), False, cg_utils.mangle_symbol(ts) + "17__index_of_vtbl__", [ir.Ident(ir.UINT_T, "self")], False, ir.UINT_T, False ) diff --git a/rivetc/src/parser.py b/rivetc/src/parser.py index f37f9699b..0f54f30a4 100644 --- a/rivetc/src/parser.py +++ b/rivetc/src/parser.py @@ -152,8 +152,8 @@ def parse_doc_comment(self): def parse_attributes(self, parse_mod_attributes = False): if parse_mod_attributes and self.mod_sym.attributes == None: - self.mod_sym.attributes = ast.Annotations() - attributes = ast.Annotations() + self.mod_sym.attributes = ast.Attributes() + attributes = ast.Attributes() while self.accept(Kind.Hash): if parse_mod_attributes: self.expect(Kind.Bang) @@ -173,11 +173,11 @@ def parse_attributes(self, parse_mod_attributes = False): else: name = "" expr = self.parse_expr() - args.append(ast.AnnotationArg(name, expr)) + args.append(ast.AttributeArg(name, expr)) if not self.accept(Kind.Comma): break self.expect(Kind.Rparen) - attribute = ast.Annotation(attribute_name, args, pos) + attribute = ast.Attribute(attribute_name, args, pos) if parse_mod_attributes: self.mod_sym.attributes.add(attribute) else: @@ -208,19 +208,20 @@ def parse_decls(self): return decls def parse_decl(self): - doc_comment = self.parse_doc_comment() - attributes = self.parse_attributes( - self.tok.kind == Kind.Hash and self.peek_tok.kind == Kind.Bang - ) - is_public = self.is_public() - pos = self.tok.pos if self.accept(Kind.KwComptime): if self.tok.kind == Kind.KwIf: return self.parse_comptime_if(0) else: report.error("invalid comptime construction", self.tok.pos) return - elif self.accept(Kind.KwImport): + doc_comment = self.parse_doc_comment() + is_mod_attr = self.tok.kind == Kind.Hash and self.peek_tok.kind == Kind.Bang + attributes = self.parse_attributes(is_mod_attr) + if is_mod_attr: + return ast.EmptyDecl() + is_public = self.is_public() + pos = self.tok.pos + if self.accept(Kind.KwImport): path = self.parse_import_path() alias = "" import_list = []