Skip to content
← Back

src/syntax/process/default_argument_values.ghul

1
namespace Syntax.Process is
2
use Semantic.Types.Type;
3
4
use IR.Values;
5
6
// Translates a stored argument-default string (from
7
// `Function.argument_defaults`) into an IR value of the formal
8
// parameter type. ghūl source declares only `= _`, so a null or
9
// "default" entry maps to `IR.Values.DEFAULT`; the remaining
10
// cases are reflected .NET literal defaults — stored in invariant-
11
// culture text form by symbol_factory and read back here via
12
// per-type dispatch. Shared by an omitted call argument
13
// (COMPILE_CALLS) and an omitted attribute constructor argument
14
// (ATTRIBUTE_RESOLVER).
15
class DEFAULT_ARGUMENT_VALUES is
16
build(
17
stored: string?,
18
formal_type: Type,
19
innate_symbol_lookup: Semantic.Lookups.InnateSymbolLookup
20
) -> Value static is
21
if !stored? \/ stored =~ "default" then
22
return IR.Values.DEFAULT(formal_type);
23
fi
24
25
if formal_type.matches(innate_symbol_lookup.get_string_type()) then
26
return IR.Values.Literal.STRING(stored, formal_type);
27
fi
28
29
if formal_type.matches(innate_symbol_lookup.get_bool_type()) then
30
let v = if stored =~ "True" then "1" else "0" fi;
31
return IR.Values.Literal.NUMBER(v, formal_type, "i4");
32
fi
33
34
if formal_type.matches(innate_symbol_lookup.get_char_type()) then
35
let v =
36
if stored.length > 0 then
37
"{cast int(stored.get_chars(0))}"
38
else
39
"0"
40
fi;
41
return IR.Values.Literal.NUMBER(v, formal_type, "i4");
42
fi
43
44
if formal_type.matches(innate_symbol_lookup.get_long_type())
45
\/ formal_type.matches(innate_symbol_lookup.get_ulong_type())
46
then
47
return IR.Values.Literal.NUMBER(stored, formal_type, "i8");
48
fi
49
50
if formal_type.matches(innate_symbol_lookup.get_single_type()) then
51
return IR.Values.Literal.NUMBER(stored, formal_type, "r4");
52
fi
53
54
if formal_type.matches(innate_symbol_lookup.get_double_type()) then
55
return IR.Values.Literal.NUMBER(stored, formal_type, "r8");
56
fi
57
58
let is_enum_type =
59
formal_type.symbol.symbol_kind == Semantic.Symbols.SymbolKind.ENUM;
60
61
if formal_type.matches(innate_symbol_lookup.get_byte_type())
62
\/ formal_type.matches(innate_symbol_lookup.get_ubyte_type())
63
\/ formal_type.matches(innate_symbol_lookup.get_short_type())
64
\/ formal_type.matches(innate_symbol_lookup.get_ushort_type())
65
\/ formal_type.matches(innate_symbol_lookup.get_int_type())
66
\/ formal_type.matches(innate_symbol_lookup.get_uint_type())
67
\/ is_enum_type
68
then
69
return IR.Values.Literal.NUMBER(stored, formal_type, "i4");
70
fi
71
72
return IR.Values.DEFAULT(formal_type);
73
si
74
si
75
si