Add enum type info and fix enum casting

This commit is contained in:
Ginger Bill
2017-01-01 16:58:38 +00:00
parent 6fef74317c
commit 311b5cb6e2
11 changed files with 178 additions and 193 deletions

View File

@@ -11,13 +11,14 @@ import {
win32 "sys/windows.odin";
}
type Thing enum f64 {
_, // Ignore first value
A = 1<<(10*iota),
B,
C,
D,
}
};
proc main() {
var ti = type_info(Thing);
@@ -32,6 +33,8 @@ proc main() {
fmt.println();
}
fmt.println(Thing.A, Thing.B, Thing.C, Thing.D);
var x Thing = Thing.A;
fmt.println(x, Thing.B, Thing.C, Thing.D);
}

View File

@@ -1,10 +1,8 @@
#shared_global_scope;
import {
"os.odin";
"fmt.odin";
"mem.odin";
}
import "os.odin";
import "fmt.odin";
import "mem.odin";
// IMPORTANT NOTE(bill): `type_info` & `type_info_val` cannot be used within a
// #shared_global_scope due to the internals of the compiler.
@@ -76,6 +74,7 @@ type {
Enum struct #ordered {
base ^Type_Info;
names []string;
// TODO(bill): store values some how. Maybe using a raw_union
};
}
}

View File

