Browse Source

Allow for base enum type with an enum declaration

gingerBill 7 years ago
parent
commit
8236c6d4b7
6 changed files with 51 additions and 47 deletions
  1. 6 0
      src/check_expr.cpp
  2. 1 0
      src/check_type.cpp
  3. 0 3
      src/checker.cpp
  4. 1 0
      src/checker.hpp
  5. 0 1
      src/parser.cpp
  6. 43 43
      src/tokenizer.cpp

+ 6 - 0
src/check_expr.cpp

@@ -486,6 +486,12 @@ i64 check_distance_between_types(CheckerContext *c, Operand *operand, Type *type
 		}
 	}
 
+	if (is_type_enum(dst) && are_types_identical(dst->Enum.base_type, operand->type)) {
+		if (c->in_enum_type) {
+			return 3;
+		}
+	}
+
 #if 0
 	if (are_types_identical(dst, src) && (!is_type_named(dst) || !is_type_named(src))) {
 		return 1;

+ 1 - 0
src/check_type.cpp

@@ -1931,6 +1931,7 @@ bool check_type_internal(CheckerContext *ctx, AstNode *e, Type **type, Type *nam
 		bool ips = ctx->in_polymorphic_specialization;
 		defer (ctx->in_polymorphic_specialization = ips);
 		ctx->in_polymorphic_specialization = false;
+		ctx->in_enum_type = true;
 
 		*type = alloc_type_enum();
 		set_base_type(named_type, *type);

+ 0 - 3
src/checker.cpp

@@ -549,9 +549,6 @@ void init_universal_scope(void) {
 	add_global_constant(str_lit("false"), t_untyped_bool, exact_value_bool(false));
 
 	add_global_entity(alloc_entity_nil(str_lit("nil"), t_untyped_nil));
-	// add_global_entity(alloc_entity_library_name(universal_scope,
-	//                                             make_token_ident(str_lit("__llvm_core")), t_invalid,
-	//                                             str_lit(""), str_lit("__llvm_core")));
 
 	// TODO(bill): Set through flags in the compiler
 	add_global_string_constant(str_lit("ODIN_VENDOR"),  bc->ODIN_VENDOR);

+ 1 - 0
src/checker.hpp

@@ -343,6 +343,7 @@ struct CheckerContext {
 	CheckerTypePath *type_path;
 	isize            type_level; // TODO(bill): Actually handle correctly
 
+	bool       in_enum_type;
 	bool       collect_delayed_decls;
 	bool       allow_polymorphic_types;
 	bool       no_polymorphic_errors;

+ 0 - 1
src/parser.cpp

@@ -1030,7 +1030,6 @@ AstNode *ast_attribute(AstFile *f, Token token, Token open, Token close, Array<A
 
 
 bool next_token0(AstFile *f) {
-	// Token prev = f->curr_token;
 	if (f->curr_token_index+1 < f->tokens.count) {
 		f->curr_token = f->tokens[++f->curr_token_index];
 		return true;

+ 43 - 43
src/tokenizer.cpp

@@ -82,49 +82,49 @@ TOKEN_KIND(Token__ComparisonEnd, ""), \
 TOKEN_KIND(Token__OperatorEnd, ""), \
 \
 TOKEN_KIND(Token__KeywordBegin, ""), \
-	TOKEN_KIND(Token_import,                 "import"),                 \
-	TOKEN_KIND(Token_export,                 "export"),                 \
-	TOKEN_KIND(Token_foreign,                "foreign"),                \
-	TOKEN_KIND(Token_package,                "package"),                \
-	TOKEN_KIND(Token_type,                   "type"),                   \
-	TOKEN_KIND(Token_when,                   "when"),                   \
-	TOKEN_KIND(Token_if,                     "if"),                     \
-	TOKEN_KIND(Token_else,                   "else"),                   \
-	TOKEN_KIND(Token_for,                    "for"),                    \
-	TOKEN_KIND(Token_switch,                 "switch"),                 \
-	TOKEN_KIND(Token_in,                     "in"),                     \
-	TOKEN_KIND(Token_do,                     "do"),                     \
-	TOKEN_KIND(Token_case,                   "case"),                   \
-	TOKEN_KIND(Token_break,                  "break"),                  \
-	TOKEN_KIND(Token_continue,               "continue"),               \
-	TOKEN_KIND(Token_fallthrough,            "fallthrough"),            \
-	TOKEN_KIND(Token_defer,                  "defer"),                  \
-	TOKEN_KIND(Token_return,                 "return"),                 \
-	TOKEN_KIND(Token_proc,                   "proc"),                   \
-	TOKEN_KIND(Token_macro,                  "macro"),                  \
-	TOKEN_KIND(Token_struct,                 "struct"),                 \
-	TOKEN_KIND(Token_union,                  "union"),                  \
-	TOKEN_KIND(Token_enum,                   "enum"),                   \
-	TOKEN_KIND(Token_bit_field,              "bit_field"),              \
-	TOKEN_KIND(Token_map,                    "map"),                    \
-	TOKEN_KIND(Token_static,                 "static"),                 \
-	TOKEN_KIND(Token_dynamic,                "dynamic"),                \
-	TOKEN_KIND(Token_auto_cast,              "auto_cast"),              \
-	TOKEN_KIND(Token_cast,                   "cast"),                   \
-	TOKEN_KIND(Token_transmute,              "transmute"),              \
-	TOKEN_KIND(Token_distinct,               "distinct"),               \
-	TOKEN_KIND(Token_using,                  "using"),                  \
-	TOKEN_KIND(Token_inline,                 "inline"),                 \
-	TOKEN_KIND(Token_no_inline,              "no_inline"),              \
-	TOKEN_KIND(Token_context,                "context"),                \
-	TOKEN_KIND(Token_size_of,                "size_of"),                \
-	TOKEN_KIND(Token_align_of,               "align_of"),               \
-	TOKEN_KIND(Token_offset_of,              "offset_of"),              \
-	TOKEN_KIND(Token_type_of,                "type_of"),                \
-	TOKEN_KIND(Token_const,                  "const"),                  \
-	TOKEN_KIND(Token_asm,                    "asm"),                    \
-	TOKEN_KIND(Token_yield,                  "yield"),                  \
-	TOKEN_KIND(Token_await,                  "await"),                  \
+	TOKEN_KIND(Token_import,      "import"),      \
+	TOKEN_KIND(Token_export,      "export"),      \
+	TOKEN_KIND(Token_foreign,     "foreign"),     \
+	TOKEN_KIND(Token_package,     "package"),     \
+	TOKEN_KIND(Token_type,        "type"),        \
+	TOKEN_KIND(Token_when,        "when"),        \
+	TOKEN_KIND(Token_if,          "if"),          \
+	TOKEN_KIND(Token_else,        "else"),        \
+	TOKEN_KIND(Token_for,         "for"),         \
+	TOKEN_KIND(Token_switch,      "switch"),      \
+	TOKEN_KIND(Token_in,          "in"),          \
+	TOKEN_KIND(Token_do,          "do"),          \
+	TOKEN_KIND(Token_case,        "case"),        \
+	TOKEN_KIND(Token_break,       "break"),       \
+	TOKEN_KIND(Token_continue,    "continue"),    \
+	TOKEN_KIND(Token_fallthrough, "fallthrough"), \
+	TOKEN_KIND(Token_defer,       "defer"),       \
+	TOKEN_KIND(Token_return,      "return"),      \
+	TOKEN_KIND(Token_proc,        "proc"),        \
+	TOKEN_KIND(Token_macro,       "macro"),       \
+	TOKEN_KIND(Token_struct,      "struct"),      \
+	TOKEN_KIND(Token_union,       "union"),       \
+	TOKEN_KIND(Token_enum,        "enum"),        \
+	TOKEN_KIND(Token_bit_field,   "bit_field"),   \
+	TOKEN_KIND(Token_map,         "map"),         \
+	TOKEN_KIND(Token_static,      "static"),      \
+	TOKEN_KIND(Token_dynamic,     "dynamic"),     \
+	TOKEN_KIND(Token_auto_cast,   "auto_cast"),   \
+	TOKEN_KIND(Token_cast,        "cast"),        \
+	TOKEN_KIND(Token_transmute,   "transmute"),   \
+	TOKEN_KIND(Token_distinct,    "distinct"),    \
+	TOKEN_KIND(Token_using,       "using"),       \
+	TOKEN_KIND(Token_inline,      "inline"),      \
+	TOKEN_KIND(Token_no_inline,   "no_inline"),   \
+	TOKEN_KIND(Token_context,     "context"),     \
+	TOKEN_KIND(Token_size_of,     "size_of"),     \
+	TOKEN_KIND(Token_align_of,    "align_of"),    \
+	TOKEN_KIND(Token_offset_of,   "offset_of"),   \
+	TOKEN_KIND(Token_type_of,     "type_of"),     \
+	TOKEN_KIND(Token_const,       "const"),       \
+	TOKEN_KIND(Token_asm,         "asm"),         \
+	TOKEN_KIND(Token_yield,       "yield"),       \
+	TOKEN_KIND(Token_await,       "await"),       \
 TOKEN_KIND(Token__KeywordEnd, ""), \
 	TOKEN_KIND(Token_Count, "")