Browse Source

Add `#by_ptr` procedure attribute to enforce a parameter to be passed by pointer internally

gingerBill 3 years ago
parent
commit
c8ab1b7ee1
5 changed files with 27 additions and 4 deletions
  1. 19 1
      src/check_type.cpp
  2. 1 0
      src/entity.cpp
  3. 3 2
      src/llvm_backend_general.cpp
  4. 2 0
      src/parser.cpp
  5. 2 1
      src/parser.hpp

+ 19 - 1
src/check_type.cpp

@@ -1616,6 +1616,10 @@ Type *check_get_params(CheckerContext *ctx, Scope *scope, Ast *_params, bool *is
 					error(name, "'#any_int' can only be applied to variable fields");
 					p->flags &= ~FieldFlag_any_int;
 				}
+				if (p->flags&FieldFlag_by_ptr) {
+					error(name, "'#by_ptr' can only be applied to variable fields");
+					p->flags &= ~FieldFlag_by_ptr;
+				}
 
 				param = alloc_entity_type_name(scope, name->Ident.token, type, EntityState_Resolved);
 				param->TypeName.is_type_alias = true;
@@ -1692,10 +1696,17 @@ Type *check_get_params(CheckerContext *ctx, Scope *scope, Ast *_params, bool *is
 
 				if (p->flags&FieldFlag_no_alias) {
 					if (!is_type_pointer(type)) {
-						error(name, "'#no_alias' can only be applied to fields of pointer type");
+						error(name, "'#no_alias' can only be applied pointer typed parameters");
 						p->flags &= ~FieldFlag_no_alias; // Remove the flag
 					}
 				}
+				if (p->flags&FieldFlag_by_ptr) {
+					if (is_type_internally_pointer_like(type)) {
+						error(name, "'#by_ptr' can only be applied to non-pointer-like parameters");
+						p->flags &= ~FieldFlag_by_ptr; // Remove the flag
+					}
+				}
+
 				if (is_poly_name) {
 					if (p->flags&FieldFlag_no_alias) {
 						error(name, "'#no_alias' can only be applied to non constant values");
@@ -1713,6 +1724,10 @@ Type *check_get_params(CheckerContext *ctx, Scope *scope, Ast *_params, bool *is
 						error(name, "'#const' can only be applied to variable fields");
 						p->flags &= ~FieldFlag_const;
 					}
+					if (p->flags&FieldFlag_by_ptr) {
+						error(name, "'#by_ptr' can only be applied to variable fields");
+						p->flags &= ~FieldFlag_by_ptr;
+					}
 
 					if (!is_type_constant_type(type) && !is_type_polymorphic(type)) {
 						gbString str = type_to_string(type);
@@ -1745,6 +1760,9 @@ Type *check_get_params(CheckerContext *ctx, Scope *scope, Ast *_params, bool *is
 			if (p->flags&FieldFlag_const) {
 				param->flags |= EntityFlag_ConstInput;
 			}
+			if (p->flags&FieldFlag_by_ptr) {
+				param->flags |= EntityFlag_ByPtr;
+			}
 
 			param->state = EntityState_Resolved; // NOTE(bill): This should have be resolved whilst determining it
 			add_entity(ctx, scope, name, param);

+ 1 - 0
src/entity.cpp

@@ -83,6 +83,7 @@ enum EntityFlag : u64 {
 	EntityFlag_CustomLinkage_LinkOnce = 1ull<<44,
 	
 	EntityFlag_Require = 1ull<<50,
+	EntityFlag_ByPtr = 1ull<<51, // enforce parameter is passed by pointer
 
 	EntityFlag_Overridden    = 1ull<<63,
 };

+ 3 - 2
src/llvm_backend_general.cpp

@@ -1958,11 +1958,12 @@ LLVMTypeRef lb_type_internal(lbModule *m, Type *type) {
 					if (e->flags & EntityFlag_CVarArg) {
 						continue;
 					}
-
 					Type *e_type = reduce_tuple_to_single_type(e->type);
 
 					LLVMTypeRef param_type = nullptr;
-					if (is_type_boolean(e_type) &&
+					if (e->flags & EntityFlag_ByPtr) {
+						param_type = lb_type(m, alloc_type_pointer(e_type));
+					} else if (is_type_boolean(e_type) &&
 					    type_size_of(e_type) <= 1) {
 						param_type = LLVMInt1TypeInContext(m->ctx);
 					} else {

+ 2 - 0
src/parser.cpp

@@ -3561,6 +3561,7 @@ enum FieldPrefixKind : i32 {
 	FieldPrefix_auto_cast,
 	FieldPrefix_any_int,
 	FieldPrefix_subtype, // does not imply `using` semantics
+	FieldPrefix_by_ptr,
 };
 
 struct ParseFieldPrefixMapping {
@@ -3578,6 +3579,7 @@ gb_global ParseFieldPrefixMapping parse_field_prefix_mappings[] = {
 	{str_lit("const"),      Token_Hash,      FieldPrefix_const,     FieldFlag_const},
 	{str_lit("any_int"),    Token_Hash,      FieldPrefix_any_int,   FieldFlag_any_int},
 	{str_lit("subtype"),    Token_Hash,      FieldPrefix_subtype,   FieldFlag_subtype},
+	{str_lit("by_ptr"),     Token_Hash,      FieldPrefix_by_ptr,    FieldFlag_by_ptr},
 };
 
 

+ 2 - 1
src/parser.hpp

@@ -302,13 +302,14 @@ enum FieldFlag : u32 {
 	FieldFlag_const     = 1<<5,
 	FieldFlag_any_int   = 1<<6,
 	FieldFlag_subtype   = 1<<7,
+	FieldFlag_by_ptr    = 1<<8,
 
 	// Internal use by the parser only
 	FieldFlag_Tags      = 1<<10,
 	FieldFlag_Results   = 1<<16,
 
 	// Parameter List Restrictions
-	FieldFlag_Signature = FieldFlag_ellipsis|FieldFlag_using|FieldFlag_no_alias|FieldFlag_c_vararg|FieldFlag_auto_cast|FieldFlag_const|FieldFlag_any_int,
+	FieldFlag_Signature = FieldFlag_ellipsis|FieldFlag_using|FieldFlag_no_alias|FieldFlag_c_vararg|FieldFlag_auto_cast|FieldFlag_const|FieldFlag_any_int|FieldFlag_by_ptr,
 	FieldFlag_Struct    = FieldFlag_using|FieldFlag_subtype|FieldFlag_Tags,
 };