浏览代码

Added checking for params and return values in `main`

Zachary Pierson 8 年之前
父节点
当前提交
77b3295de5
共有 1 个文件被更改,包括 35 次插入18 次删除
  1. 35 18
      src/check_decl.c

+ 35 - 18
src/check_decl.c

@@ -5,8 +5,8 @@ void check_stmt_list     (Checker *c, AstNodeArray stmts, u32 flags);
 // NOTE(bill): `content_name` is for debugging and error messages
 Type *check_init_variable(Checker *c, Entity *e, Operand *operand, String context_name) {
 	if (operand->mode == Addressing_Invalid ||
-	    operand->type == t_invalid ||
-	    e->type == t_invalid) {
+		operand->type == t_invalid ||
+		e->type == t_invalid) {
 
 		if (operand->mode == Addressing_Builtin) {
 			gbString expr_str = expr_to_string(operand->expr);
@@ -14,9 +14,9 @@ Type *check_init_variable(Checker *c, Entity *e, Operand *operand, String contex
 			// TODO(bill): is this a good enough error message?
 			// TODO(bill): Actually allow built in procedures to be passed around and thus be created on use
 			error_node(operand->expr,
-			      "Cannot assign builtin procedure `%s` in %.*s",
-			      expr_str,
-			      LIT(context_name));
+				  "Cannot assign builtin procedure `%s` in %.*s",
+				  expr_str,
+				  LIT(context_name));
 
 			operand->mode = Addressing_Invalid;
 
@@ -86,8 +86,8 @@ void check_init_variables(Checker *c, Entity **lhs, isize lhs_count, AstNodeArra
 
 void check_init_constant(Checker *c, Entity *e, Operand *operand) {
 	if (operand->mode == Addressing_Invalid ||
-	    operand->type == t_invalid ||
-	    e->type == t_invalid) {
+		operand->type == t_invalid ||
+		e->type == t_invalid) {
 		if (e->type == NULL) {
 			e->type = t_invalid;
 		}
@@ -182,7 +182,7 @@ void check_const_decl(Checker *c, Entity *e, AstNode *type_expr, AstNode *init,
 	check_init_constant(c, e, &operand);
 
 	if (operand.mode == Addressing_Invalid ||
-	    base_type(operand.type) == t_invalid) {
+		base_type(operand.type) == t_invalid) {
 		error(e->token, "Invalid declaration type");
 	}
 }
@@ -249,13 +249,30 @@ void check_proc_lit(Checker *c, Entity *e, DeclInfo *d) {
 	bool is_no_inline    = (pd->tags & ProcTag_no_inline) != 0;
 
 	if ((d->scope->is_file || d->scope->is_global) &&
-	    str_eq(e->token.string, str_lit("main"))) {
+		str_eq(e->token.string, str_lit("main"))) {
 		if (proc_type != NULL) {
 			TypeProc *pt = &proc_type->Proc;
-			if (pt->param_count != 0 ||
-			    pt->result_count != 0) {
+
+			// This is an ugly monstrosity, but I see no other way.
+			bool valid_param = pt->param_count == 0 || (pt->params->kind == Type_Tuple &&
+			                                            pt->params->Tuple.variable_count == 2 &&
+			                                            pt->params->Tuple.variables[0]->kind == Entity_Variable &&
+			                                            pt->params->Tuple.variables[0]->type->kind == Type_Basic &&
+			                                            pt->params->Tuple.variables[0]->type->Basic.kind == Basic_i32 &&
+			                                            pt->params->Tuple.variables[1]->kind == Entity_Variable &&
+			                                            pt->params->Tuple.variables[1]->type->kind == Type_Slice &&
+			                                            pt->params->Tuple.variables[1]->type->Slice.elem->kind == Type_Pointer &&
+			                                            pt->params->Tuple.variables[1]->type->Slice.elem->Pointer.elem->kind == Type_Basic &&
+			                                            pt->params->Tuple.variables[1]->type->Slice.elem->Pointer.elem->Basic.kind == Basic_u8);
+
+			bool valid_result = pt->result_count == 0 || (pt->results->kind == Type_Tuple &&
+			                                              pt->results->Tuple.variable_count == 1 &&
+			                                              pt->results->Tuple.variables[0]->kind == Entity_Variable &&
+			                                              pt->results->Tuple.variables[0]->type->kind == Type_Basic &&
+			                                              pt->results->Tuple.variables[0]->type->Basic.kind == Basic_i32);
+			if (!valid_param || !valid_result) {
 				gbString str = type_to_string(proc_type);
-				error(e->token, "Procedure type of `main` was expected to be `proc()`, got %s", str);
+				error(e->token, "Procedure type of `main` was expected to be `proc()` or `proc(i32, []^byte) -> i32`, got %s", str);
 				gb_string_free(str);
 			}
 		}
@@ -325,9 +342,9 @@ void check_proc_lit(Checker *c, Entity *e, DeclInfo *d) {
 			Type *other_type = base_type(f->type);
 			if (!are_signatures_similar_enough(this_type, other_type)) {
 				error_node(d->proc_lit,
-				           "Redeclaration of #foreign procedure `%.*s` with different type signatures\n"
-				           "\tat %.*s(%td:%td)",
-				           LIT(name), LIT(pos.file), pos.line, pos.column);
+						   "Redeclaration of #foreign procedure `%.*s` with different type signatures\n"
+						   "\tat %.*s(%td:%td)",
+						   LIT(name), LIT(pos.file), pos.line, pos.column);
 			}
 		} else {
 			map_entity_set(fp, key, e);
@@ -350,9 +367,9 @@ void check_proc_lit(Checker *c, Entity *e, DeclInfo *d) {
 				TokenPos pos = f->token.pos;
 				// TODO(bill): Better error message?
 				error_node(d->proc_lit,
-				           "Non unique linking name for procedure `%.*s`\n"
-				           "\tother at %.*s(%td:%td)",
-				           LIT(name), LIT(pos.file), pos.line, pos.column);
+						   "Non unique linking name for procedure `%.*s`\n"
+						   "\tother at %.*s(%td:%td)",
+						   LIT(name), LIT(pos.file), pos.line, pos.column);
 			} else {
 				map_entity_set(fp, key, e);
 			}