Add #load(path) or_else default in favour of #load_or(path, default)

This commit is contained in:
gingerBill
2022-08-11 13:01:54 +01:00
parent 0997df4fcf
commit 38102f14c1
4 changed files with 171 additions and 81 deletions
+49 -30
View File
@@ -1074,32 +1074,12 @@ bool check_builtin_simd_operation(CheckerContext *c, Operand *operand, Ast *call
return false; return false;
} }
LoadDirectiveResult check_load_directive(CheckerContext *c, Operand *operand, Ast *call, Type *type_hint, bool err_on_not_found) {
bool check_builtin_procedure_directive(CheckerContext *c, Operand *operand, Ast *call, i32 id, Type *type_hint) {
ast_node(ce, CallExpr, call); ast_node(ce, CallExpr, call);
ast_node(bd, BasicDirective, ce->proc); ast_node(bd, BasicDirective, ce->proc);
String name = bd->name.string; String name = bd->name.string;
if (name == "location") { GB_ASSERT(name == "load");
if (ce->args.count > 1) {
error(ce->args[0], "'#location' expects either 0 or 1 arguments, got %td", ce->args.count);
}
if (ce->args.count > 0) {
Ast *arg = ce->args[0];
Entity *e = nullptr;
Operand o = {};
if (arg->kind == Ast_Ident) {
e = check_ident(c, &o, arg, nullptr, nullptr, true);
} else if (arg->kind == Ast_SelectorExpr) {
e = check_selector(c, &o, arg, nullptr);
}
if (e == nullptr) {
error(ce->args[0], "'#location' expected a valid entity name");
}
}
operand->type = t_source_code_location;
operand->mode = Addressing_Value;
} else if (name == "load") {
if (ce->args.count != 1) { if (ce->args.count != 1) {
if (ce->args.count == 0) { if (ce->args.count == 0) {
error(ce->close, "'#load' expects 1 argument, got 0"); error(ce->close, "'#load' expects 1 argument, got 0");
@@ -1107,7 +1087,7 @@ bool check_builtin_procedure_directive(CheckerContext *c, Operand *operand, Ast
error(ce->args[0], "'#load' expects 1 argument, got %td", ce->args.count); error(ce->args[0], "'#load' expects 1 argument, got %td", ce->args.count);
} }
return false; return LoadDirective_Error;
} }
Ast *arg = ce->args[0]; Ast *arg = ce->args[0];
@@ -1115,14 +1095,14 @@ bool check_builtin_procedure_directive(CheckerContext *c, Operand *operand, Ast
check_expr(c, &o, arg); check_expr(c, &o, arg);
if (o.mode != Addressing_Constant) { if (o.mode != Addressing_Constant) {
error(arg, "'#load' expected a constant string argument"); error(arg, "'#load' expected a constant string argument");
return false; return LoadDirective_Error;
} }
if (!is_type_string(o.type)) { if (!is_type_string(o.type)) {
gbString str = type_to_string(o.type); gbString str = type_to_string(o.type);
error(arg, "'#load' expected a constant string, got %s", str); error(arg, "'#load' expected a constant string, got %s", str);
gb_string_free(str); gb_string_free(str);
return false; return LoadDirective_Error;
} }
gbAllocator a = heap_allocator(); gbAllocator a = heap_allocator();
@@ -1148,14 +1128,23 @@ bool check_builtin_procedure_directive(CheckerContext *c, Operand *operand, Ast
switch (file_err) { switch (file_err) {
default: default:
case gbFileError_Invalid: case gbFileError_Invalid:
if (err_on_not_found) {
error(ce->proc, "Failed to `#load` file: %s; invalid file or cannot be found", c_str); error(ce->proc, "Failed to `#load` file: %s; invalid file or cannot be found", c_str);
return false; }
call->state_flags |= StateFlag_DirectiveWasFalse;
return LoadDirective_NotFound;
case gbFileError_NotExists: case gbFileError_NotExists:
if (err_on_not_found) {
error(ce->proc, "Failed to `#load` file: %s; file cannot be found", c_str); error(ce->proc, "Failed to `#load` file: %s; file cannot be found", c_str);
return false; }
call->state_flags |= StateFlag_DirectiveWasFalse;
return LoadDirective_NotFound;
case gbFileError_Permission: case gbFileError_Permission:
if (err_on_not_found) {
error(ce->proc, "Failed to `#load` file: %s; file permissions problem", c_str); error(ce->proc, "Failed to `#load` file: %s; file permissions problem", c_str);
return false; }
call->state_flags |= StateFlag_DirectiveWasFalse;
return LoadDirective_NotFound;
case gbFileError_None: case gbFileError_None:
// Okay // Okay
break; break;
@@ -1174,7 +1163,36 @@ bool check_builtin_procedure_directive(CheckerContext *c, Operand *operand, Ast
operand->type = t_u8_slice; operand->type = t_u8_slice;
operand->mode = Addressing_Constant; operand->mode = Addressing_Constant;
operand->value = exact_value_string(result); operand->value = exact_value_string(result);
return LoadDirective_Success;
}
bool check_builtin_procedure_directive(CheckerContext *c, Operand *operand, Ast *call, Type *type_hint) {
ast_node(ce, CallExpr, call);
ast_node(bd, BasicDirective, ce->proc);
String name = bd->name.string;
if (name == "location") {
if (ce->args.count > 1) {
error(ce->args[0], "'#location' expects either 0 or 1 arguments, got %td", ce->args.count);
}
if (ce->args.count > 0) {
Ast *arg = ce->args[0];
Entity *e = nullptr;
Operand o = {};
if (arg->kind == Ast_Ident) {
e = check_ident(c, &o, arg, nullptr, nullptr, true);
} else if (arg->kind == Ast_SelectorExpr) {
e = check_selector(c, &o, arg, nullptr);
}
if (e == nullptr) {
error(ce->args[0], "'#location' expected a valid entity name");
}
}
operand->type = t_source_code_location;
operand->mode = Addressing_Value;
} else if (name == "load") {
return check_load_directive(c, operand, call, type_hint, true) == LoadDirective_Success;
} else if (name == "load_hash") { } else if (name == "load_hash") {
if (ce->args.count != 2) { if (ce->args.count != 2) {
if (ce->args.count == 0) { if (ce->args.count == 0) {
@@ -1263,7 +1281,6 @@ bool check_builtin_procedure_directive(CheckerContext *c, Operand *operand, Ast
char *c_str = alloc_cstring(a, path); char *c_str = alloc_cstring(a, path);
defer (gb_free(a, c_str)); defer (gb_free(a, c_str));
gbFile f = {}; gbFile f = {};
gbFileError file_err = gb_file_open(&f, c_str); gbFileError file_err = gb_file_open(&f, c_str);
defer (gb_file_close(&f)); defer (gb_file_close(&f));
@@ -1321,6 +1338,8 @@ bool check_builtin_procedure_directive(CheckerContext *c, Operand *operand, Ast
operand->value = exact_value_u64(hash_value); operand->value = exact_value_u64(hash_value);
} else if (name == "load_or") { } else if (name == "load_or") {
warning(call, "'#load_or' is deprecated in favour of '#load(path) or_else default'");
if (ce->args.count != 2) { if (ce->args.count != 2) {
if (ce->args.count == 0) { if (ce->args.count == 0) {
error(ce->close, "'#load_or' expects 2 arguments, got 0"); error(ce->close, "'#load_or' expects 2 arguments, got 0");
@@ -1640,7 +1659,7 @@ bool check_builtin_procedure(CheckerContext *c, Operand *operand, Ast *call, i32
break; break;
case BuiltinProc_DIRECTIVE: case BuiltinProc_DIRECTIVE:
return check_builtin_procedure_directive(c, operand, call, id, type_hint); return check_builtin_procedure_directive(c, operand, call, type_hint);
case BuiltinProc_len: case BuiltinProc_len:
check_expr_or_type(c, operand, ce->args[0]); check_expr_or_type(c, operand, ce->args[0]);
+68 -2
View File
@@ -121,6 +121,28 @@ void check_or_return_split_types(CheckerContext *c, Operand *x, String const &na
bool is_diverging_expr(Ast *expr); bool is_diverging_expr(Ast *expr);
enum LoadDirectiveResult {
LoadDirective_Success = 0,
LoadDirective_Error = 1,
LoadDirective_NotFound = 2,
};
bool is_load_directive_call(Ast *call) {
call = unparen_expr(call);
if (call->kind != Ast_CallExpr) {
return false;
}
ast_node(ce, CallExpr, call);
if (ce->proc->kind != Ast_BasicDirective) {
return false;
}
ast_node(bd, BasicDirective, ce->proc);
String name = bd->name.string;
return name == "load";
}
LoadDirectiveResult check_load_directive(CheckerContext *c, Operand *operand, Ast *call, Type *type_hint, bool err_on_not_found);
void check_did_you_mean_print(DidYouMeanAnswers *d, char const *prefix = "") { void check_did_you_mean_print(DidYouMeanAnswers *d, char const *prefix = "") {
auto results = did_you_mean_results(d); auto results = did_you_mean_results(d);
if (results.count != 0) { if (results.count != 0) {
@@ -7407,9 +7429,54 @@ ExprKind check_or_else_expr(CheckerContext *c, Operand *o, Ast *node, Type *type
String name = oe->token.string; String name = oe->token.string;
Ast *arg = oe->x; Ast *arg = oe->x;
Ast *default_value = oe->y; Ast *default_value = oe->y;
Operand x = {}; Operand x = {};
Operand y = {}; Operand y = {};
// NOTE(bill, 2022-08-11): edge case to handle #load(path) or_else default
if (is_load_directive_call(arg)) {
LoadDirectiveResult res = check_load_directive(c, &x, arg, type_hint, false);
if (res == LoadDirective_Success) {
*o = x;
return Expr_Expr;
}
bool y_is_diverging = false;
check_expr_base(c, &y, default_value, x.type);
switch (y.mode) {
case Addressing_NoValue:
if (is_diverging_expr(y.expr)) {
// Allow
y.mode = Addressing_Value;
y_is_diverging = true;
} else {
error_operand_no_value(&y);
y.mode = Addressing_Invalid;
}
break;
case Addressing_Type:
error_operand_not_expression(&y);
y.mode = Addressing_Invalid;
break;
}
if (y.mode == Addressing_Invalid) {
o->mode = Addressing_Value;
o->type = t_invalid;
o->expr = node;
return Expr_Expr;
}
if (!y_is_diverging) {
check_assignment(c, &y, x.type, name);
}
o->mode = y.mode;
o->type = y.type;
o->expr = node;
return Expr_Expr;
}
check_multi_expr_with_type_hint(c, &x, arg, type_hint); check_multi_expr_with_type_hint(c, &x, arg, type_hint);
if (x.mode == Addressing_Invalid) { if (x.mode == Addressing_Invalid) {
o->mode = Addressing_Value; o->mode = Addressing_Value;
@@ -7417,7 +7484,6 @@ ExprKind check_or_else_expr(CheckerContext *c, Operand *o, Ast *node, Type *type
o->expr = node; o->expr = node;
return Expr_Expr; return Expr_Expr;
} }
bool y_is_diverging = false; bool y_is_diverging = false;
check_expr_base(c, &y, default_value, x.type); check_expr_base(c, &y, default_value, x.type);
switch (y.mode) { switch (y.mode) {
+4
View File
@@ -351,6 +351,10 @@ lbValue lb_emit_try_has_value(lbProcedure *p, lbValue rhs) {
lbValue lb_emit_or_else(lbProcedure *p, Ast *arg, Ast *else_expr, TypeAndValue const &tv) { lbValue lb_emit_or_else(lbProcedure *p, Ast *arg, Ast *else_expr, TypeAndValue const &tv) {
if (arg->state_flags & StateFlag_DirectiveWasFalse) {
return lb_build_expr(p, else_expr);
}
lbValue lhs = {}; lbValue lhs = {};
lbValue rhs = {}; lbValue rhs = {};
lb_emit_try_lhs_rhs(p, arg, tv, &lhs, &rhs); lb_emit_try_lhs_rhs(p, arg, tv, &lhs, &rhs);
+2 -1
View File
@@ -282,7 +282,8 @@ enum StateFlag : u8 {
StateFlag_type_assert = 1<<2, StateFlag_type_assert = 1<<2,
StateFlag_no_type_assert = 1<<3, StateFlag_no_type_assert = 1<<3,
StateFlag_SelectorCallExpr = 1<<6, StateFlag_SelectorCallExpr = 1<<5,
StateFlag_DirectiveWasFalse = 1<<6,
StateFlag_BeenHandled = 1<<7, StateFlag_BeenHandled = 1<<7,
}; };