@@ -424,159 +424,152 @@ proc wglDeleteContext (hglrc HGLRC) -> BOOL #foreign #dll_import
proc GetKeyState (v_key i32) -> i16 #foreign #dll_import
proc GetAsyncKeyState(v_key i32) -> i16 #foreign #dll_import
proc is_key_down(key i32) -> bool #inline { return GetAsyncKeyState(key) < 0; }
proc is_key_down(key Key_Code) -> bool #inline { return GetAsyncKeyState(key as i32) < 0; }
const {
KEY_LBUTTON = 0x01;
KEY_RBUTTON = 0x02;
KEY_CANCEL = 0x03;
KEY_MBUTTON = 0x04;
type Key_Code enum i32 {
LBUTTON = 0x01,
RBUTTON = 0x02,
CANCEL = 0x03,
MBUTTON = 0x04,
BACK = 0x08,
TAB = 0x09,
CLEAR = 0x0C,
RETURN = 0x0D,
KEY_BACK = 0x08;
KEY_TAB = 0x09;
SHIFT = 0x10,
CONTROL = 0x11,
MENU = 0x12,
PAUSE = 0x13,
CAPITAL = 0x14,
KANA = 0x15,
HANGEUL = 0x15,
HANGUL = 0x15,
JUNJA = 0x17,
FINAL = 0x18,
HANJA = 0x19,
KANJI = 0x19,
ESCAPE = 0x1B,
CONVERT = 0x1C,
NONCONVERT = 0x1D,
ACCEPT = 0x1E,
MODECHANGE = 0x1F,
SPACE = 0x20,
PRIOR = 0x21,
NEXT = 0x22,
END = 0x23,
HOME = 0x24,
LEFT = 0x25,
UP = 0x26,
RIGHT = 0x27,
DOWN = 0x28,
SELECT = 0x29,
PRINT = 0x2A,
EXECUTE = 0x2B,
SNAPSHOT = 0x2C,
INSERT = 0x2D,
DELETE = 0x2E,
HELP = 0x2F,
KEY_CLEAR = 0x0C;
KEY_RETURN = 0x0D;
NUM0 = '0',
NUM1 = '1',
NUM2 = '2',
NUM3 = '3',
NUM4 = '4',
NUM5 = '5',
NUM6 = '6',
NUM7 = '7',
NUM8 = '8',
NUM9 = '9',
A = 'A',
B = 'B',
C = 'C',
D = 'D',
E = 'E',
F = 'F',
G = 'G',
H = 'H',
I = 'I',
J = 'J',
K = 'K',
L = 'L',
M = 'M',
N = 'N',
O = 'O',
P = 'P',
Q = 'Q',
R = 'R',
S = 'S',
T = 'T',
U = 'U',
V = 'V',
W = 'W',
X = 'X',
Y = 'Y',
Z = 'Z',
KEY_SHIFT = 0x10;
KEY_CONTROL = 0x11;
KEY_MENU = 0x12;
KEY_PAUSE = 0x13;
KEY_CAPITAL = 0x14;
LWIN = 0x5B,
RWIN = 0x5C,
APPS = 0x5D,
KEY_KANA = 0x15;
KEY_HANGEUL = 0x15;
KEY_HANGUL = 0x15;
KEY_JUNJA = 0x17;
KEY_FINAL = 0x18;
KEY_HANJA = 0x19;
KEY_KANJI = 0x19;
NUMPAD0 = 0x60,
NUMPAD1 = 0x61,
NUMPAD2 = 0x62,
NUMPAD3 = 0x63,
NUMPAD4 = 0x64,
NUMPAD5 = 0x65,
NUMPAD6 = 0x66,
NUMPAD7 = 0x67,
NUMPAD8 = 0x68,
NUMPAD9 = 0x69,
MULTIPLY = 0x6A,
ADD = 0x6B,
SEPARATOR = 0x6C,
SUBTRACT = 0x6D,
DECIMAL = 0x6E,
DIVIDE = 0x6F,
KEY_ESCAPE = 0x1B;
F1 = 0x70,
F2 = 0x71,
F3 = 0x72,
F4 = 0x73,
F5 = 0x74,
F6 = 0x75,
F7 = 0x76,
F8 = 0x77,
F9 = 0x78,
F10 = 0x79,
F11 = 0x7A,
F12 = 0x7B,
F13 = 0x7C,
F14 = 0x7D,
F15 = 0x7E,
F16 = 0x7F,
F17 = 0x80,
F18 = 0x81,
F19 = 0x82,
F20 = 0x83,
F21 = 0x84,
F22 = 0x85,
F23 = 0x86,
F24 = 0x87,
KEY_CONVERT = 0x1C;
KEY_NONCONVERT = 0x1D;
KEY_ACCEPT = 0x1E;
KEY_MODECHANGE = 0x1F;
KEY_SPACE = 0x20;
KEY_PRIOR = 0x21;
KEY_NEXT = 0x22;
KEY_END = 0x23;
KEY_HOME = 0x24;
KEY_LEFT = 0x25;
KEY_UP = 0x26;
KEY_RIGHT = 0x27;
KEY_DOWN = 0x28;
KEY_SELECT = 0x29;
KEY_PRINT = 0x2A;
KEY_EXECUTE = 0x2B;
KEY_SNAPSHOT = 0x2C;
KEY_INSERT = 0x2D;
KEY_DELETE = 0x2E;
KEY_HELP = 0x2F;
KEY_NUM0 = '0';
KEY_NUM1 = '1';
KEY_NUM2 = '2';
KEY_NUM3 = '3';
KEY_NUM4 = '4';
KEY_NUM5 = '5';
KEY_NUM6 = '6';
KEY_NUM7 = '7';
KEY_NUM8 = '8';
KEY_NUM9 = '9';
KEY_A = 'A';
KEY_B = 'B';
KEY_C = 'C';
KEY_D = 'D';
KEY_E = 'E';
KEY_F = 'F';
KEY_G = 'G';
KEY_H = 'H';
KEY_I = 'I';
KEY_J = 'J';
KEY_K = 'K';
KEY_L = 'L';
KEY_M = 'M';
KEY_N = 'N';
KEY_O = 'O';
KEY_P = 'P';
KEY_Q = 'Q';
KEY_R = 'R';
KEY_S = 'S';
KEY_T = 'T';
KEY_U = 'U';
KEY_V = 'V';
KEY_W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EM_CLEAR = 0xFE;
NUMLOCK = 0x90,
SCROLL = 0x91,
LSHIFT = 0xA0,
RSHIFT = 0xA1,
LCONTROL = 0xA2,
RCONTROL = 0xA3,
LMENU = 0xA4,
RMENU = 0xA5,
PROCESSKEY = 0xE5,
ATTN = 0xF6,
CRSEL = 0xF7,
EXSEL = 0xF8,
EREOF = 0xF9,
PLAY = 0xFA,
ZOOM = 0xFB,
NONAME = 0xFC,
PA1 = 0xFD,
OEM_CLEAR = 0xFE,
}

View File

@@ -1177,7 +1177,6 @@ void check_global_collect_entities_from_file(Checker *c, Scope *parent_scope, As
case_end;
case_ast_node(vs, ValueSpec, spec);
switch (vs->keyword) {
case Token_let:
case Token_var: {
// NOTE(bill): You need to store the entity information here unline a constant declaration
isize entity_count = vs->names.count;
@@ -1202,7 +1201,7 @@ void check_global_collect_entities_from_file(Checker *c, Scope *parent_scope, As
error_node(name, "A declaration's name must be an identifier, got %.*s", LIT(ast_node_strings[name->kind]));
continue;
}
Entity *e = make_entity_variable(c->allocator, parent_scope, name->Ident, NULL, vs->keyword == Token_let);
Entity *e = make_entity_variable(c->allocator, parent_scope, name->Ident, NULL);
e->identifier = name;
entities[entity_index++] = e;

View File

@@ -95,18 +95,20 @@ void check_init_variables(Checker *c, Entity **lhs, isize lhs_count, AstNodeArra
error(lhs[0]->token, "Assignment count mismatch `%td` = `%td`", lhs_count, rhs_count);
}
#if 0
if (lhs[0]->kind == Entity_Variable &&
lhs[0]->Variable.is_let) {
if (lhs_count != rhs_count) {
error(lhs[0]->token, "`let` variables must be initialized, `%td` = `%td`", lhs_count, rhs_count);
}
}
#endif
gb_temp_arena_memory_end(tmp);
}
void check_var_spec_node(Checker *c, AstNodeValueSpec *vs) {
GB_ASSERT(vs->keyword == Token_var || vs->keyword == Token_let);
GB_ASSERT(vs->keyword == Token_var);
isize entity_count = vs->names.count;
isize entity_index = 0;
Entity **entities = gb_alloc_array(c->allocator, Entity *, entity_count);
@@ -123,7 +125,7 @@ void check_var_spec_node(Checker *c, AstNodeValueSpec *vs) {
found = current_scope_lookup_entity(c->context.scope, str);
}
if (found == NULL) {
entity = make_entity_variable(c->allocator, c->context.scope, token, NULL, vs->keyword == Token_let);
entity = make_entity_variable(c->allocator, c->context.scope, token, NULL);
add_entity_definition(&c->info, name, entity);
} else {
TokenPos pos = found->token.pos;

View File

@@ -57,7 +57,6 @@ struct Entity {
struct {
i32 field_index;
i32 field_src_index;
bool is_let;
} Variable;
i32 TypeName;
struct {
@@ -97,9 +96,8 @@ Entity *alloc_entity(gbAllocator a, EntityKind kind, Scope *scope, Token token,
return entity;
}
Entity *make_entity_variable(gbAllocator a, Scope *scope, Token token, Type *type, bool is_let) {
Entity *make_entity_variable(gbAllocator a, Scope *scope, Token token, Type *type) {
Entity *entity = alloc_entity(a, Entity_Variable, scope, token, type);
entity->Variable.is_let = is_let;
return entity;
}
@@ -124,7 +122,7 @@ Entity *make_entity_type_name(gbAllocator a, Scope *scope, Token token, Type *ty
}
Entity *make_entity_param(gbAllocator a, Scope *scope, Token token, Type *type, bool anonymous) {
Entity *entity = make_entity_variable(a, scope, token, type, false);
Entity *entity = make_entity_variable(a, scope, token, type);
entity->flags |= EntityFlag_Used;
entity->flags |= EntityFlag_Anonymous*(anonymous != 0);
entity->flags |= EntityFlag_Param;
@@ -132,7 +130,7 @@ Entity *make_entity_param(gbAllocator a, Scope *scope, Token token, Type *type,
}
Entity *make_entity_field(gbAllocator a, Scope *scope, Token token, Type *type, bool anonymous, i32 field_src_index) {
Entity *entity = make_entity_variable(a, scope, token, type, false);
Entity *entity = make_entity_variable(a, scope, token, type);
entity->Variable.field_src_index = field_src_index;
entity->Variable.field_index = field_src_index;
entity->flags |= EntityFlag_Field;
@@ -141,7 +139,7 @@ Entity *make_entity_field(gbAllocator a, Scope *scope, Token token, Type *type,
}
Entity *make_entity_vector_elem(gbAllocator a, Scope *scope, Token token, Type *type, i32 field_src_index) {
Entity *entity = make_entity_variable(a, scope, token, type, false);
Entity *entity = make_entity_variable(a, scope, token, type);
entity->Variable.field_src_index = field_src_index;
entity->Variable.field_index = field_src_index;
entity->flags |= EntityFlag_Field;
@@ -186,6 +184,6 @@ Entity *make_entity_implicit_value(gbAllocator a, String name, Type *type, Impli
Entity *make_entity_dummy_variable(gbAllocator a, Scope *file_scope, Token token) {
token.string = str_lit("_");
return make_entity_variable(a, file_scope, token, NULL, false);
return make_entity_variable(a, file_scope, token, NULL);
}

View File

@@ -791,7 +791,7 @@ void check_enum_type(Checker *c, Type *enum_type, Type *named_type, AstNode *nod
continue;
}
GB_ASSERT(c->context.iota.kind == ExactValue_Invalid);
ExactValue context_iota = c->context.iota;
c->context.iota = e->Constant.value;
e->Constant.value = (ExactValue){0};
@@ -799,7 +799,7 @@ void check_enum_type(Checker *c, Type *enum_type, Type *named_type, AstNode *nod
check_expr(c, &operand, init);
check_init_constant(c, e, &operand);
c->context.iota = (ExactValue){0};
c->context.iota = context_iota;
if (operand.mode == Addressing_Constant) {
HashKey key = hash_string(name);
@@ -1001,9 +1001,9 @@ void check_identifier(Checker *c, Operand *o, AstNode *n, Type *named_type) {
}
#else
o->mode = Addressing_Variable;
if (e->Variable.is_let) {
o->mode = Addressing_Value;
}
// if (e->Variable.is_let) {
// o->mode = Addressing_Value;
// }
#endif
break;
@@ -1770,8 +1770,8 @@ bool check_is_castable_to(Checker *c, Operand *operand, Type *y) {
}
Type *x = operand->type;
Type *xb = base_type(x);
Type *yb = base_type(y);
Type *xb = base_type(base_enum_type(x));
Type *yb = base_type(base_enum_type(y));
if (are_types_identical(xb, yb)) {
return true;
}

View File

@@ -848,7 +848,7 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) {
tt = make_type_pointer(c->allocator, case_type);
add_type_info_type(c, tt);
}
Entity *tag_var = make_entity_variable(c->allocator, c->context.scope, ms->var->Ident, tt, false);
Entity *tag_var = make_entity_variable(c->allocator, c->context.scope, ms->var->Ident, tt);
tag_var->flags |= EntityFlag_Used;
add_entity(c, c->context.scope, ms->var, tag_var);
add_entity_use(c, ms->var, tag_var);
@@ -1106,7 +1106,6 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) {
case_end;
case_ast_node(vs, ValueSpec, spec);
switch (vs->keyword) {
case Token_let:
case Token_var: {
isize entity_count = vs->names.count;
isize entity_index = 0;
@@ -1124,7 +1123,7 @@ void check_stmt_internal(Checker *c, AstNode *node, u32 flags) {
found = current_scope_lookup_entity(c->context.scope, str);
}
if (found == NULL) {
entity = make_entity_variable(c->allocator, c->context.scope, token, NULL, vs->keyword == Token_let);
entity = make_entity_variable(c->allocator, c->context.scope, token, NULL);
add_entity_definition(&c->info, name, entity);
} else {
TokenPos pos = found->token.pos;

View File

@@ -1211,7 +1211,6 @@ void fix_advance_to_next_stmt(AstFile *f) {
return;
case Token_var:
case Token_let:
case Token_const:
case Token_type:
case Token_proc:
@@ -2154,7 +2153,6 @@ PARSE_SPEC_PROC(parse_value_spec) {
switch (keyword) {
case Token_var:
case Token_let:
if (type == NULL && values.count == 0 && names.count > 0) {
syntax_error(f->curr_token, "Missing type or initialization");
return make_bad_decl(f, f->curr_token, f->curr_token);
@@ -2240,7 +2238,6 @@ PARSE_SPEC_PROC(parse_include_spec) {
AstNode *parse_decl(AstFile *f) {
switch (f->curr_token.kind) {
case Token_var:
case Token_let:
case Token_const:
return parse_generic_decl(f, f->curr_token.kind, parse_value_spec);
@@ -2269,7 +2266,6 @@ AstNode *parse_decl(AstFile *f) {
AstNode *parse_simple_stmt(AstFile *f) {
switch (f->curr_token.kind) {
case Token_var:
case Token_let:
case Token_const:
return parse_decl(f);
}
@@ -2555,9 +2551,11 @@ AstNode *parse_identifier_or_type(AstFile *f) {
base_type = parse_type(f);
}
Token open = expect_token(f, Token_OpenBrace);
AstNodeArray fields = parse_element_list(f);
AstNodeArray values = parse_element_list(f);
Token close = expect_token(f, Token_CloseBrace);
return make_enum_type(f, token, base_type, fields);
return make_enum_type(f, token, base_type, values);
}
case Token_proc: {
@@ -3026,7 +3024,6 @@ AstNode *parse_stmt(AstFile *f) {
return s;
case Token_var:
case Token_let:
case Token_const:
case Token_proc:
case Token_type:
@@ -3071,8 +3068,6 @@ AstNode *parse_stmt(AstFile *f) {
case AstNode_GenericDecl:
if (node->GenericDecl.token.kind == Token_var) {
valid = true;
} else if (node->GenericDecl.token.kind == Token_let) {
valid = true;
}
break;
}

View File

@@ -1233,8 +1233,7 @@ ssaValue *ssa_add_local_generated(ssaProcedure *proc, Type *type) {
Entity *e = make_entity_variable(proc->module->allocator,
scope,
empty_token,
type,
false);
type);
return ssa_add_local(proc, e);
}
@@ -1920,8 +1919,8 @@ ssaValue *ssa_emit_conv(ssaProcedure *proc, ssaValue *value, Type *t) {
return value;
}
Type *src = base_type(src_type);
Type *dst = base_type(t);
Type *src = base_type(base_enum_type(src_type));
Type *dst = base_type(base_enum_type(t));
if (value->kind == ssaValue_Constant) {
if (is_type_any(dst)) {
@@ -3934,7 +3933,6 @@ void ssa_build_stmt_internal(ssaProcedure *proc, AstNode *node) {
switch (vs->keyword) {
case Token_const:
break;
case Token_let:
case Token_var: {
ssaModule *m = proc->module;
gbTempArenaMemory tmp = gb_temp_arena_memory_begin(&m->tmp_arena);
@@ -4784,7 +4782,7 @@ void ssa_init_module(ssaModule *m, Checker *c, BuildContext *build_context) {
{
String name = str_lit(SSA_TYPE_INFO_DATA_NAME);
isize count = c->info.type_info_map.entries.count;
Entity *e = make_entity_variable(m->allocator, NULL, make_token_ident(name), make_type_array(m->allocator, t_type_info, count), false);
Entity *e = make_entity_variable(m->allocator, NULL, make_token_ident(name), make_type_array(m->allocator, t_type_info, count));
ssaValue *g = ssa_make_value_global(m->allocator, e, NULL);
g->Global.is_private = true;
ssa_module_add_value(m, e, g);
@@ -4816,7 +4814,7 @@ void ssa_init_module(ssaModule *m, Checker *c, BuildContext *build_context) {
String name = str_lit(SSA_TYPE_INFO_DATA_MEMBER_NAME);
Entity *e = make_entity_variable(m->allocator, NULL, make_token_ident(name),
make_type_array(m->allocator, t_type_info_member, count), false);
make_type_array(m->allocator, t_type_info_member, count));
ssaValue *g = ssa_make_value_global(m->allocator, e, NULL);
ssa_module_add_value(m, e, g);
map_ssa_value_set(&m->members, hash_string(name), g);
@@ -5504,7 +5502,7 @@ void ssa_gen_tree(ssaGen *s) {
token.string.text = gb_alloc_array(a, u8, name_len);
token.string.len = gb_snprintf(cast(char *)token.string.text, name_len,
"%s-%d", name_base, id)-1;
Entity *e = make_entity_variable(a, NULL, token, make_type_array(a, t_string, count), false);
Entity *e = make_entity_variable(a, NULL, token, make_type_array(a, t_string, count));
name_array = ssa_make_value_global(a, e, NULL);
name_array->Global.is_private = true;
ssa_module_add_value(m, e, name_array);

View File

@@ -85,7 +85,6 @@ TOKEN_KIND(Token__KeywordBegin, "_KeywordBegin"), \
TOKEN_KIND(Token_type, "type"), \
TOKEN_KIND(Token_proc, "proc"), \
TOKEN_KIND(Token_var, "var"), \
TOKEN_KIND(Token_let, "let"), \
TOKEN_KIND(Token_const, "const"), \
TOKEN_KIND(Token_import, "import"), \
TOKEN_KIND(Token_include, "include"), \