#include "../include/ast.h" #include "../include/parser.h" #include "../include/compiler.h" #include "../include/std/log.h" #include "../include/std/hash.h" #include #include #define throw(result) do { throw_debug_msg; longjmp(context->env, (result)); } while(0) #define throw_if_error(result) \ do { \ int return_if_result; \ return_if_result = (result); \ if((return_if_result) != 0) \ throw(return_if_result); \ } while(0) static void ast_resolve(Ast *self, AstCompilerContext *context); static void resolve_data_init(AstResolveData *self) { self->status = AST_NOT_RESOLVED; self->type = NULL; } int ast_create(ScopedAllocator *allocator, void *value, AstType type, Ast **result) { return_if_error(scoped_allocator_alloc(allocator, sizeof(Ast), (void**)result)); (*result)->value.data = value; (*result)->type = type; resolve_data_init(&(*result)->resolve_data); (*result)->ssa_reg = 0; return 0; } static bool ast_is_decl(Ast *self) { /* TODO: Add more types as they are introduced */ return self->type == AST_FUNCTION_DECL || self->type == AST_STRUCT_DECL; } BufferView ast_get_name(Ast *self) { BufferView name; switch(self->type) { case AST_FUNCTION_DECL: case AST_STRUCT_DECL: case AST_IMPORT: case AST_STRING: case AST_BINOP: name = create_buffer_view_null(); break; case AST_NUMBER: name = self->value.number->code_ref; break; case AST_LHS: name = self->value.lhs_expr->var_name; break; case AST_FUNCTION_CALL: name = self->value.func_call->func.name; break; case AST_VARIABLE: name = self->value.variable->name; break; case AST_STRUCT_FIELD: name = self->value.struct_field->name; break; } return name; } static BufferView ast_get_code_reference(Ast *self) { return ast_get_name(self); } int funcdecl_init(FunctionDecl *self, Scope *parent, ScopedAllocator *allocator) { self->ssa_func_index = 0; return scope_init(&self->body, parent, allocator); } int funccall_init(FunctionCall *self, BufferView name, ScopedAllocator *allocator) { variable_init(&self->func, name); return buffer_init(&self->args, allocator); } int structdecl_init(StructDecl *self, Scope *parent, ScopedAllocator *allocator) { return scope_init(&self->body, parent, allocator); } void structfield_init(StructField *self, BufferView name, BufferView type_name) { self->name = name; variable_init(&self->type, type_name); } int lhsexpr_init(LhsExpr *self, bool is_pub, bool is_const, BufferView var_name, ScopedAllocator *allocator) { self->is_pub = is_pub; self->is_const = is_const; variable_init(&self->type, create_buffer_view_null()); self->var_name = var_name; self->rhs_expr = NULL; return_if_error(scoped_allocator_create_mutex(allocator, &self->mutex)); return 0; } void import_init(Import *self, BufferView path) { self->path = path; self->file_scope = NULL; } int string_init(String *self, BufferView str) { /* TODO: Convert special characters. For example \n should be converted to binary newline etc */ self->str = str; return 0; } void number_init(Number *self, i64 value, bool is_integer, BufferView code_ref) { self->value.integer = value; self->is_integer = is_integer; self->code_ref = code_ref; } void variable_init(Variable *self, BufferView name) { self->name = name; } void binop_init(Binop *self) { self->lhs = NULL; self->rhs = NULL; self->type = BINOP_ADD; self->grouped = bool_false; } int scope_init(Scope *self, Scope *parent, ScopedAllocator *allocator) { return_if_error(buffer_init(&self->ast_objects, allocator)); return_if_error(hash_map_init(&self->named_objects, allocator, sizeof(Ast*), hash_compare_string, amal_hash_string)); self->parent = parent; self->parser = NULL; return 0; } int file_scope_reference_init(FileScopeReference *self, BufferView canonical_path, ScopedAllocator *allocator) { char null_terminator; null_terminator = '\0'; self->parser = NULL; return_if_error(buffer_init(&self->canonical_path, allocator)); return_if_error(buffer_append(&self->canonical_path, canonical_path.data, canonical_path.size)); return_if_error(buffer_append(&self->canonical_path, &null_terminator, 1)); /* To exclude null-terminator character from size but not from data */ self->canonical_path.size -= 1; return 0; } int scope_add_child(Scope *self, Ast *child) { Ast *existing_child; bool child_already_exists; /* TODO: Implement for parameter */ if(child->type == AST_LHS) { BufferView var_name; var_name = child->value.lhs_expr->var_name; assert(var_name.data); child_already_exists = hash_map_get(&self->named_objects, var_name, &existing_child); if(child_already_exists) return AST_ERR_DEF_DUP; cleanup_if_error(hash_map_insert(&self->named_objects, var_name, &child)); } cleanup_if_error(buffer_append(&self->ast_objects, &child, sizeof(Ast*))); return 0; cleanup: return AST_ERR; } void scope_resolve(Scope *self, AstCompilerContext *context) { Ast **ast; Ast **ast_end; ast = buffer_begin(&self->ast_objects); ast_end = buffer_end(&self->ast_objects); context->scope = self; for(; ast != ast_end; ++ast) { ast_resolve(*ast, context); } context->scope = self->parent; } static Parser* scope_get_parser(Scope *scope) { while(scope) { if(scope->parser) return scope->parser; scope = scope->parent; } return NULL; } static void parser_print_error(Parser *parser, const char *ref, const char *fmt, ...) { va_list args; va_start(args, fmt); if(parser) { tokenizer_print_error(&parser->tokenizer, tokenizer_get_code_reference_index(&parser->tokenizer, ref), fmt, args); } else { /* TODO: Redirect error to compiler error callback if set */ amal_log_error(fmt, args); } va_end(args); } static Ast* scope_get_resolved_variable(Scope *self, AstCompilerContext *context, BufferView name) { Ast *result; bool exists; Scope *prev_scope; assert(self); exists = hash_map_get(&self->named_objects, name, &result); if(!exists) { Parser *parser; if(self->parent) return scope_get_resolved_variable(self->parent, context, name); parser = scope_get_parser(self); parser_print_error(parser, name.data, "Undefined reference to variable \"%.*s\"", name.size, name.data); throw(AST_ERR); } /* Need to change scope here because we are changing the visible scope and the ast object may be in another scope than the current resolving ast. */ prev_scope = context->scope; context->scope = self; ast_resolve(result, context); context->scope = prev_scope; assert(result->type == AST_LHS); return result; } static void variable_resolve(Variable *self, AstCompilerContext *context, AstResolveData *resolve_data) { if(!resolve_data->type) resolve_data->type = scope_get_resolved_variable(context->scope, context, self->name)->resolve_data.type; } static LhsExpr* lhsexpr_resolve_rhs(LhsExpr *self, AstCompilerContext *context) { LhsExpr *rhs_resolved_type; ast_resolve(self->rhs_expr, context); if(ast_is_decl(self->rhs_expr)) rhs_resolved_type = self; else rhs_resolved_type = self->rhs_expr->resolve_data.type; return rhs_resolved_type; } static void lhsexpr_resolve(Ast *ast, AstCompilerContext *context) { LhsExpr *self; LhsExpr *rhs_resolve_type; assert(ast->type == AST_LHS); self = ast->value.lhs_expr; rhs_resolve_type = NULL; if(self->type.name.data) variable_resolve(&self->type, context, &ast->resolve_data); /* TODO: When parameters and return types are implemented, AST_RESOLVE_END should be set after the parameters and return types have been resolved as recursive function calls should be allowed but recursive function calls still require parameters and return types to be known. */ if(self->rhs_expr) { if(self->rhs_expr->type == AST_FUNCTION_DECL) ast->resolve_data.status = AST_RESOLVED; rhs_resolve_type = lhsexpr_resolve_rhs(self, context); /* self->rhs_expr can be null here because this is valid: var num: i32; */ if(ast->resolve_data.type && self->rhs_expr && ast->resolve_data.type != rhs_resolve_type) { Parser *parser; parser = scope_get_parser(context->scope); parser_print_error(parser, self->type.name.data, "Variable type and variable assignment type (right-hand side) do not match"); throw(AST_ERR); } } if(rhs_resolve_type) ast->resolve_data.type = rhs_resolve_type; } static void import_resolve(Ast *ast, AstCompilerContext *context) { Import *self; assert(ast->type == AST_IMPORT); (void)context; self = ast->value.import; ast->resolve_data.type = &self->file_scope->parser->file_decl; assert(ast->resolve_data.type); } static Scope* lhsexpr_get_scope(LhsExpr *self) { AstValue value; value = self->rhs_expr->value; switch(self->rhs_expr->type) { case AST_FUNCTION_DECL: return &value.func_decl->body; case AST_STRUCT_DECL: return &value.struct_decl->body; case AST_IMPORT: return &value.import->file_scope->parser->struct_decl.body; default: break; } assert(bool_false && "Expected lhsexpr_get_scope to only be called for function decl, struct decl and import"); return NULL; } /* @self has to have a resolved type before calling this */ static Parser* get_resolved_type_parser(Ast *self) { assert(self->resolve_data.type); return scope_get_parser(lhsexpr_get_scope(self->resolve_data.type)); } static void funcdecl_resolve(FunctionDecl *self, AstCompilerContext *context) { /* TODO: Implement parameters and return types */ scope_resolve(&self->body, context); } static void funccall_resolve(Ast *self, AstCompilerContext *context) { FunctionCall *func_call; Ast **ast; Ast **ast_end; func_call = self->value.func_call; variable_resolve(&func_call->func, context, &self->resolve_data); if(self->resolve_data.type->rhs_expr->type != AST_FUNCTION_DECL) { Parser *caller_parser; Parser *callee_parser; BufferView callee_code_ref; caller_parser = scope_get_parser(context->scope); callee_parser = get_resolved_type_parser(self); callee_code_ref = self->resolve_data.type->var_name; parser_print_error(caller_parser, func_call->func.name.data, "\"%.*s\" is not a function. Only functions can be called", func_call->func.name.size, func_call->func.name.data); /* TODO: use tokenizer_print_note, once it has been added */ /* TODO: Print type */ parser_print_error(callee_parser, callee_code_ref.data, "Type was declared here"); throw(AST_ERR); } ast = buffer_begin(&func_call->args); ast_end = buffer_end(&func_call->args); for(; ast != ast_end; ++ast) { ast_resolve(*ast, context); } } static void structdecl_resolve(Ast *self, AstCompilerContext *context) { StructDecl *struct_decl; struct_decl = self->value.struct_decl; scope_resolve(&struct_decl->body, context); } static void structfield_resolve(Ast *self, AstCompilerContext *context) { /* TODO: Implement */ StructField *struct_field; struct_field = self->value.struct_field; variable_resolve(&struct_field->type, context, &self->resolve_data); } static void binop_resolve_dot_access(Ast *ast, AstCompilerContext *context) { Binop *self; Scope *lhs_scope; Parser *caller_parser; Parser *callee_parser; BufferView caller_code_ref; BufferView callee_code_ref; assert(ast->type == AST_BINOP); self = ast->value.binop; caller_parser = scope_get_parser(context->scope); if(self->lhs->type != AST_VARIABLE) { /* TODO: Allow field access for numbers and string as well */ BufferView code_ref; code_ref = ast_get_code_reference(self->lhs); parser_print_error(caller_parser, code_ref.data, "Accessing fields is only applicable for variables"); throw(AST_ERR); } lhs_scope = lhsexpr_get_scope(self->lhs->resolve_data.type); self->rhs->resolve_data.type = scope_get_resolved_variable(lhs_scope, context, self->rhs->value.variable->name)->resolve_data.type; callee_parser = get_resolved_type_parser(self->rhs); caller_code_ref = ast_get_code_reference(self->rhs); callee_code_ref = self->rhs->resolve_data.type->var_name; if(self->lhs->resolve_data.type->rhs_expr->type != AST_STRUCT_DECL) { parser_print_error(caller_parser, caller_code_ref.data, "Can only access field of structs"); /* TODO: use tokenizer_print_note, once it has been added */ /* TODO: Print type */ parser_print_error(callee_parser, callee_code_ref.data, "Type was declared here"); throw(AST_ERR); } if(!self->rhs->resolve_data.type->is_pub) { parser_print_error(caller_parser, caller_code_ref.data, "Can't access non-public field \"%.*s\"", caller_code_ref.size, caller_code_ref.data); /* TODO: use tokenizer_print_note, once it has been added */ /* TODO: Print type */ parser_print_error(callee_parser, callee_code_ref.data, "Type was declared non-public here"); throw(AST_ERR); } } static void binop_resolve(Ast *ast, AstCompilerContext *context) { Binop *self; assert(ast->type == AST_BINOP); self = ast->value.binop; ast_resolve(self->lhs, context); if(self->type == BINOP_DOT && (self->rhs->type == AST_VARIABLE || self->rhs->type == AST_FUNCTION_CALL)) { binop_resolve_dot_access(ast, context); /* Only function call has extra data that needs to be resolved (args) */ if(self->rhs->type == AST_FUNCTION_CALL) ast_resolve(self->rhs, context); self->rhs->resolve_data.status = AST_RESOLVED; ast->resolve_data.type = self->rhs->resolve_data.type; } else { ast_resolve(self->rhs, context); /* TODO: Convert types that can be safely converted */ assert(self->lhs->resolve_data.type); assert(self->rhs->resolve_data.type); if(self->rhs->resolve_data.type != self->lhs->resolve_data.type) { /* TODO: For this first error, only print the line without a reference to code. This requires change in tokenizer_print_error to be able to take a line as reference. */ Parser *parser; parser = scope_get_parser(context->scope); parser_print_error(parser, ast_get_code_reference(self->rhs).data, "Can't cast type \"%.*s\" to type \"%.*s\"", self->rhs->resolve_data.type->var_name.size, self->rhs->resolve_data.type->var_name.data, self->lhs->resolve_data.type->var_name.size, self->lhs->resolve_data.type->var_name.data); parser_print_error(parser, ast_get_code_reference(self->lhs).data, "Left-hand side is of type %.*s", self->lhs->resolve_data.type->var_name.size, self->lhs->resolve_data.type->var_name.data); parser_print_error(parser, ast_get_code_reference(self->rhs).data, "Right-hand side is of type %.*s", self->rhs->resolve_data.type->var_name.size, self->rhs->resolve_data.type->var_name.data); throw(AST_ERR); } ast->resolve_data.type = self->lhs->resolve_data.type; } } void ast_resolve(Ast *self, AstCompilerContext *context) { assert(self); /* TODO: Move these to the types that need checks for recursive dependency (function declaration, struct declaration) For function declaration, it should be marked as resolved when the signature has been resolved instead of the whole function declaration including the body because the body can have function call that calls functions that are resolving or even recursive function call, which should be allowed. */ /* This check is outside lhs_expr mutex for optimization purpose as most times there wont be a race in multiple threads to resolve an AST expression. */ if(self->resolve_data.status == AST_RESOLVED) { return; } else if(self->resolve_data.status == AST_RESOLVING) { Parser *parser; parser = scope_get_parser(context->scope); parser_print_error(parser, ast_get_code_reference(self).data, "Found recursive dependency"); throw(AST_ERR); } if(self->type == AST_LHS) { throw_if_error(amal_mutex_lock(self->value.lhs_expr->mutex, "ast_resolve")); if(self->resolve_data.status == AST_RESOLVED) { amal_mutex_tryunlock(self->value.lhs_expr->mutex); return; } else if(self->resolve_data.status == AST_RESOLVING) { Parser *parser; parser = scope_get_parser(context->scope); amal_mutex_tryunlock(self->value.lhs_expr->mutex); parser_print_error(parser, ast_get_code_reference(self).data, "Found recursive dependency"); throw(AST_ERR); } } self->resolve_data.status = AST_RESOLVING; switch(self->type) { case AST_NUMBER: { Number *number; number = self->value.number; /* TODO: Support other number types */ if(number->is_integer) self->resolve_data.type = context->compiler->default_types.i64; else self->resolve_data.type = context->compiler->default_types.f64; break; } case AST_FUNCTION_DECL: funcdecl_resolve(self->value.func_decl, context); break; case AST_FUNCTION_CALL: funccall_resolve(self, context); break; case AST_STRUCT_DECL: structdecl_resolve(self, context); break; case AST_STRUCT_FIELD: structfield_resolve(self, context); break; case AST_LHS: lhsexpr_resolve(self, context); break; case AST_IMPORT: /* TODO: When @import(...).data syntax is added, implement the resolve for it */ import_resolve(self, context); break; case AST_STRING: /* TODO: Convert special combinations. For example \n to newline */ self->resolve_data.type = context->compiler->default_types.str; break; case AST_VARIABLE: variable_resolve(self->value.variable, context, &self->resolve_data); break; case AST_BINOP: binop_resolve(self, context); break; } /* TODO: See comment at the top of this function */ self->resolve_data.status = AST_RESOLVED; if(self->type == AST_LHS) amal_mutex_tryunlock(self->value.lhs_expr->mutex); }