File: resolve.ha

package info (click to toggle)
hare 0.26.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 7,352 kB
  • sloc: asm: 1,374; makefile: 123; sh: 117; lisp: 101
file content (192 lines) | stat: -rw-r--r-- 4,152 bytes parent folder | download
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// SPDX-License-Identifier: GPL-3.0-only
// (c) Hare authors <https://harelang.org>

use bufio;
use fmt;
use fs;
use hare::ast;
use hare::lex;
use hare::module;
use hare::parse;
use io;
use os;

type symkind = enum {
	LOCAL,
	MODULE,
	SYMBOL,
	ENUM_LOCAL,
	ENUM_REMOTE,
};

// Resolves a reference. Given an identifier, determines if it refers to a local
// symbol, a module, or a symbol in a remote module, then returns this
// information combined with a corrected ident if necessary.
fn resolve(ctx: *context, what: ast::ident) ((ast::ident, symkind) | void | error) = {
	if (is_local(ctx, what)) {
		return (what, symkind::LOCAL);
	};

	if (len(what) > 1) {
		// Look for symbol in remote module
		let partial = what[..len(what) - 1];

		match (module::find(ctx.mctx, partial)) {
		case let r: (str, module::srcset) =>
			module::finish_srcset(&r.1);
			return (what, symkind::SYMBOL);
		case module::error => void;
		};
	};
	if (len(what) == 2) {
		match (lookup_local_enum(ctx, what)) {
		case let id: ast::ident =>
			return (id, symkind::ENUM_LOCAL);
		case => void;
		};
	};
	if (len(what) > 2) {
		match (lookup_remote_enum(ctx, what)?) {
		case let id: ast::ident =>
			return (id, symkind::ENUM_REMOTE);
		case => void;
		};
	};

	match (module::find(ctx.mctx, what)) {
	case let r: (str, module::srcset) =>
		module::finish_srcset(&r.1);
		return (what, symkind::MODULE);
	case module::error => void;
	};

	return;
};

fn is_local(ctx: *context, what: ast::ident) bool = {
	if (len(what) != 1) {
		return false;
	};

	const summary = ctx.summary;
	for (let c &.. summary.constants) {
		const name = decl_ident(c)[0];
		if (name == what[0]) {
			return true;
		};
	};
	for (let e &.. summary.errors) {
		const name = decl_ident(e)[0];
		if (name == what[0]) {
			return true;
		};
	};
	for (let t &.. summary.types) {
		const name = decl_ident(t)[0];
		if (name == what[0]) {
			return true;
		};
	};
	for (let g &.. summary.globals) {
		const name = decl_ident(g)[0];
		if (name == what[0]) {
			return true;
		};
	};
	for (let f &.. summary.funcs) {
		const name = decl_ident(f)[0];
		if (name == what[0]) {
			return true;
		};
	};

	return false;
};

fn lookup_local_enum(ctx: *context, what: ast::ident) (ast::ident | void) = {
	for (let decl &.. ctx.summary.types) {
		const name = decl_ident(decl)[0];
		if (name == what[0]) {
			const t = decl.decl as ast::decl_type;
			const e = match (t._type.repr) {
			case let e: ast::enum_type =>
				yield e;
			case =>
				return;
			};
			for (let value .. e.values) {
				if (value.name == what[1]) {
					return what;
				};
			};
		};
	};
};

fn lookup_remote_enum(ctx: *context, what: ast::ident) (ast::ident | void | error) = {
	// mod::decl_name::member
	const mod = what[..len(what) - 2];
	const decl_name = what[len(what) - 2];
	const member = what[len(what) - 1];

	const srcs = match (module::find(ctx.mctx, mod)) {
	case let s: (str, module::srcset) =>
		yield s.1;
	case let e: module::error =>
		module::finish_error(e);
		return void;
	};

	// This would take a lot of memory to load
	let decls: []ast::decl = [];
	defer {
		for (let decl .. decls) {
			ast::decl_finish(decl);
		};
		free(decls);
	};
	for (let in .. srcs.ha) {
		let d = scan(in)?;
		defer free(d);
		append(decls, d...)!;
	};

	for (let decl .. decls) {
		const d = match (decl.decl) {
		case let t: ast::decl_type =>
			yield t;
		case =>
			continue;
		};
		if (d.ident[0] == decl_name) {
			const e = match (d._type.repr) {
			case let e: ast::enum_type =>
				yield e;
			case =>
				abort();
			};
			for (let value .. e.values) {
				if (value.name == member) {
					return what;
				};
			};
		};
	};
};

export fn scan(path: str) ([]ast::decl | parse::error) = {
	const input = match (os::open(path)) {
	case let f: io::file =>
		yield f;
	case let err: fs::error =>
		fmt::fatalf("Error reading {}: {}", path, fs::strerror(err));
	};
	defer io::close(input)!;
	let sc = bufio::newscanner(input);
	defer bufio::finish(&sc);
	let lexer = lex::init(&sc, path, lex::flag::COMMENTS);
	defer lex::finish(&lexer);
	let su = parse::subunit(&lexer)?;
	ast::imports_finish(su.imports);
	return su.decls;
};