mirror of
				https://github.com/Ed94/gencpp.git
				synced 2025-10-31 15:00:53 -07:00 
			
		
		
		
	Added support for friend operator definitions
This commit is contained in:
		| @@ -128,12 +128,12 @@ int gen_main() | |||||||
| 		header.print( debug ); | 		header.print( debug ); | ||||||
|  |  | ||||||
| 		CodeBody parsed_memory = parse_file( project_dir "dependencies/memory.hpp" ); | 		CodeBody parsed_memory = parse_file( project_dir "dependencies/memory.hpp" ); | ||||||
| 		CodeBody memory        = def_body(ECode::Global_Body); | 		CodeBody memory        = def_body(CT_Global_Body); | ||||||
| 		for ( Code entry = parsed_memory.begin(); entry != parsed_memory.end(); ++ entry ) | 		for ( Code entry = parsed_memory.begin(); entry != parsed_memory.end(); ++ entry ) | ||||||
| 		{ | 		{ | ||||||
| 			switch (entry->Type) | 			switch (entry->Type) | ||||||
| 			{ | 			{ | ||||||
| 				case ECode::Using: | 				case CT_Using: | ||||||
| 				{ | 				{ | ||||||
| 					log_fmt("REPLACE THIS MANUALLY: %S\n", entry->Name); | 					log_fmt("REPLACE THIS MANUALLY: %S\n", entry->Name); | ||||||
| 					CodeUsing   using_ver   = cast(CodeUsing, entry); | 					CodeUsing   using_ver   = cast(CodeUsing, entry); | ||||||
| @@ -142,7 +142,7 @@ int gen_main() | |||||||
| 					memory.append(typedef_ver); | 					memory.append(typedef_ver); | ||||||
| 				} | 				} | ||||||
| 				break; | 				break; | ||||||
| 				case ECode::Function_Fwd: | 				case CT_Function_Fwd: | ||||||
| 				{ | 				{ | ||||||
| 					CodeFn fn = cast(CodeFn, entry); | 					CodeFn fn = cast(CodeFn, entry); | ||||||
| 					if ( fn->Name.is_equal(txt("free")) ) | 					if ( fn->Name.is_equal(txt("free")) ) | ||||||
| @@ -152,13 +152,13 @@ int gen_main() | |||||||
| 					memory.append(entry); | 					memory.append(entry); | ||||||
| 				} | 				} | ||||||
| 				break; | 				break; | ||||||
| 				case ECode::Function: | 				case CT_Function: | ||||||
| 				{ | 				{ | ||||||
| 					CodeFn fn = cast(CodeFn, entry); | 					CodeFn fn = cast(CodeFn, entry); | ||||||
| 					s32 constexpr_found = fn->Specs.remove( ESpecifier::Constexpr ); | 					s32 constexpr_found = fn->Specs.remove( Spec_Constexpr ); | ||||||
| 					if (constexpr_found > -1) { | 					if (constexpr_found > -1) { | ||||||
| 						log_fmt("Found constexpr: %S\n", entry.to_string()); | 						log_fmt("Found constexpr: %S\n", entry.to_string()); | ||||||
| 						fn->Specs.append(ESpecifier::Inline); | 						fn->Specs.append(Spec_Inline); | ||||||
| 					} | 					} | ||||||
| 					if ( fn->Name.is_equal(txt("free")) ) | 					if ( fn->Name.is_equal(txt("free")) ) | ||||||
| 					{ | 					{ | ||||||
| @@ -167,7 +167,7 @@ int gen_main() | |||||||
| 					memory.append(entry); | 					memory.append(entry); | ||||||
| 				} | 				} | ||||||
| 				break; | 				break; | ||||||
| 				case ECode::Template: | 				case CT_Template: | ||||||
| 				{ | 				{ | ||||||
| 					CodeTemplate tmpl = cast(CodeTemplate, entry); | 					CodeTemplate tmpl = cast(CodeTemplate, entry); | ||||||
| 					if ( tmpl->Declaration->Name.contains(txt("swap"))) | 					if ( tmpl->Declaration->Name.contains(txt("swap"))) | ||||||
| @@ -186,14 +186,14 @@ int gen_main() | |||||||
| 					} | 					} | ||||||
| 				} | 				} | ||||||
| 				break; | 				break; | ||||||
| 				case ECode::Class: | 				case CT_Class: | ||||||
| 				case ECode::Struct: | 				case CT_Struct: | ||||||
| 				{ | 				{ | ||||||
| 					CodeBody body     = cast(CodeBody, entry->Body); | 					CodeBody body     = cast(CodeBody, entry->Body); | ||||||
| 					CodeBody new_body = def_body( entry->Body->Type ); | 					CodeBody new_body = def_body( entry->Body->Type ); | ||||||
| 					for ( Code body_entry = body.begin(); body_entry != body.end(); ++ body_entry ) switch | 					for ( Code body_entry = body.begin(); body_entry != body.end(); ++ body_entry ) switch | ||||||
| 					(body_entry->Type) { | 					(body_entry->Type) { | ||||||
| 						case ECode::Preprocess_If: | 						case CT_Preprocess_If: | ||||||
| 						{ | 						{ | ||||||
| 							ignore_preprocess_cond_block(txt("GEN_SUPPORT_CPP_MEMBER_FEATURES"), body_entry, body ); | 							ignore_preprocess_cond_block(txt("GEN_SUPPORT_CPP_MEMBER_FEATURES"), body_entry, body ); | ||||||
| 						} | 						} | ||||||
| @@ -208,7 +208,7 @@ int gen_main() | |||||||
| 					memory.append(entry); | 					memory.append(entry); | ||||||
| 				} | 				} | ||||||
| 				break; | 				break; | ||||||
| 				case ECode::Preprocess_If: | 				case CT_Preprocess_If: | ||||||
| 				{ | 				{ | ||||||
| 					b32 found = ignore_preprocess_cond_block(txt("GEN_SUPPORT_CPP_MEMBER_FEATURES"), entry, parsed_memory ); | 					b32 found = ignore_preprocess_cond_block(txt("GEN_SUPPORT_CPP_MEMBER_FEATURES"), entry, parsed_memory ); | ||||||
| 					if (found) break; | 					if (found) break; | ||||||
| @@ -216,7 +216,7 @@ int gen_main() | |||||||
| 					memory.append(entry); | 					memory.append(entry); | ||||||
| 				} | 				} | ||||||
| 				break; | 				break; | ||||||
| 				case ECode::Preprocess_IfDef: | 				case CT_Preprocess_IfDef: | ||||||
| 				{ | 				{ | ||||||
| 					b32 found = ignore_preprocess_cond_block(txt("GEN_INTELLISENSE_DIRECTIVES"), entry, parsed_memory ); | 					b32 found = ignore_preprocess_cond_block(txt("GEN_INTELLISENSE_DIRECTIVES"), entry, parsed_memory ); | ||||||
| 					if (found) break; | 					if (found) break; | ||||||
| @@ -224,7 +224,7 @@ int gen_main() | |||||||
| 					memory.append(entry); | 					memory.append(entry); | ||||||
| 				} | 				} | ||||||
| 				break; | 				break; | ||||||
| 				case ECode::Preprocess_Pragma: | 				case CT_Preprocess_Pragma: | ||||||
| 				{ | 				{ | ||||||
| 					b32 found = swap_pragma_region_implementation( txt("FixedArena"), gen_fixed_arenas, entry, memory); | 					b32 found = swap_pragma_region_implementation( txt("FixedArena"), gen_fixed_arenas, entry, memory); | ||||||
| 					if (found) break; | 					if (found) break; | ||||||
| @@ -244,12 +244,12 @@ int gen_main() | |||||||
| 		header.print( string_ops ); | 		header.print( string_ops ); | ||||||
|  |  | ||||||
| 		CodeBody printing_parsed = parse_file( project_dir "dependencies/printing.hpp" ); | 		CodeBody printing_parsed = parse_file( project_dir "dependencies/printing.hpp" ); | ||||||
| 		CodeBody printing        = def_body(ECode::Global_Body); | 		CodeBody printing        = def_body(CT_Global_Body); | ||||||
| 		for ( Code entry = printing_parsed.begin(); entry != printing_parsed.end(); ++ entry ) | 		for ( Code entry = printing_parsed.begin(); entry != printing_parsed.end(); ++ entry ) | ||||||
| 		{ | 		{ | ||||||
| 			switch (entry->Type) | 			switch (entry->Type) | ||||||
| 			{ | 			{ | ||||||
| 				case ECode::Preprocess_IfDef: | 				case CT_Preprocess_IfDef: | ||||||
| 				{ | 				{ | ||||||
| 					b32 found = ignore_preprocess_cond_block(txt("GEN_INTELLISENSE_DIRECTIVES"), entry, printing_parsed ); | 					b32 found = ignore_preprocess_cond_block(txt("GEN_INTELLISENSE_DIRECTIVES"), entry, printing_parsed ); | ||||||
| 					if (found) break; | 					if (found) break; | ||||||
| @@ -257,7 +257,7 @@ int gen_main() | |||||||
| 					printing.append(entry); | 					printing.append(entry); | ||||||
| 				} | 				} | ||||||
| 				break; | 				break; | ||||||
| 				case ECode::Variable: | 				case CT_Variable: | ||||||
| 				{ | 				{ | ||||||
| 					if (contains(entry->Name, txt("Msg_Invalid_Value"))) | 					if (contains(entry->Name, txt("Msg_Invalid_Value"))) | ||||||
| 					{ | 					{ | ||||||
| @@ -275,7 +275,7 @@ int gen_main() | |||||||
| 		} | 		} | ||||||
| 		header.print(dump_to_scratch_and_retireve(printing)); | 		header.print(dump_to_scratch_and_retireve(printing)); | ||||||
|  |  | ||||||
| 		CodeBody containers = def_body(ECode::Global_Body); | 		CodeBody containers = def_body(CT_Global_Body); | ||||||
| 		{ | 		{ | ||||||
| 			containers.append( def_pragma(code(region Containers))); | 			containers.append( def_pragma(code(region Containers))); | ||||||
|  |  | ||||||
| @@ -291,24 +291,24 @@ int gen_main() | |||||||
| 		header.print( hashing ); | 		header.print( hashing ); | ||||||
|  |  | ||||||
| 		CodeBody parsed_strings = parse_file( project_dir "dependencies/strings.hpp" ); | 		CodeBody parsed_strings = parse_file( project_dir "dependencies/strings.hpp" ); | ||||||
| 		CodeBody strings        = def_body(ECode::Global_Body); | 		CodeBody strings        = def_body(CT_Global_Body); | ||||||
| 		for ( Code entry = parsed_strings.begin(); entry != parsed_strings.end(); ++ entry ) | 		for ( Code entry = parsed_strings.begin(); entry != parsed_strings.end(); ++ entry ) | ||||||
| 		{ | 		{ | ||||||
| 			switch (entry->Type) | 			switch (entry->Type) | ||||||
| 			{ | 			{ | ||||||
| 				case ECode::Preprocess_If: | 				case CT_Preprocess_If: | ||||||
| 				{ | 				{ | ||||||
| 					ignore_preprocess_cond_block(txt("! GEN_COMPILER_C"), entry, parsed_strings); | 					ignore_preprocess_cond_block(txt("! GEN_COMPILER_C"), entry, parsed_strings); | ||||||
| 				} | 				} | ||||||
| 				break; | 				break; | ||||||
|  |  | ||||||
| 				case ECode::Preprocess_IfDef: | 				case CT_Preprocess_IfDef: | ||||||
| 				{ | 				{ | ||||||
| 					ignore_preprocess_cond_block(txt("GEN_INTELLISENSE_DIRECTIVES"), entry, parsed_strings ); | 					ignore_preprocess_cond_block(txt("GEN_INTELLISENSE_DIRECTIVES"), entry, parsed_strings ); | ||||||
| 				} | 				} | ||||||
| 				break; | 				break; | ||||||
|  |  | ||||||
| 				case ECode::Struct_Fwd: | 				case CT_Struct_Fwd: | ||||||
| 				{ | 				{ | ||||||
| 					if ( entry->Name.is_equal(txt("String")) ) | 					if ( entry->Name.is_equal(txt("String")) ) | ||||||
| 					{ | 					{ | ||||||
| @@ -322,13 +322,13 @@ int gen_main() | |||||||
| 				} | 				} | ||||||
| 				break; | 				break; | ||||||
|  |  | ||||||
| 				case ECode::Struct: | 				case CT_Struct: | ||||||
| 				{ | 				{ | ||||||
| 					CodeBody body     = cast(CodeBody, entry->Body); | 					CodeBody body     = cast(CodeBody, entry->Body); | ||||||
| 					CodeBody new_body = def_body( entry->Body->Type ); | 					CodeBody new_body = def_body( entry->Body->Type ); | ||||||
| 					for ( Code body_entry = body.begin(); body_entry != body.end(); ++ body_entry ) switch | 					for ( Code body_entry = body.begin(); body_entry != body.end(); ++ body_entry ) switch | ||||||
| 					(body_entry->Type) { | 					(body_entry->Type) { | ||||||
| 						case ECode::Preprocess_If: | 						case CT_Preprocess_If: | ||||||
| 						{ | 						{ | ||||||
| 							b32 found = ignore_preprocess_cond_block(txt("! GEN_COMPILER_C"), body_entry, body ); | 							b32 found = ignore_preprocess_cond_block(txt("! GEN_COMPILER_C"), body_entry, body ); | ||||||
| 							if (found) break; | 							if (found) break; | ||||||
|   | |||||||
| @@ -5,7 +5,7 @@ using namespace gen; | |||||||
|  |  | ||||||
| CodeBody gen_fixed_arenas() | CodeBody gen_fixed_arenas() | ||||||
| { | { | ||||||
| 	CodeBody result = def_body(ECode::Global_Body); | 	CodeBody result = def_body(CT_Global_Body); | ||||||
| 	result.append(def_pragma(txt("region FixedArena"))); | 	result.append(def_pragma(txt("region FixedArena"))); | ||||||
|  |  | ||||||
| 	char const* template_struct = stringize( | 	char const* template_struct = stringize( | ||||||
|   | |||||||
| @@ -17,13 +17,13 @@ b32 ignore_preprocess_cond_block( StrC cond_sig, Code& entry_iter, CodeBody& bod | |||||||
| 		s32 depth = 1; | 		s32 depth = 1; | ||||||
| 		++ entry_iter; for(b32 continue_for = true; continue_for && entry_iter != body.end(); ) switch | 		++ entry_iter; for(b32 continue_for = true; continue_for && entry_iter != body.end(); ) switch | ||||||
| 		(entry_iter->Type) { | 		(entry_iter->Type) { | ||||||
| 			case ECode::Preprocess_If: | 			case CT_Preprocess_If: | ||||||
| 			case ECode::Preprocess_IfDef: | 			case CT_Preprocess_IfDef: | ||||||
| 			case ECode::Preprocess_IfNotDef: | 			case CT_Preprocess_IfNotDef: | ||||||
| 				depth ++; | 				depth ++; | ||||||
| 			break; | 			break; | ||||||
|  |  | ||||||
| 			case ECode::Preprocess_EndIf: | 			case CT_Preprocess_EndIf: | ||||||
| 			{ | 			{ | ||||||
| 				depth --; | 				depth --; | ||||||
| 				if (depth == 0) { | 				if (depth == 0) { | ||||||
| @@ -56,7 +56,7 @@ bool swap_pragma_region_implementation( StrC region_name, SwapContentProc* swap_ | |||||||
|  |  | ||||||
| 		++ entry_iter; for(b32 continue_for = true; continue_for; ++entry_iter) switch | 		++ entry_iter; for(b32 continue_for = true; continue_for; ++entry_iter) switch | ||||||
| 		(entry_iter->Type) { | 		(entry_iter->Type) { | ||||||
| 			case ECode::Preprocess_Pragma: | 			case CT_Preprocess_Pragma: | ||||||
| 			{ | 			{ | ||||||
| 				CodePragma possible_end_region = cast(CodePragma, entry_iter); | 				CodePragma possible_end_region = cast(CodePragma, entry_iter); | ||||||
| 				if ( possible_end_region->Content.contains(endregion_sig) ) { | 				if ( possible_end_region->Content.contains(endregion_sig) ) { | ||||||
|   | |||||||
| @@ -449,7 +449,7 @@ void to_string(CodeFriend self, String* result ) | |||||||
| { | { | ||||||
| 	append_fmt( result, "friend %S", to_string(self->Declaration) ); | 	append_fmt( result, "friend %S", to_string(self->Declaration) ); | ||||||
|  |  | ||||||
| 	if ( self->Declaration->Type != CT_Function && (* result)[ length(* result) - 1 ] != ';' ) | 	if ( self->Declaration->Type != CT_Function && self->Declaration->Type != CT_Operator && (* result)[ length(* result) - 1 ] != ';' ) | ||||||
| 	{ | 	{ | ||||||
| 		append( result, ";" ); | 		append( result, ";" ); | ||||||
| 	} | 	} | ||||||
|   | |||||||
| @@ -905,11 +905,11 @@ CodeBody parse_class_struct_body( TokType which, Token name ) | |||||||
| 			break; | 			break; | ||||||
|  |  | ||||||
| 			case Tok_Operator: | 			case Tok_Operator: | ||||||
| 				if ( currtok.Text[0] != '~' ) | 				//if ( currtok.Text[0] != '~' ) | ||||||
| 				{ | 				//{ | ||||||
| 					log_failure( "Operator token found in global body but not destructor unary negation\n%s", to_string(Context) ); | 				//	log_failure( "Operator token found in global body but not destructor unary negation\n%s", to_string(Context) ); | ||||||
| 					return InvalidCode; | 				//	return InvalidCode; | ||||||
| 				} | 				//} | ||||||
|  |  | ||||||
| 				member = parse_destructor(); | 				member = parse_destructor(); | ||||||
| 				// ~<Name>() | 				// ~<Name>() | ||||||
| @@ -3859,7 +3859,8 @@ CodeFriend parse_friend() | |||||||
| 	eat( Tok_Decl_Friend ); | 	eat( Tok_Decl_Friend ); | ||||||
| 	// friend | 	// friend | ||||||
|  |  | ||||||
| 	CodeFn function = { nullptr }; | 	CodeFn       function = { nullptr }; | ||||||
|  | 	CodeOperator op       = { nullptr }; | ||||||
|  |  | ||||||
| 	// Type declaration or return type | 	// Type declaration or return type | ||||||
| 	CodeTypename type = parse_type(); | 	CodeTypename type = parse_type(); | ||||||
| @@ -3892,6 +3893,12 @@ CodeFriend parse_friend() | |||||||
| 		// if ( params ) | 		// if ( params ) | ||||||
| 			// function->Params = params; | 			// function->Params = params; | ||||||
| 	} | 	} | ||||||
|  | 	 | ||||||
|  | 	// Operator declaration or definition | ||||||
|  | 	if ( currtok.Type == Tok_Decl_Operator ) | ||||||
|  | 	{ | ||||||
|  | 		op = parse_operator_after_ret_type( ModuleFlag_None, NullCode, type ); | ||||||
|  | 	} | ||||||
|  |  | ||||||
| 	CodeComment inline_cmt = NullCode; | 	CodeComment inline_cmt = NullCode; | ||||||
| 	if ( function && function->Type == CT_Function_Fwd ) | 	if ( function && function->Type == CT_Function_Fwd ) | ||||||
| @@ -3912,6 +3919,8 @@ CodeFriend parse_friend() | |||||||
|  |  | ||||||
| 	if ( function ) | 	if ( function ) | ||||||
| 		result->Declaration = function; | 		result->Declaration = function; | ||||||
|  | 	else if ( op ) | ||||||
|  | 		result->Declaration = op; | ||||||
| 	else | 	else | ||||||
| 		result->Declaration = type; | 		result->Declaration = type; | ||||||
|  |  | ||||||
|   | |||||||
| @@ -25,13 +25,13 @@ struct StrC | |||||||
| 	operator char const* ()               const { return Ptr; } | 	operator char const* ()               const { return Ptr; } | ||||||
| 	char const& operator[]( ssize index ) const { return Ptr[index]; } | 	char const& operator[]( ssize index ) const { return Ptr[index]; } | ||||||
|  |  | ||||||
| #if GEN_SUPPORT_CPP_MEMBER_FUNCTIONS | #if GEN_SUPPORT_CPP_MEMBER_FEATURES | ||||||
| 	bool   is_equal            (StrC rhs)                { return are_equal(* this, rhs); } | 	bool        is_equal            (StrC rhs)                const { return GEN_NS are_equal(* this, rhs); } | ||||||
| 	char*  back                ()                        { return back(* this); } | 	char const* back                ()                        const { return GEN_NS back(* this); } | ||||||
| 	bool   contains            (StrC substring)          { return contains(* this, substring); } | 	bool        contains            (StrC substring)          const { return GEN_NS contains(* this, substring); } | ||||||
| 	String duplicate           (AllocatorInfo allocator) { return duplicate(* this, allocator); } | 	StrC        duplicate           (AllocatorInfo allocator) const { return GEN_NS duplicate(* this, allocator); } | ||||||
| 	b32    starts_with         (StrC substring)          { return starts_with(* this, substring); } | 	b32         starts_with         (StrC substring)          const { return GEN_NS starts_with(* this, substring); } | ||||||
| 	String visualize_whitespace()                        { return visualize_whitespace(* this); } | 	StrC        visualize_whitespace(AllocatorInfo allocator) const { return GEN_NS visualize_whitespace(* this, allocator); } | ||||||
| #endif // GEN_SUPPORT_CPP_MEMBER_FUNCTIONS | #endif // GEN_SUPPORT_CPP_MEMBER_FUNCTIONS | ||||||
| #endif // GEN_COMPILERC | #endif // GEN_COMPILERC | ||||||
| }; | }; | ||||||
|   | |||||||
| @@ -7,6 +7,7 @@ | |||||||
| #   pragma clang diagnostic ignored "-Wunknown-pragmas" | #   pragma clang diagnostic ignored "-Wunknown-pragmas" | ||||||
| #	pragma clang diagnostic ignored "-Wvarargs" | #	pragma clang diagnostic ignored "-Wvarargs" | ||||||
| #	pragma clang diagnostic ignored "-Wunused-function" | #	pragma clang diagnostic ignored "-Wunused-function" | ||||||
|  | #	pragma clang diagnostic ignored "-Wbraced-scalar-init" | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
| #ifdef __GNUC__ | #ifdef __GNUC__ | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user