1 #include "analysis/type_precheck.h"
2 #include "ast/ast_base.h"
3 #include "ast/ast_node_type.h"
4 #include "common/ast_visitor.h"
8 #include "ast/package.h"
10 #include "ast/intrinsic.h"
11 #include "ast/context.h"
12 #include "source_file/token.h"
13 #include "driver/driver.h"
18 void TypePrecheck::default_visit(ASTBase *) { TAN_ASSERT(
false); }
20 void TypePrecheck::run_impl(Package *p) {
25 for (
const auto &c : p->get_children()) {
26 switch (c->get_node_type()) {
27 case ASTNodeType::IMPORT:
28 CALL_AST_VISITOR(Import, c);
30 case ASTNodeType::STRUCT_DECL:
31 CALL_AST_VISITOR(StructDecl, c);
33 case ASTNodeType::FUNC_DECL:
34 CALL_AST_VISITOR(FunctionDecl, c);
36 case ASTNodeType::INTRINSIC:
37 CALL_AST_VISITOR(Intrinsic, c);
47 Type *TypePrecheck::check_type_ref(Type *p, ASTBase *node) {
48 TAN_ASSERT(p->is_ref());
51 const str &referred_name = p->get_typename();
52 auto *decl = search_decl_in_scopes(referred_name);
53 if (decl && decl->is_type_decl()) {
54 ret = decl->get_type();
56 if (!ret->is_canonical()) {
57 _package->top_level_symbol_dependency.add_dependency(decl, node);
60 error(ErrorType::TYPE_ERROR, node, fmt::format(
"Unknown type {}", referred_name));
66 Type *TypePrecheck::check_type(Type *p, ASTBase *node) {
72 ret = check_type_ref(p, node);
73 }
else if (p->is_pointer()) {
74 auto *pointee = pcast<PointerType>(p)->get_pointee();
77 if (pointee->is_ref()) {
78 pointee = check_type_ref(pointee, node);
79 if (pointee->is_canonical()) {
80 ret = Type::GetPointerType(pointee);
89 DEFINE_AST_VISITOR_IMPL(TypePrecheck, Import) {
90 str name = p->get_name();
92 auto *compiler = CompilerDriver::instance();
94 Package *
package = compiler->get_package(name);
96 error(ErrorType::IMPORT_ERROR, p,
"Cannot find package named: " + name);
99 Context *imported_ctx = package->ctx();
106 vector<FunctionDecl *> funcs = imported_ctx->get_func_decls();
107 for (
auto *f : funcs) {
108 f->set_start(p->start());
109 f->set_end(p->end());
111 if (f->is_public() || f->is_external()) {
114 auto *existing = top_ctx()->get_func_decl(f->get_name());
116 p->_imported_funcs.push_back(f);
117 top_ctx()->set_function_decl(f);
123 vector<Decl *> decls = imported_ctx->get_decls();
124 for (
auto *t : decls) {
125 if (t->is_type_decl() && t->is_public()) {
126 top_ctx()->set_decl(t->get_name(), t);
127 p->_imported_types.push_back(pcast<TypeDecl>(t));
149 DEFINE_AST_VISITOR_IMPL(TypePrecheck, Intrinsic) {
151 if (p->get_intrinsic_type() == IntrinsicType::TEST_COMP_ERROR) {
152 auto *tce = pcast<TestCompError>(p->get_sub());
159 for (
auto *c : tce->get_children())
161 }
catch (
const CompileException &e) {
162 std::cerr << fmt::format(
"Caught expected compile error: {}\nContinue compilation...\n", e.what());
170 DEFINE_AST_VISITOR_IMPL(TypePrecheck, VarDecl) {
171 Type *ty = p->get_type();
173 p->set_type(check_type(ty, p));
177 DEFINE_AST_VISITOR_IMPL(TypePrecheck, ArgDecl) { p->set_type(check_type(p->get_type(), p)); }
179 DEFINE_AST_VISITOR_IMPL(TypePrecheck, Assignment) {
180 auto *lhs = p->get_lhs();
184 if (lhs->get_node_type() == ASTNodeType::VAR_DECL) {
185 p->set_type(pcast<Decl>(lhs)->get_type());
189 DEFINE_AST_VISITOR_IMPL(TypePrecheck, FunctionDecl) {
193 auto *func_type = pcast<FunctionType>(p->get_type());
194 auto *ret_type = check_type(func_type->get_return_type(), p);
195 func_type->set_return_type(ret_type);
198 size_t n = p->get_n_args();
199 const auto &arg_decls = p->get_arg_decls();
200 vector<Type *> arg_types(n,
nullptr);
201 for (
size_t i = 0; i < n; ++i) {
203 arg_types[i] = arg_decls[i]->get_type();
205 func_type->set_arg_types(arg_types);
210 DEFINE_AST_VISITOR_IMPL(TypePrecheck, StructDecl) {
211 str struct_name = p->get_name();
213 auto members = p->get_member_decls();
214 size_t n = members.size();
215 auto *ty = pcast<StructType>(p->get_type());
220 for (
size_t i = 0; i < n; ++i) {
221 Expr *m = members[i];
224 switch (m->get_node_type()) {
225 case ASTNodeType::VAR_DECL:
226 case ASTNodeType::FUNC_DECL:
228 case ASTNodeType::ASSIGN: {
229 auto *assign = pcast<Assignment>(m);
230 if (assign->get_lhs()->get_node_type() != ASTNodeType::VAR_DECL)
231 error(ErrorType::SEMANTIC_ERROR, assign,
"Expect a member variable declaration");
235 error(ErrorType::SEMANTIC_ERROR, p,
"Invalid struct member");
250 if (!m->get_type()->is_canonical() && !m->get_type()->is_pointer()) {
251 _package->top_level_symbol_dependency.add_dependency(m, p);
254 if (m->get_node_type() == ASTNodeType::VAR_DECL) {
255 str name = pcast<VarDecl>(m)->get_name();
256 p->set_member_index(name, (
int)i);
257 (*ty)[i] = m->get_type();
259 }
else if (m->get_node_type() == ASTNodeType::ASSIGN) {
260 auto *assign = pcast<Assignment>(m);
261 auto decl = pcast<VarDecl>(assign->get_lhs());
263 (*ty)[i] = decl->get_type();
266 p->set_member_index(decl->get_name(), (
int)i);
269 auto *rhs = assign->get_rhs();
270 if (!rhs->is_comptime_known()) {
271 error(ErrorType::SEMANTIC_ERROR, rhs,
"Expect the value to be compile time known");
273 p->set_member_default_val((
int)i, rhs);
275 }
else if (m->get_node_type() == ASTNodeType::FUNC_DECL) {
276 auto f = pcast<FunctionDecl>(m);
278 (*ty)[i] = f->get_type();
279 p->set_member_index(f->get_name(), (
int)i);