File: decl.ha

package info (click to toggle)
hare-update 0.26.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,044 kB
  • sloc: makefile: 37; sh: 14
file content (112 lines) | stat: -rw-r--r-- 2,198 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// SPDX-License-Identifier: MPL-2.0
// (c) Hare authors <https://harelang.org>

use common;

// A constant declaration.
//
// 	def foo: int = 0;
export type decl_const = struct {
	ident: ident,
	_type: nullable *_type,
	init: *expr,
};

// A global declaration.
//
// 	let foo: int = 0;
// 	const foo: int = 0;
export type decl_global = struct {
	is_const: bool,
	is_threadlocal: bool,
	symbol: str,
	ident: ident,
	_type: nullable *_type,
	init: nullable *expr,
	start: common::location,
	end: common::location,
};

// A type declaration.
//
// 	type foo = int;
export type decl_type = struct {
	ident: ident,
	_type: *_type,
};

// Attributes applicable to a function declaration.
export type fndecl_attr = enum {
	NONE,
	FINI,
	INIT,
	TEST,
};

// A function declaration.
//
// fn main() void = void;
export type decl_func = struct {
	symbol: str,
	ident: ident,
	prototype: *_type,
	body: nullable *expr,
	attrs: fndecl_attr,
};

// A Hare declaration.
export type decl = struct {
	exported: bool,
	start: common::location,
	end: common::location,
	decl: ([]decl_const | []decl_global | []decl_type | decl_func |
		assert_expr),

	// Only valid if the lexer has comments enabled
	docs: str,
};

// Frees resources associated with a declaration.
export fn decl_finish(d: decl) void = {
	free(d.docs);
	match (d.decl) {
	case let g: []decl_global =>
		for (let i = 0z; i < len(g); i += 1) {
			free(g[i].symbol);
			ident_free(g[i].ident);
			type_finish(g[i]._type);
			free(g[i]._type);
			expr_finish(g[i].init);
			free(g[i].init);
		};
		free(g);
	case let t: []decl_type =>
		for (let i = 0z; i < len(t); i += 1) {
			ident_free(t[i].ident);
			type_finish(t[i]._type);
			free(t[i]._type);
		};
		free(t);
	case let f: decl_func =>
		free(f.symbol);
		ident_free(f.ident);
		type_finish(f.prototype);
		free(f.prototype);
		expr_finish(f.body);
		free(f.body);
	case let c: []decl_const =>
		for (let i = 0z; i < len(c); i += 1) {
			ident_free(c[i].ident);
			type_finish(c[i]._type);
			free(c[i]._type);
			expr_finish(c[i].init);
			free(c[i].init);
		};
		free(c);
	case let e: assert_expr =>
		expr_finish(e.cond);
		free(e.cond);
		expr_finish(e.message);
		free(e.message);
	};
};