Browse Source

`new` as a user-level procedure

Ginger Bill 8 years ago
parent
commit
3ab481df17
5 changed files with 13 additions and 8 deletions
  1. 1 5
      code/demo.odin
  2. 5 0
      core/_preload.odin
  3. 3 1
      src/check_expr.cpp
  4. 2 2
      src/checker.cpp
  5. 2 0
      src/ir.cpp

+ 1 - 5
code/demo.odin

@@ -4,11 +4,7 @@ import (
 
 
 proc main() {
-	proc new_type(T: type) -> ^T {
-		return ^T(alloc(size_of(T), align_of(T)));
-	}
-
-	var ptr = new_type(int);
+	var ptr = new(int);
 	ptr^ = 123;
 
 	fmt.println(ptr^);

+ 5 - 0
core/_preload.odin

@@ -264,6 +264,11 @@ proc resize(ptr: rawptr, old_size, new_size: int, alignment: int = DEFAULT_ALIGN
 }
 
 
+proc new(T: type) -> ^T {
+	return ^T(alloc(size_of(T), align_of(T)));
+}
+
+
 
 proc default_resize_align(old_memory: rawptr, old_size, new_size, alignment: int) -> rawptr {
 	if old_memory == nil {

+ 3 - 1
src/check_expr.cpp

@@ -3784,7 +3784,7 @@ bool check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id
 
 
 	switch (id) {
-	case BuiltinProc_new:
+	// case BuiltinProc_new:
 	case BuiltinProc_make:
 	case BuiltinProc_size_of:
 	case BuiltinProc_align_of:
@@ -3878,6 +3878,7 @@ bool check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id
 		operand->type  = type;
 	} break;
 
+	#if 0
 	case BuiltinProc_new: {
 		// proc new(Type) -> ^Type
 		Operand op = {};
@@ -3890,6 +3891,7 @@ bool check_builtin_procedure(Checker *c, Operand *operand, AstNode *call, i32 id
 		operand->mode = Addressing_Value;
 		operand->type = make_type_pointer(c->allocator, type);
 	} break;
+	#endif
 	#if 0
 	case BuiltinProc_new_slice: {
 		// proc new_slice(Type, len: int) -> []Type

+ 2 - 2
src/checker.cpp

@@ -27,7 +27,7 @@ enum BuiltinProcId {
 	BuiltinProc_len,
 	BuiltinProc_cap,
 
-	BuiltinProc_new,
+	// BuiltinProc_new,
 	BuiltinProc_make,
 	BuiltinProc_free,
 
@@ -75,7 +75,7 @@ gb_global BuiltinProc builtin_procs[BuiltinProc_COUNT] = {
 	{STR_LIT("len"),              1, false, Expr_Expr},
 	{STR_LIT("cap"),              1, false, Expr_Expr},
 
-	{STR_LIT("new"),              1, false, Expr_Expr},
+	// {STR_LIT("new"),              1, false, Expr_Expr},
 	{STR_LIT("make"),             1, true,  Expr_Expr},
 	{STR_LIT("free"),             1, false, Expr_Stmt},
 

+ 2 - 0
src/ir.cpp

@@ -3772,6 +3772,7 @@ irValue *ir_build_builtin_proc(irProcedure *proc, AstNode *expr, TypeAndValue tv
 
 	} break;
 
+	#if 0
 	case BuiltinProc_new: {
 		ir_emit_comment(proc, str_lit("new"));
 		// proc new(Type) -> ^Type
@@ -3807,6 +3808,7 @@ irValue *ir_build_builtin_proc(irProcedure *proc, AstNode *expr, TypeAndValue tv
 		}
 		return v;
 	} break;
+	#endif
 
 	case BuiltinProc_make: {
 		ir_emit_comment(proc, str_lit("make"));