1 // This file defines how to do type promotions during compilation
4 #include "../environment.h"
5 #include "../stdlib/datatypes.h"
6 #include "../stdlib/text.h"
7 #include "../typecheck.h"
9 #include "compilation.h"
11 static Text_t quoted_str(const char *str) {
12 return Text$quoted(Text$from_str(str), false, Text("\""));
16 bool promote(env_t *env, ast_t *ast, Text_t *code, type_t *actual, type_t *needed) {
17 if (type_eq(actual, needed)) return true;
19 if (!can_promote(actual, needed)) return false;
21 if (needed->tag == ClosureType && actual->tag == FunctionType) {
22 *code = Texts("((Closure_t){", *code, ", NULL})");
27 type_t *more_complete = most_complete_type(actual, needed);
28 if (more_complete) return true;
30 // Serialization/deserialization:
31 if (!type_eq(non_optional(value_type(needed)), Type(ListType, Type(ByteType)))
32 || !type_eq(non_optional(value_type(actual)), Type(ListType, Type(ByteType)))) {
33 if (type_eq(needed, Type(ListType, Type(ByteType)))) {
34 *code = Texts("generic_serialize((", compile_declaration(actual, Text("[1]")), "){", *code, "}, ",
35 compile_type_info(actual), ")");
37 } else if (type_eq(actual, Type(ListType, Type(ByteType)))) {
38 *code = Texts("({ ", compile_declaration(needed, Text("deserialized")),
40 "generic_deserialize(",
41 *code, ", &deserialized, ", compile_type_info(needed),
48 // Optional promotion:
49 if (needed->tag == OptionalType && type_eq(actual, Match(needed, OptionalType)->type)) {
50 *code = promote_to_optional(actual, *code);
54 // Optional -> Bool promotion
55 if (actual->tag == OptionalType && needed->tag == BoolType) {
56 *code = Texts("(!", check_none(actual, *code), ")");
61 if (actual->tag == TextType && needed->tag == TextType && streq(Match(needed, TextType)->lang, "Text")) return true;
63 // Automatic optional checking for nums:
64 if (needed->tag == NumType && actual->tag == OptionalType && Match(actual, OptionalType)->type->tag == NumType) {
65 int64_t line = get_line_number(ast->file, ast->start);
66 *code = Texts("({ ", compile_declaration(actual, Text("opt")), " = ", *code, "; ", "if unlikely (",
67 check_none(actual, Text("opt")), ")\n", "#line ", line, "\n", "fail_source(",
68 quoted_str(ast->file->filename), ", ", (int64_t)(ast->start - ast->file->text), ", ",
69 (int64_t)(ast->end - ast->file->text), ", ",
70 "Text(\"This was expected to be a value, but it's none\"));\n",
71 optional_into_nonnone(actual, Text("opt")), "; })");
75 // Numeric promotions/demotions
76 if ((is_numeric_type(actual) || actual->tag == BoolType) && (is_numeric_type(needed) || needed->tag == BoolType)) {
77 arg_ast_t *args = new (arg_ast_t, .value = LiteralCode(*code, .type = actual));
78 binding_t *constructor = get_constructor(
79 env, needed, args, env->current_type != NULL && type_eq(env->current_type, value_type(needed)));
81 DeclareMatch(fn, constructor->type, FunctionType);
82 if (fn->args->next == NULL) {
83 *code = Texts(constructor->code, "(", compile_arguments(env, ast, fn->args, args), ")");
89 if (needed->tag == EnumType) {
90 const char *tag = enum_single_value_tag(needed, actual);
91 binding_t *b = get_binding(Match(needed, EnumType)->env, tag);
92 assert(b && b->type->tag == FunctionType);
93 // Single-value enum constructor:
94 if (!promote(env, ast, code, actual, Match(b->type, FunctionType)->args->type)) return false;
95 *code = Texts(b->code, "(", *code, ")");
100 if (actual->tag == TextType && type_eq(actual, TEXT_TYPE) && needed->tag == CStringType) {
101 *code = Texts("Text$as_c_string(", *code, ")");
105 // Automatic dereferencing:
106 if (actual->tag == PointerType && can_promote(Match(actual, PointerType)->pointed, needed)) {
107 *code = Texts("*(", *code, ")");
108 return promote(env, ast, code, Match(actual, PointerType)->pointed, needed);
111 // Stack ref promotion:
112 if (actual->tag == PointerType && needed->tag == PointerType) return true;
114 // Cross-promotion between tables with default values and without
115 if (needed->tag == TableType && actual->tag == TableType) return true;
117 if (needed->tag == ClosureType && actual->tag == ClosureType) return true;
119 if (needed->tag == FunctionType && actual->tag == FunctionType) {
120 *code = Texts("(", compile_type(needed), ")", *code);
128 Text_t compile_to_type(env_t *env, ast_t *ast, type_t *t) {
129 assert(!is_incomplete_type(t));
131 if (t->tag == EnumType) {
132 env = with_enum_scope(env, t);
135 if (ast->tag == Block && Match(ast, Block)->statements && !Match(ast, Block)->statements->next) {
136 ast = Match(ast, Block)->statements->ast;
139 if (ast->tag == Int && is_numeric_type(non_optional(t))) {
140 return compile_int_to_type(env, ast, t);
141 } else if (ast->tag == Num && t->tag == NumType) {
142 double n = Match(ast, Num)->n;
143 switch (Match(t, NumType)->bits) {
144 case TYPE_NBITS64: return Text$from_str(String(hex_double(n)));
145 case TYPE_NBITS32: return Text$from_str(String(hex_double(n), "f"));
146 default: code_err(ast, "This is not a valid number bit width");
148 } else if (ast->tag == None) {
149 if (t->tag != OptionalType) code_err(ast, "This is not supposed to be an optional type");
150 else if (Match(t, OptionalType)->type == NULL)
151 code_err(ast, "I don't know what kind of `none` this is supposed to "
153 "tell me by declaring a variable like `foo : Type = none`");
154 return compile_none(t);
155 } else if (t->tag == PointerType && (ast->tag == HeapAllocate || ast->tag == StackReference)) {
156 return compile_typed_allocation(env, ast, t);
157 } else if (t->tag == ListType && ast->tag == List) {
158 return compile_typed_list(env, ast, t);
159 } else if (t->tag == TableType && ast->tag == Table) {
160 return compile_typed_table(env, ast, t);
163 type_t *actual = get_type(env, ast);
165 // Edge case: there are some situations where a method call needs to have
166 // the `self` value get compiled to a specific type that can't be fully
167 // inferred from the expression itself. We can infer what the specific type
168 // should be from what we know the specific type of the return value is,
169 // but it requires a bit of special logic.
171 // x : [Int?] = [none].sorted()
172 // Here, we know that `[none]` is `[Int?]`, but we need to thread that
173 // information through the compiler using an `ExplicitlyTyped` node.
174 if (ast->tag == MethodCall) {
175 DeclareMatch(methodcall, ast, MethodCall);
176 type_t *self_type = get_type(env, methodcall->self);
177 // Currently, this is only implemented for cases where you have the
178 // return type and the self type equal to each other, because that's the
179 // main case I care about with list and set methods (e.g.
181 if (is_incomplete_type(self_type) && type_eq(self_type, actual)) {
182 type_t *completed_self = most_complete_type(self_type, t);
183 if (completed_self) {
184 ast_t *explicit_self =
185 WrapAST(methodcall->self, ExplicitlyTyped, .ast = methodcall->self, .type = completed_self);
186 ast_t *new_methodcall =
187 WrapAST(ast, MethodCall, .self = explicit_self, .name = methodcall->name, .args = methodcall->args);
188 return compile_to_type(env, new_methodcall, t);
193 // Promote values to views-of-values if needed:
194 if (t->tag == PointerType && Match(t, PointerType)->is_stack && actual->tag != PointerType) {
195 if (type_eq(actual, Match(t, PointerType)->pointed) && can_be_mutated(env, ast))
196 return Texts("&(", compile_lvalue(env, ast), ")");
199 if (!is_incomplete_type(actual)) {
200 Text_t code = compile(env, ast);
201 if (promote(env, ast, &code, actual, t)) return code;
204 arg_ast_t *constructor_args = new (arg_ast_t, .value = ast);
205 binding_t *constructor = get_constructor(env, t, constructor_args, true);
207 arg_t *arg_spec = Match(constructor->type, FunctionType)->args;
208 return Texts(constructor->code, "(", compile_arguments(env, ast, arg_spec, constructor_args), ")");
211 code_err(ast, "I expected a ", type_to_text(t), " here, but this is a ", type_to_text(actual));